ignoring the initial press of a key when holding it down for a short pause.

Hi All,

So i have managed to program an LED to be dimmed to off when the letter 'a' is held on the keyboard, and brightens when i hold 'q', but stays in the middle when no key is pressed, it works as i wanted.

However when i press and hold the key, it intitally detects the key press, then switches off, and then recognises it as held, it makes the led blink before doing what its told.

Is there a way of solving this to streamline the effect? Maybe ignoring that first bit of the output for a timed pause, or reducing that pause?

Thanks in advance.

My code is below. And also a small video of the system and shows the input from the keys and how they react. Im using Tera Term to activate it, and you can see the initial press of q, then the delay and then it recognises it etc.

** Edit it wont allow me to upload my video **

char incomingByte;
void setup() {
// put your setup code here, to run once:
pinMode(9, OUTPUT);
Serial.begin(9600);
analogWrite(9, 125);

}
void loop() {
// put your main code here, to run repeatedly:
Serial.print(incomingByte);
if (Serial.available() > 0 ) {
incomingByte = Serial.read();

if (incomingByte == 'q' )
{

analogWrite(9, 255);
delay(35);

}
if (incomingByte == 'a' )
{
analogWrite(9, 0);
delay(35);
}

}
else
{
incomingByte = ' ';
analogWrite(9, 125);

}

}

That code does no dimming at all. You only send a constant value based on the input. Did you post the correct code?

Also, please read the sticky post at the top of the forum about how to properly post your code inside code tags. It helps people help you.

ok will do

it uses the analogwrite to increase or decrease the 5v from the normal mid point of 125 when the keys are pressed and held.

It does work and this is the code.

thanks

john

look at the State Change Detection example sketch in the IDE.

What is the purpose of this line?

   incomingByte = ' ';

this was a way i found so that if no key is pressed, it detected this, and it results in the led being in its medium state

When ive tried controling servos, and needed a similar thing where no key press results in it centring its position ive had this same delay in operation.

If i dont use a switch / default, and if / else, or a no data detected, then its smooth, press to activate, and press another key to deactivate, it appears what ever i do with adding a default position i conflict somehow.

Its been suggested to use a different arduino where the key press / release function would be more appropriate.

Not sure which way to go forward now. It appears maybe the arduino isnt best suited to control things via a keyboard when trying to press and hold keys and then release, which is what i wanted to do.

thanks

john

I think this is what is causing a problem

else
 {
   incomingByte = ' ';
   analogWrite(9, 125);
   
 }

because it has the effect of setting the LED at 125 if any character other than 'q' or 'a' is received.

Perhaps it was meant to be

if (incomingByte == ' ') { // I assume this is a space character
       analogWrite(9, 125);
}

Note that I have removed the ELSE because it would only relate to the immediately previous IF rather than to both of the IF clauses. You need to use ELSE IF in all the tests or none of them if you want them to be related (or not).

...R