Go Down

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


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:
  // 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);
   case 'b':    
     digitalWrite(23, HIGH);
   case 'c':    
     digitalWrite(24, HIGH);
   case 'd':    
     digitalWrite(25, HIGH);
   case 'e':    
     digitalWrite(26, HIGH);
   case 'f':    
     digitalWrite(27, HIGH);
   case 'g':    
     digitalWrite(28, HIGH);
     // turn all the LEDs off:
     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.


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

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131