Go Down

Topic: [solved] Serial Communication over Programming USB Port (Read 848 times) previous topic - next topic

sinnaro

Nov 22, 2012, 04:39 am Last Edit: Nov 22, 2012, 08:12 am by Massimo Banzi Reason: 1
Hello, I am trying to figure out serial communication on the Due programming port with the below test code:

Code: [Select]

void setup() {
 // initialize serial communication:
 Serial.begin(9600);
  // initialize the LED pins:
 for (int thisPin = 22; thisPin <= 28; thisPin++) {
   pinMode(thisPin, OUTPUT);
 }
}

void loop() {
 // read the sensor:
 if (Serial.available() > 0) {
   int inByte = Serial.read();
   switch (inByte) {
   case 'a':    
     digitalWrite(22, HIGH);
     break;
   case 'b':    
     digitalWrite(23, HIGH);
     break;
   case 'c':    
     digitalWrite(24, HIGH);
     break;
   case 'd':    
     digitalWrite(25, HIGH);
     break;
   case 'e':    
     digitalWrite(26, HIGH);
     break;
   case 'f':    
     digitalWrite(27, HIGH);
     break;
   case 'g':    
     digitalWrite(28, HIGH);
     break;      
   default:
     // turn all the LEDs off:
     delay(1000);
     Serial.println("Turning all pins off");
     for (int thisPin = 22; thisPin <= 28; thisPin++) {
       digitalWrite(thisPin, LOW);
     }
   }
 }
}


I've got the digital output pins 22-28 hooked up to LEDs on my breadboard and sending the characters 'a'-'f' through the Serial Monitor should activate one of the LEDs and any other character should turn them all off. My LEDs light up as expected for each character, but only stay lit for 1 second (due to the delay(1000) in the default: of the switch()) and the print statement in the default case is always printed. It's as if my switch statement is going to the correct case in addition to the default case.

Anyone have some input on this problem? The only thing I can think of is that Serial.available() is returning '1' more than expected.

sinnaro

I figured it out. It was reading the new line character in as Serial input and that was activating the default case. This can be fixed in the Serial Monitor by selecting "No line ending" in the drop down box on the bottom. See here: http://i.imgur.com/IM2vo.png

Go Up