Switchcase statement issue and Arduino MEGA
This question is from Gary Kahne:
- Arduino Course for Absolute Beginners, 2nd Edition
- Control Structures
Question:
Here is the code that was giving Gary trouble:
/*This program takes user input from a computer
through the Arduino IDE serial monitor "send" line.
The values sent are checked against a switch case
statement.
Each case turns on a different LED, or has an effect
on the LEDs attached to pins on the connected Arduino.
*/
/* these are pins that will be used for the LEDs.
*/
const byte LED_1_Pin = 6;
const byte LED_2_Pin = 7;
const byte LED_3_Pin = 8;
const byte LED_4_Pin = 9;
/*Define start and stop point for "for loops"
NOTE: these varaible will only be helpful in
"for loops" if all the pins used are contiguos
to each other.
*/
const byte firstLEDpin = 6;
const byte lastLEDpin = 9;
void setup() {
/*Initiate serial comunication for talking between
the computer and Arduino.
*/
Serial.begin(9600);
//Set the mode of the LED pins as OUTPUTS
for (int i = firstLEDpin; i < lastLEDpin + 1; i++) {
pinMode(i, OUTPUT);
}
}
void loop() {
/* Check to see if there are any bytes available
in the incoming serial buffer - the user would
have typed and sent these in the Arduino IDE
serial monitor "send line".
*/
if (Serial.available() > 0) {
/*If there is data available, then read out the
first byte and assign its value to a variable
*/
byte userSelection = Serial.read();
/*Based on the selection of the user
use a switch case to perfrom a
specific task
*/
switch (userSelection) {
case 'a':
digitalWrite(LED_1_Pin, HIGH);
break;
case 'b':
digitalWrite(LED_2_Pin, HIGH);
break;
case 'c':
digitalWrite(LED_3_Pin, HIGH);
break;
case 'd':
digitalWrite(LED_4_Pin, HIGH);
break;
case 'e':
for (int i = firstLEDpin; i < lastLEDpin + 1; i++) {
digitalWrite(i, HIGH);
}
break;
default:
for (int i = firstLEDpin; i < lastLEDpin + 1; i++) {
digitalWrite(i, LOW);
}
break;
}//Close switch case.
}//Close if.
}//Close loop.
Hello Michael,
I am sorry to trouble you but I was hoping you might let me know if the above referenced sketch should be working on my Mega 2600.
My guess is ‘No’, but if you knew for sure I could leave it alone…or buy a new board….(if this proves to be problematic in the lessons to follow).
I know you recommended a simpler board, but I had this one on hand and until today it has worked.
Try as I might I can not get this to work on my Arduino Mega 2560. No LED lights
As I VERY much doubt that there was a mistake in your code, I did the following:
1) verified that other sketches (done to date) still work with my board, they do, so I presume board is not damaged.
2) Tried the following sketch to see the results:
int incomingByte = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// show in serial monitor
Serial.print("I received: ");
Serial.println(incomingByte, BIN);
}
}
And the results were as expected!
3) I then integrated this into your sketch for the lesson. Wow! some strange results.
Still no LEDs were lighting, but the results of Serial.println(incomingByte, BIN);
made no sense whatsoever. Strings of more than 4 letters only returned 4 results (only when putting the above sketch in your lesson sketch ).
4) checked over your code for any sly teaching tricks…(not your style).
So I am guessing (actually hoping) that this is a quirk of arduino MEGA??
Thank you in advance for your trouble,
Gary
Your Challenge:
Take a look at the code…
What do you think, is it an issue with the MEGA or not? If not, what might be the issue.
Hint:
- It’s not an issue with the MEGA or the code, but something to do with setting on the Serial Monitor window.
Solution:
Here is what I came up with…
[fvplayer src=”https://vimeo.com/333774013″ transcript=”auto” splash=”https://i.vimeocdn.com/video/779860675_1280x720.jpg?r=pad” caption=”Gary Kahne – MEGA”]

Had a similar issue but worked when I set serial monitor to ‘no line ending’.