I have got an Arduino UNO. I wrote a script to control the lights connected to PIN 2, 3, 4 on my Arduino. The lights can be turned off or on using my laptop:
- 0 turns off all the LEDS
- 1 turns on LED on pin 1
- 2 turns on LED on pin 2
- 3 turns on LED on pin 1 and 2
- 4 turns on LED on pin 3
- 5 turns on LED on pin 1 and 3
- 6 turns on LED on pin 2 and 3
- 7 turns on all the LEDS.
I am actually interested in only sending data from my laptop and blink the lights accordingly.
Here's my program:
# pragma GCC optimize ("Ofast")
# define LED1 2
# define LED2 3
# define LED3 4
char buf[4] ;
unsigned char inp, len ;
void setup() {
pinMode(LED1, OUTPUT) ;
pinMode(LED2, OUTPUT) ;
pinMode(LED3, OUTPUT) ;
Serial.begin(115200) ;
Serial.setTimeout(1) ;
}
void loop() {
if (Serial.available() > 0) {
len = Serial.readBytes(buf, 3) ;
if (len) {
buf[3] = '\0' ;
inp = atoi(buf) ;
// Illuminate LEDs
digitalWrite(LED1, 1 & inp ? HIGH : LOW) ;
digitalWrite(LED2, 2 & inp ? HIGH : LOW) ;
digitalWrite(LED3, 4 & inp ? HIGH : LOW) ;
}
}
}
It works fine when I open the IDE's Serial Monitor and I write printf 1 > /dev/ttyUSB0
.
But when I close the Serial monitor, and printf 1 > /dev/ttyUSB0
, it doesn't work, and the only thing I get is the flashing onboard LED.
So every time I need to light up an LED, I must make sure the IDE is open.
Is there a way to write to the Arduino serial port from any Linux computer without opening the IDE?