why am I getting random variables?

I’m trying to make a simple system that takes any number I specify, and sends it through serial…

I’m pretty new to this so the programming is VERY basic and primitive

but this is what I’ve come up with:

int incomingByte;
void setup()
{
Serial.begin(9600);
}

void loop()
{
incomingByte = Serial.read();

if(incomingByte == ‘9’)
{
Serial.print(‘9’);
}
if(incomingByte == ‘8’)
{
Serial.print(‘8’);
}
if(incomingByte == ‘7’)
{
Serial.print(‘7’);
}
if(incomingByte == ‘6’)
{
Serial.print(‘6’);
}
if(incomingByte == ‘5’)
{
Serial.print(‘5’);
}
if(incomingByte == ‘4’)
{
Serial.print(‘4’);
}
if(incomingByte == ‘3’)
{
Serial.print(‘3’);
}
if(incomingByte == ‘2’)
{
Serial.print(‘2’);
}
if(incomingByte == ‘1’)
{
Serial.print(‘1’);
}
if(incomingByte == ‘0’)
{
Serial.print(‘0’);
}
if(incomingByte == ‘’)
{
Serial.print(’
’);
}
if(incomingByte == ‘#’)
{
Serial.print(’#’);
}

else
{
Serial.print(‘INVALID!’);
}

}

it should in theory do what I want it to, but it doesn’t

instead TX goes crazy and prints 44117 over and over at high speed rendering it useless…

what did I do wrong?

You are missing the fundamental method of what you must do to receive a serial character inside your sketch. First you must always perform a test to make sure that there is a complete character ready to be read before you can read it:

if (Serial.available() > 0) {
        // read the incoming byte:
        incomingByte = Serial.read();

Note the testing for a character being avalible in the if statement before performing the Serial.read statement which will actually perform the reading of the character.

Further study of the Serial libraries function should help you also.

http://arduino.cc/en/Reference/Serial

Lefty

apparently the issue got resolved when I deleted the

else
{
  Serial.print('INVALID!');
}

part of the code...

which makes sense... I assume it's trying to print "invalid" when I'm not doing anything...

but yeah, I did forget to check for input...

edit:

this:

if (Serial.available() > 0) {
        // read the incoming byte:
        incomingByte = Serial.read();}

does NOT make it any better

this however:

while (Serial.available() == 0);

seems to work a lot better... however after I press a number, it prints the number, followed by 17441 so if I were to type 6 it would print 617441

how do I fix that bit?

note that I put the Serial.Print('INVALID!') part back in to see if I could make it work with it

It was doing what you told it to do. :)

When the input buffer is empty is empty, read() will return a -1. That is not equal to '#' so it prints the number 'INVALID!'.

If you wanted a character string, use double quotes: "INVALID!"

If you want it to print "INVALID!" when the input does not match ANY of the choices you have to put 'else' after EACH 'if' statement, not just the last one. As written, any input that is not '#' will trigger the 'INVALID' message.

Since in this case you don't want to execute more than one of those code paths in any given loop, you can change all but the first "if" to an "else if". This will make the loop run faster because as soon as one of the conditions is met and the associated block is executed, program execution will jump back to the beginning of the loop instead of testing all the other conditions.

The code could use a switch statement:

int incomingByte;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available() >0) 
  {
    incomingByte = Serial.read();
    switch(incomingByte)
    {
    case '1' : Serial.print('one'); 
      break;
    case '2' : Serial.print('two'); 
      break;
    case '3' : Serial.print('three'); 
      break;
   // more of these ... ommitted
    default: Serial.print('other'); 
      break;
    }
  }
}

johnmchilton:
Since in this case you don’t want to execute more than one of those code paths in any given loop, you can change all but the first “if” to an “else if”. This will make the loop run faster because as soon as one of the conditions is met and the associated block is executed, program execution will jump back to the beginning of the loop instead of testing all the other conditions.

yeah, I thought about that…
but didn’t really bother too much with it…

thanks you guys!

I’ll post new issues as they come along…
hopefully my lazy mind will want to learn something from it…