I'm not a terribly experienced programmer, I can't seem to figure out how to get this working.
The code is supposed to let you enter three numbers in the serial monitor and apply them as voltages through three pins connected to a tricolor LED.
The bug is that when you enter a value to break the first while loop, the serial monitor displays the message to enter the blue value again, then enter the green value twice. It never gets to enter red value. It will display the set of three messages even if the value entered is not greater than 0.
Maybe I don't understand the serial monitor well enough.
Thanks in advance for any help, and sorry it's not commented, i was planning to just delete it once it worked but that didn't work out.
EDIT: more info, when a correct number is entered, the LED turns on immediately, where it should wait for all numbers to be entered. The color that appears is not the color entered, and seems to be a purple color (red and blue power applied). When negative numbers are entered, the correct color turns on.
How are you entering the values? Your sketch seems to be reading binary values one byte at a time. Do you have something sending it binary data? If you're sending the numbers via the serial monitor, you'll be sending ascii character sequences which you will need to parse to get the corresponding numeric values. I notice that you aren't checking there is a byte available to read before you try to read it, so I guess you're getting a lot of -1s most of the time.
PeterH:
How are you entering the values? Your sketch seems to be reading binary values one byte at a time. Do you have something sending it binary data? If you're sending the numbers via the serial monitor, you'll be sending ascii character sequences which you will need to parse to get the corresponding numeric values. I notice that you aren't checking there is a byte available to read before you try to read it, so I guess you're getting a lot of -1s most of the time.
I'm entering them numerically, as I said, I don't know much about the serial connection. So are you saying I need to enter a binary version of the decimal number I want to enter?
I'm not checking it, should I be?
After reading your reply and doing some experimenting, I see what's going on for the most part. It seems that when multiple digits are entered, it checks the first one, breaks the loop and clears the first digit, then uses the next digit, breaking each loop in turn based on how many digits were entered.
So how do I enter a value between 0 and 1023? Do I need to use serial read multiple times and store each digit entered in an array? Or is there a simpler command for this? I saw in the webpage for serial read that you can use Serial.print as "Serial.print(variable, DEC)".
Two problems here. First, the values in inByte are ASCII. Look up the ASCII code for 0 to 9. The values are NOT 0 to 9.
Second, look at the range of value that analogWrite() accepts for the second argument. 1023 is NOT a valid value.
There is a much simpler way to do this, that allows you to enter something like <12, 67, 198> and let the Arduino perform all the data collection, parsing, and conversion to ints.
#define SOP '<'
#define EOP '>'
bool started = false;
bool ended = false;
char inData[80];
byte index;
void setup()
{
Serial.begin(57600);
// Other stuff...
}
void loop()
{
// Read all serial data available, as fast as possible
while(Serial.available() > 0)
{
char inChar = Serial.read();
if(inChar == SOP)
{
index = 0;
inData[index] = '\0';
started = true;
ended = false;
}
else if(inChar == EOP)
{
ended = true;
break;
}
else
{
if(index < 79)
{
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
}
// We are here either because all pending serial
// data has been read OR because an end of
// packet marker arrived. Which is it?
if(started && ended)
{
// The end of packet marker arrived. Process the packet
// Reset for the next packet
started = false;
ended = false;
index = 0;
inData[index] = '\0';
}
}
Where the "Process the packet" comment is, you can add code to call strtok() and atoi() to parse the inData string, and convert the tokens to ints.