August 29, 2014

Arduino: avrdude stk500_getsync(): not in sync resp=0x30 error

1. Make sure you have Arduino driver installed correctly (serial com is working)

2. Make sure serial port is correct

3. When uploading the sketch, make sure there is no connection at Tx (pin0) and Rx (pin1)

Arduino : Serial communication with interrupt

The other day, I helped Joe with his project. The project requires the Arduino to listen to the Bluetooth communication (sent via an Android App) while also running its own program. The Bluetooth module uses the serial comm. To make it work, I'll use this basic code for the interrupt which when triggered, it takes the Bluetooth communication's input.

// To use this example, you have to connect Rx pin (digital pin 0) to interrupt 0 pin (digital pin 2).
void setup()
{
    // Using interrupt 0 on digital pin 2.
    pinMode(2, INPUT);
    digitalWrite(2, LOW);
   
    Serial.begin(9600);
    attachInterrupt(0, serialInterrupt, CHANGE);

    // Used to signal that main loop is alive.

    pinMode(4, OUTPUT);
    digitalWrite(4, LOW);

    // Used to signal that Serial input was read.

    pinMode(5, OUTPUT);
    digitalWrite(5, LOW);
}

void loop()

{
  // Do something using even delays. There is an interrupt for that (Serial I/O)!

  // Blink led to signal loop is alive.

  digitalWrite(4, HIGH);
  delay(500);

  digitalWrite(4, LOW);
  delay(500);
}

// Volatile, since it is modified in an ISR.

volatile boolean inService = false;

void serialInterrupt()

{
  // Trick: since Serial I/O in interrupt driven, we must reenable interrupts while in this Interrupt Service Routine.
  // But doing so will cause the routine to be called nestedly, causing problems.
  // So we mark we are already in service.

  // Already in service? Do nothing.

  if (inService) return;

  // You was not in service. Now you are.

  inService = true;

  // Reenable interrupts, to allow Serial to work. We do this only if inService is false.
  interrupts();

  // Allow serial to read at least one byte.
  while(!Serial.available());

  // Blink led to signal Serial data arrived.

  digitalWrite(5, !digitalRead(5));
  byte data = Serial.read();

  // Echo data back to developer ;-)

  Serial.print(data);

  // Job done.

  inService = false;
}

August 21, 2014

Raspberry Pi - run a script from boot up using Crontab

1. First is add a SHEBANG line on top of your python script
#!/usr/bin/env python

2. Make your script executable with
chmod a+x

3. Open crontab with
sudo crontab -e

4. Add the following line. Make sure to include the full path to the script (MyScript.py is the name of your script)
@reboot python /home/pi/Desktop/MyScript.py &

5. To save these changes click “CTRL-X”, then “Y” and finally “Return”. You should now be back at the command prompt.

To start testing you can now reboot