all these suggest
while (Serial.available ()==0){}
which doesn't really work cause it seems there's always some junk data in the serial so it just doesn't wait
I would like to be able to input r or y and then a number but as it stands if I try to enter them one after the other I have something like a second to get the integer input in before the line is skipped
const int ledPins [2]= {10, 11}; //11 is red, 10 is yellow
char colourinput;
int flashinput;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(ledPins, OUTPUT);
Serial.println("input colour r/y and number of flashes, hit enter, repeat indefinitely");
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available ()==0){}
if (Serial.available ()>0)
{
colourinput=Serial.read();
if (colourinput=='r')
{
while (Serial.available ()<1){}
while (!Serial){}
flashinput=Serial.parseInt(); //Serial.println(flashinput); //test line
for (int i = 0; i<flashinput; i++)
{
digitalWrite (ledPins[1], HIGH);
delay (150);
digitalWrite (ledPins[1],LOW);
delay (100);
}
}
else if (colourinput=='y')
{
while (Serial.available ()<1){}
flashinput=Serial.parseInt(); //Serial.println(flashinput); //test line
for (int i = 0; i<flashinput; i++)
{
digitalWrite (ledPins[0], HIGH);
delay (150);
digitalWrite (ledPins[0],LOW);
delay (100);
}
}
}
}
//this has an issue wherein should I wait too long before inputting an integer it completely ignores things
// after printing the initial line it seems there's always something in the serial so "while (Serial.available ()<1){}" does nothing
Are you sure that the junk data is not just the Line Ending characters inserted by the Serial monitor because that is how you have specified it should behave in the Line Ending setting of the SM ?
I have something like a second to get the integer input in before the line is skipped
That's what comes of using Serial.parseInt(), Consider using Serial.readBytesUntil() instead
TheMemberFormerlyKnownAsAWOL:
What do you think the "junk data," is?
Truly random sh*t, or something that maybe ought to be there, but something you don't want to, or know how to, handle?
with a previous program I had an issue where it was truly random shit
series of characters that I had never input or asked it to print
I think in this case it's likely that the previous input (either r or y) is still in the buffer even after colourinput is being assigned to those values
UKHeliBob:
Are you sure that the junk data is not just the Line Ending characters inserted by the Serial monitor because that is how you have specified it should behave in the Line Ending setting of the SM ?
I'm sorry, I'm not really familiar enough with this to understand this
UKHeliBob:
That's what comes of using Serial.parseInt(), Consider using Serial.readBytesUntil() instead
this is sort of inline with what I'm looking for however if the program isn't stopping to wait for that input it's not gonna matter right?
I'm sorry, I'm not really familiar enough with this to understand this
Open the Serial monitor and look at the bottom of the screen
There is an option dropdown that allows you to select which characters, if any, will be appended to the text entered by the user.
Depending on how you read the Serial data then these characters may still be in the Serial buffer after you read the text, so a test for whether there is serial data available will return true and your program may read them when what you are expecting is text
1. Very very hard for me to follow your UART program logic; I have executed your sketch; I have entered r5 from the InputBox of the Serial Monitor (with Line ending tab at option Newline to terminate parseInt method, Fig-1) and then clicked on the Send Button of SM/Enter Key of PC ; I have not seen my LED connected at DPin-11 to flash for 5 times. (Note: pinMode(ledpins, OUTPUT) code does not work; I have used traditional method of initializing each pin separately.)
Figure-1:
2. You may re-structure your posted sketch based on solid principles/understanding of the UART communication protocol. Given below my sketch that works on DPin-13/L for rD command (yD command is not yet implemented) which you may follow to moderate your sketch.
const int ledPins [2] = {13, 11}; //11 is red, 10 is yellow
char colourinput;
int flashinput;
bool flag;
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
// pinMode(ledPins, OUTPUT); does not work
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Serial.println("input colour r/y and number of flashes, hit enter, repeat indefinitely");
}
void loop()
{
byte n = Serial.available(); //check if charctaer(s)has been accumulated in buffer
if ( n != 0) //there is at least onr charcater in the buffer
{
if (flag == false)
{
char colourinput = Serial.read(); //read the charcater expecting it as r
Serial.print(colourinput);
if (colourinput == 'r') //checking if it is r
{
flag = true; //r found; catch next charcaters for the number of flahes
}
}
else //r already detected
{
flashinput = Serial.parseInt();
Serial.println(flashinput);
for (byte i = 0; i < flashinput; i++)
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
flag = false;
}
}
}