I posted something sorta (not really) about this same topic awhile ago, but my question is, how would i write a command that, say, turns on an LED on "W" and then when i push "W" again the LED turns off?
is there a way to do something until the key is no longer pressed. i use putty for my serial com, but i want to be able to drive two DC motors until i stop pushing a button
how would i write a command that, say, turns on an LED on "W" and then when i push "W" again the LED turns off?
Create a state variable to track the state of the LED. Use the serial library to check if you've received a 'W' character. If so, check to see if the state variable is high or low and then swap it. Each iteration of loop should set the pin with the LED to the state of the variable.
The "button" example (File -> Example -> Digital -> Button) is similar to what you want to do. Instead of a button you are using Serial.available and Serial.read.
is there a way to do something until the key is no longer pressed.
The challenge is that serial transmits 1 character a time. Not the actual key press events. So what the arduino would end up seeing is: "WWWWWWWWWWWWWWWWWWWWWWWWWWW". You'd have to create a timeout for when the character stopped coming in. That's likely to be error prone.
The challenge is that serial transmits 1 character a time. Not the actual key press events. So what the arduino would end up seeing is: "WWWWWWWWWWWWWWWWWWWWWWWWWWW". You'd have to create a timeout for when the character stopped coming in. That's likely to be error prone.
Alternatively, you could have the PC send one character when the key was pressed ('W') and another when the key was released ('w'). The Arduino would then start something happening (set a pin HIGH) when one character arrived and stop it when another character arrived.
Of course, this all depends on what is sending the serial data. The Serial Monitor does not send anything when you press a key. It only sends something when you click the send button. So, there is no possibility of sending different data when a key is pressed and when it is released.