Go Down

Topic: Tutorial Interfacing with arduino over serial (Read 485 times) previous topic - next topic

Dreded

I made a video tutorial on how to receive commands from the computer over serial and recognize specific characters to do specific functions.

It will be a two part tutorial. Part1 is the arduino code and part2 will be some code written in python so for part 1 look here:

http://dredx.com/electronics/?p=199

for those more curious in just the code I wrote in the tutorial here it is...

Code: [Select]

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
int state;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  if (Serial.available() > 0) {
    if (Serial.peek() == 'c') { //check for the character that signifies that this will be a command
      Serial.read(); //remove the character that signifies this is a command from the serial buffer
      state = Serial.parseInt(); //store our expected integer into state
      digitalWrite(led,state); // set the state of the LED
    }
    while (Serial.available() > 0){ //Discard everything that we didn't expect
      Serial.read();
    }
  }
}

Dreded

Part 2 which details the python side of things is posted here: http://dredx.com/electronics/2013/01/python-tutorial-to-send-data-to-arduino/

Go Up