Hi
I'm using the program below to PWM 2 led's on pins 10 and 11. Using the serial monitor the code will accept strings of characters
like c100b220. the number following the "c" is what varies the brightness of the led on pin 11 and the number following the "b"
varies the brightness of an led on pin 10. This all works if I use the serial monitor to send in these strings ( with numbers between 0-255) - even if I put in strings like 12c200b30 (I put the serial read after the if( x >= '0' && x <='9') step to read spurious numbers like the leading 12 off the buffer to simulate how things would be if there was an initial mismatch between send signal and receive). It works even if I feed in a group like c200b230c30b220 etc. to simulate a continuous stream of data.
However if I use a sending program to send the strings to the arduino, it will set the brightnesses correctly - but only once - after receiving the first pair of numbers! After that, changing the sent string does nothing - the system sticks with the first set of brightnesses sent. I've used a setup like this for a single string like c200 and it works fine with a sending program but with two in I run into problems. Can anyone suggest where my error might lie. I know the sending program may be the problem but it's just a standard liberty basic program sending "c"+A$+"b"+B$ via the com port where A$ and B$ are the numbers to change the brightnesses. The code is
char x;
int result_c = 0;
int result_b = 0;//temporary store for data sent
// a variable to store incoming integer serial data
int outputPin11 =11; // Use pin 11 (~ a PWM pin) as the output to an LED
// and 330 ohm resitor to GND.
int outputPin10 = 10;// Use pin 10 (~ a PWM pin) as the output to an LED
// and 330 ohm resitor to GND.
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(outputPin11, OUTPUT);
pinMode(outputPin10, OUTPUT);
}
void loop() {
while (Serial.available() > 0)// See if there are characters in
//the serial buffer
{
x = Serial.peek();
if( x == 'c')
{
Serial.read();
result_c = Serial.parseInt();
Serial.println(x);
Serial.println(result_c);
analogWrite(outputPin11, result_c);
}//end if
if( x == 'b')
{
Serial.read();
result_b = Serial.parseInt();
Serial.println(x);
Serial.println(result_b);
analogWrite(outputPin10, result_b);
}//end if
if( x >= '0' && x <='9')
{
Serial.read(); //empties the buffer of stray integers during the "while loop"
}
}//wend
// do more stuff here
}
Am I doing something wrong with the arduino code - does there need to be a delay somewhere to help the data through?
Thank you for any help.