I want to create a LED Sunrise/Sundown alarm clock. Therefore, I will use a Raspberry Pi that is sending commands over serial to a Teensy 3.1. Like:
- "30 Minute Sunrise starting with Color(255,140,0)"
- "20 Minute Sundown with Color(255,140,0)"
- "start something else with these 3 parameter.
But I don't even get the basic serial part working. E.g. I tried this:
void setup() {
Serial.begin(9600);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
if (Serial.available() > 0) {
if (Serial.read() == 'A'){
int inte1 = Serial.parseInt();
int inte2 = Serial.parseInt();
int inte3 = Serial.parseInt();
int inte4 = Serial.parseInt();
int inte5 = Serial.parseInt();
if (Serial.read() == '\n') {
Serial.print(inte1);
Serial.print(inte2);
Serial.print(inte3);
Serial.print(inte4);
Serial.println(inte5);
}
}
}
}
I planned to do a case
switch afterwards. Inte1
should define sunrise/sundown, Inte2
the minutes, 'Inte3-5' the rgb color.
However, when I enter "A 1 100 100 100" it is not printing anything at all.
What is the best way do get this working?
Thanks in advance!
1 Answer 1
Okay, as Majenko and Nick Gammon have suggested. The \n
had not arrived in the serial buffer. That way the if clause was not fulfilled and nothing was printed. This code has worked for me now:
if (Serial.available() > 0) {
if (Serial.read() == 'A'){
char inte1 = Serial.read();
int inte2 = Serial.parseInt();
int inte3 = Serial.parseInt();
int inte4 = Serial.parseInt();
int inte5 = Serial.parseInt();
Serial.print(inte1);
Serial.print(inte2);
Serial.print(inte3);
Serial.print(inte4);
Serial.println(inte5);
}
}
Although i read quite a bit now, serial input is still a mystery to me.
\n
and expecting it to already be in the serial buffer when it hasn't actually arrived yet. Don't expect it to be there immediately you have parsed the last int.