So I have this script with which im using to turn led on and off over the serial port using the software terra term. Its working fine and ive over time made variants on this software to add many leds
so I would like to do a few things but the only one I need help with is this
Firstly I would like to change it so that as long as I hold down the “1” key the led stays on and when I let go of the “1” key
char INBYTE;
int LED = 13; // LED on pin 13
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
Serial.println("Press 1 to turn Arduino pin 13 LED ON or 0 to turn it OFF:");
while (!Serial.available()); // stay here so long as COM port is empty
INBYTE = Serial.read(); // read next available byte
if( INBYTE == '0' ) digitalWrite(LED, LOW); // if it's a 0 (zero) tun LED off
if( INBYTE == '1' ) digitalWrite(LED, HIGH); // if it's a 1 (one) turn LED on
delay(50);
}
munch15a:
Firstly I would like to change it so that as long as I hold down the “1” key the led stays on and when I let go of the “1” key
The usual way to do that is to send one message when the key is pressed and a different message when the key is released. Perhaps send 'A' when it is pressed and 'a' when it is released.
It seems that TeraTerm does continue to send data when a key is held down (I just tried it) so all you need to do is to turn on the LED when a '1' is received and turn it off if you receive any other character.
Slightly smarter would be to turn on the LED once when you first receive a '1' and turn it of once when you stop receiving a '1'
sure but that would be very hard to add more leds to right ?
right now im just learning by way around but the long term project is to have 2 motors hooked up instead with a few different functions for different motor profiles
that would be very hard to add more leds to right ?
Yes, it would soon become clumsy.
If you need to control more LEDs then use arrays or an array of structs to hold data about them such as their current state, the input character or string that changes their state and possibly the time that they entered the current state if you wish to do timed operations.
You can then iterate through the array(s) with a for loop when serial data is available to decide which, if any, LED should be affected and what should be done to it such as setting its state or changing its state or even setting its brightness level if it is in a PWM pin.
The LEDs would be easier to control if there was an explicit character to turn them on and a different one to turn them off or if the control character simply changed their state from on to off or vice versa.
sure but that would be very hard to add more leds to right ?
Depends how many more you want to add.
then have something like if no key is being pressed digitalWrite(LED, LOW);
You don't seem to understand that you are NOT reading whether a key on a keyboard connected to a different computer is being pressed, or not.
You are reading serial data that tells you whether some application on that computer has determined that a key is being/has been pressed, and you don't even know whether the application can tell you that.
Typically, an OS has the ability to define how often a key-press event is generated when a key is held down. An application that subscribes to the key-press event might also subscribe to the key-release event, and decide to ignore any key-press event that follows a key-press event, without a key-release event in between.
Or, it might not. But, lets suppose that TeraTerm does. (You'll need to experiment to find out for sure.) It will write a character to the serial port each time the event occurs. Serial data transmission is relatively slow. The serial buffer is relatively small. Your buffer could be stuffed full of '1's when the 1 key is pressed so fast that data starts being lost.
Suppose, though, that you were able to read the data as fast as it arrives. There is a stream of '1's and then a stream of '3's and then some '2's. What do you do when the first '1' arrives? When the first '3' arrives? When the buffer is empty, and there is nothing to read?
Ok sure so i've obviously missed something basic here I see what your saying now I think someone else here said it should do that but a Clarifying question
So when I press a key it sends a signal
This would start a key press event
I then need a terminal that will trigger a key release event when done
So next I'm going to go and read around on line to see if I can find out if what I've got does this or find something that does
in terms of how many in the long run it will have 2 motors
but I want have WSAD controlling forward back left and right movement
then a servo to move forward and backward
but right now Im leaving the servo for later and using leds to just get my head around the communications part of the script
munch15a:
Ok sure so i've obviously missed something basic here I see what your saying now I think someone else here said it should do that but a Clarifying question
So when I press a key it sends a signal
This would start a key press event
I then need a terminal that will trigger a key release event when done
It's a terminal emulator.
It either sends a character (if a key is pressed).
Or it doesn't (send anything, if no key is pressed)
It simply doesn't do "events" - that's all down to you.
Hence my earlier answer.