0

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!

  • Suggested reading: hacking.majenko.co.uk/reading-serial-on-the-arduino – Majenko Sep 14 '15 at 19:55
  • Thank you, that was a nice read. The examples compiles fine and I get what this is about, but still not working. Does not print a single line on the serial monitor! – Markus Sep 14 '15 at 20:27
  • Baud in Teensy and serial monitor is the same! – Markus Sep 14 '15 at 20:27
  • Have you managed to get even simple "echo what you send" serial communication working? – Majenko Sep 14 '15 at 21:13
  • 1
    Probably because you're not waiting for the \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. – Majenko Sep 14 '15 at 21:26
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.