Wednesday, June 12, 2013

Arduino Due communication with PC running Processing 2.0

Similar to the example "Read byte from Serial port and set pin", this example demonstrate how to control LED on Arduino Due board with PC running Processing 2.0.


It's the code in the Arduino Due side, the PC side Processing code will be in next post.

int led = 13;
int incomingByte = 0;
 
void setup() {
    pinMode(led, OUTPUT);
     
    //Setup Serial Port with baud rate of 9600
    Serial.begin(9600);
    Serial.println("Press H to turn LED ON");
    Serial.println("Press L to turn LED OFF");
}
 
void loop() {
    if (Serial.available() > 0) {
        // read the incoming byte:
        incomingByte = Serial.read();
     
        if(incomingByte == 'H'){
            digitalWrite(led, HIGH);
            Serial.println("LED ON");
        }else if(incomingByte == 'L'){
            digitalWrite(led, LOW);
            Serial.println("LED OFF");
        }else{
            Serial.println("invalid!");
        }
       
    }
}

2 comments:

  1. Hey!

    Thanks for sharing, I'm currently working through the same tutorial.
    I was just wondering if you knew why the code had to use H or L? I'v searched everywhere and can't find a reason.

    Cheers

    ReplyDelete
    Replies
    1. Just assign a key to control the LED, such that you can type a character in PC side using Serial Monitor of Arduino Software. You can assign any character you want.

      Delete