I have a program for my Arduino Uno that changes the rate at which the in-built LED blinks depending on serial input. It works fine on the Arduino IDE serial listener but not with PuTTY or bash terminal. With both of these, I get output but no matter what value I input, 0 is returned. The input is working (sort of) because I see the RX LED flash when I type. Help?
bash (preferred) command used: cu -l /dev/ttyACM0 -s 9600
PuTTY: just put used the GUI to connect.
Again, it's connected fine (apart from input not working) so it's not the PC that's the issue.
But, the code works fine on the Arduino IDE so it's not the Arduino that's the problem either...
int ledPin = 13;
unsigned long previousMillis = 0;
int interval = 250;
int ledOn = LOW;
void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("Enter frequency (ms) of flashes.");
Serial.println(interval);
}
void loop(){
unsigned long currentMillis = millis();
if (Serial.available()) {
interval = Serial.parseInt();
Serial.println(interval);
}
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (ledOn == LOW) {
ledOn = HIGH;
}
else {
ledOn = LOW;
}
digitalWrite(ledPin, ledOn);
}
}
I use stty: stty -F /dev/ttyACM0 speed 9600
After that a simple echo: echo "123#" >/dev/ttyACM0
The '#' is to stop Serial.parseInt() to wait for more digits.
The parseInt() waits for a number with a timeout.
Suppose something is available and it is the '#' or a '\r', that starts parseInt(), but no new number is given yet. So the sketch continues after parseInt() has timed out, perhaps a few times, causing the value to be zero, messing up the millis() with the delay. Also the currentMillis is set to millis() before the parseInt().
I can think of only good solution: don't use parseInt(), it will always delay in one way or the other.
Let the led blink with millis() and collect the incoming characters in a buffer. Process that buffer once a non-digit is received.
Well, okay, there is an other option. When something is available, you can peek if a digit is available. If not, read the character and don't use that character. If it is a digit, do a parseInt(). But I don't like this option
Due to an unfortunate, ah, 'misunderstanding' between Windows, Grub and myself, my computer now has no operating system. I'm writing this from a public Windows machine so I can't test your code out. Thanks for the reply and hopefully I'll have a working computer of my own soon to try it out.
You can fix grub, without disturbing the Windows and Linux. You can use a Live CD or USB stick for that. Or did that go wrong ? That's bad luck.
Or you can do like me. I running now Windows 10 Preview, but I didn't know if it could harm my other Operating Systems. So I used an old harddisk, and removed the SATA cables from the other harddisks. Then I installed Windows 10 Preview, and connected the other hardddisks again. I can select the harddisk to boot when my computer starts.