I'm having some trouble using data from the Serial Monitor as the variable used to control which case is executed. Since I can't figure out if I'm just not understanding the switch case variable or if I've written the code poorly I figured I'd post on here and see if any one could help me figure why the cases don't execute when I input the correct value for a particular case.
The goal of this code is to just cycle through and output which case was selected each time. The final inputs for the project will be from -99 to 99.
Here is the code
void setup()
{
Serial.begin(9600);
Serial.println("RAM AIR Serial comms TEST 1");
Wire.begin();
Wire.beginTransmission(0x20);// start queue
Wire.write(0x00); //write to reg A
Wire.write(0x00); //set PORT A to outputs
Wire.endTransmission(); //send
Wire.beginTransmission(0x20); //start queue
Wire.write(0x01); //write to reg B
Wire.write(0x00); //set PORTB to outputs
Wire.endTransmission(); //send
}
int AirTemp;
int sign = 1;
void loop()
{
if( Serial.available())
{
char ch = Serial.read();
if(ch >= '0' && ch <= '9')
AirTemp = (AirTemp * 10) + (ch - '0');
else if( ch == '-')
sign = -1;
else
{
AirTemp = AirTemp * sign ;
Serial.println(AirTemp);
switch (AirTemp)
{
Serial.print("AirTemp = ");
Serial.println(AirTemp);
case '-1':
{
Serial.println("case -1");
}
break;
case '10':
{
Serial.println("case 10");
}
break;
case '1':
{
Serial.println("case 1");
}
break;
case '2':
{
Serial.println("case 2");
}
break;
}
AirTemp = 0;
sign = 1;
}
}
}
These two lines Serial.print("AirTemp = ");
** Serial.println(AirTemp);**
are within your switch statement but not within any case. I'm surprised it doesn't provoke a warning. Nevertheless I'm not entirely sure what your problem is.
I would suggest, however, that you use something to terminate the string being sent from the serial monitor, perhaps a newline character. It would make your logic easier to implement (no more nested elses)
You could then do it something like this.
void setup()
{
Serial.begin(9600);
Serial.println("RAM AIR Serial comms TEST 1");
Wire.begin();
Wire.beginTransmission(0x20);// start queue
Wire.write(0x00); //write to reg A
Wire.write(0x00); //set PORT A to outputs
Wire.endTransmission(); //send
Wire.beginTransmission(0x20); //start queue
Wire.write(0x01); //write to reg B
Wire.write(0x00); //set PORTB to outputs
Wire.endTransmission(); //send
}
int AirTemp;
int sign = 1;
void loop()
{
if( Serial.available())
{
char ch = Serial.read();
if(ch >= '0' && ch <= '9')
AirTemp = (AirTemp * 10) + (ch - '0');
if( ch == '-')
sign = -1;
if(ch=='\n')
{
AirTemp = AirTemp * sign ;
Serial.println(AirTemp);
Serial.print("AirTemp = ");
Serial.println(AirTemp);
switch (AirTemp)
{
case -1:
{
Serial.println("case -1");
}
break;
case 10:
{
Serial.println("case 10");
}
break;
case 1:
{
Serial.println("case 1");
}
break;
case 2:
{
Serial.println("case 2");
}
break;
}
AirTemp = 0;
sign = 1;
}
}
}
Single quotes are for single characters. Please post a picture of your keyboard, with the ONE key that you pressed to get the ONE character in the single quotes circled.
AirTemp is an int. Why are you comparing it to characters?
Single quotes are for single characters. Please post a picture of your keyboard, with the ONE key that you pressed to get the ONE character in the single quotes circled.
AirTemp is an int. Why are you comparing it to characters?
And yet it compiles (with the additition of #include <Wire.h>).
Thanks for the help guys, the problem was that I'd forgotten to take the character quotes out and I wasn't even looking there when I was trying to figure out the problem.
KenF thanks for the help with the easier to follow code that makes it much less confusing.