This code works to turn led pin on/off with separate key inputs. Trying to work out 2 different variations...
Same key toggles the led pin. Example press H once = pin high, press H again = pin low.
Same key momentary. Example hold H = pin high, release H = pin low.
const int ledPin = 13; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') {
digitalWrite(ledPin, HIGH);
}
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {
digitalWrite(ledPin, LOW);
}
}
}
if (incomingByte == 'H') {
digitalWrite(ledPin, !digitalRead(ledPin) ); // The attention-mark is the NOT-operator
}
!false meaning "not" false = true
!true meaning "not" true = false
!LOW meaning "not" LOW = HIGH
!HIGH meaning "not" HIGH = LOW
as the serial interface only sends one or multiple-characters after pressing "Enter" or clicking on send
the serial interface of the serial monitor can not be used for your second variant
The hold key pressed down is a physical event on your keyboards computer which is not directly detected by the microcontroller.
If you wish to have something similar you would have to write a program on your computer
that sends the new byte everytime the state of the key changes
This means for this variant you need a PC-software that does state-change-dection on your PC
I guess it will be pretty easy to do this with python.
In this case, an int type would be consistent with the return type of the function called, and not become a problem when Serial.read() returns -1 when you were throwing that information away.
Even if it usually makes no difference. In this code it would not, since no characters are sought that aren't available.
Thanks Stefan works perfectly for same key toggle scenario!
For scenario 2 the case would be similar to any motor control where it actuates as long as you hold the keyboard button, in this case a key in a terminal for test.
I should have noted the use case scenario will not be Arduino terminal monitor. At this point it could be any (Linux) terminal, in my case usually Putty. Eventually (hopefully) ROS over serial but that's a way's down the road...
Thanks for the help, hardware is my thing, SW not so much but I'm learning...
The basic principle of serial transmission is:
if the "end-of-entering-data-event" is happening usually this "end-of-entering-data-event" is pressing the enter-key
then the software will send the data
one time
you are asking for: as long as I press the toggle-key "H" down keep the LED switched on
As soon as I release the "H"-key switch LED off
Your brain has to understand a serial datatransmission works
different
than a directly connected button or switch
A usual terminalprogram does
not
send a repeated "HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH....."
as long as you press down the "H"-key
This means you can't use code like
as long as you receive a new "H" keep LED on (and for how long shall the LED be ON???
How fast do you want to send the continious stream of "H"s ???
Another approach is a computer-software running on your computer
that is "watching" the state of the toggle-key "H"
if the state changed from unpressed to pressed send a serial "command" switch ON LED
if the state changed from pressed to unpressed send a serial "command" switch OFF LED
a standard-terminal-software like putty does
not
have such a mode of operation.
because putty is not made for send a single key-press
putty is made for send multiple characters once
and print received characters once
There might be more special terminal-programs you can write scipts for.
Though I doubt that such a script-language is able to monitor the pressing-state of a key on the keyboard.
As soon as you write your own code in whatever language
for the computer
not on the arduino
it will work as describe above
best regards Stefan
Try this small sketch to toggle the LED using the H letter.
const int ledPin = 13; // the pin that the LED is attached to
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
if (Serial.read()=='H') digitalWrite(ledPin,!digitalRead(ledPin));
}
}