I want to send characters to Arduino serial port which makes the Arduino turn on/off an LED. Here is the code:
int led_main=8;
void setup()
{
pinMode(led_main, OUTPUT);
Serial.begin(9600);
}
void loop()
{
char chr;
if (Serial.available())
{
chr = Serial.read();
if (chr == '0')
{
digitalWrite(led_main, LOW);
}
else if (chr == '1')
{
digitalWrite(led_main, HIGH);
}
}
}
The Arduino is recognized as either /dev/ttyACM0
or /dev/ttyACM1
in my system. When I run the command echo "1" > /dev/ttyACM0
, it doesn't work. But, when I open a second terminal, run tail -f /dev/ttyACM0
, and then in the first terminal, run echo "1" > /dev/ttyACM0
, it turns on the LED as expected. So why do I have to open a second terminal and keep monitoring the output with tail -f
in order for this to work? Is there a way to do this with a single command?