So I am writing a code which when a button is pressed once it types "1" and once it is pressed a second time it types "2" and then the cycle repeats itself. Two issues though:
-
On the first button click, it types "1" and then 500ms later it types "2" as well.
-
I can't get it to change the buttoncount back to 1 after the second button press.
Any help would be greatly appreciated, thanks.
#include <Keyboard.h>
const int button = 3;
int buttonState = 0;
int buttoncount = 1;
void setup()
{
pinMode(button, INPUT_PULLUP);
Keyboard.begin();
}
void loop()
{
buttonState = digitalRead(button);
if (buttoncount == 1 && buttonState == LOW)
{
Keyboard.print("1");
Keyboard.releaseAll();
buttoncount++;
delay(500);
}
if (buttoncount == 2 && buttonState == LOW)
{
Keyboard.print("2");
Keyboard.releaseAll();
buttoncount == 1;
delay(500);
}
}
In the second if block, you don't assign the new value to buttoncount, but you do a compare
Hello ripbowenrip,
Welcome to the forum.
It drives people on here nuts when new members don't post their code properly. For posting correctly on your first post ++Karma.
This isn't your problem but...
delay(500);
Learn NOT to use delay as soon as you can! Delay means the processor is sitting there doing nothing else other than waiting for the delay to end, so, in the case of reading buttons, if you press your button during the delay period you button press will be ignored.
Have you seen 'blink without delay' under examples in the IDE?
try changing int buttoncount = 1; to int buttoncount = 0;
Then look into debounce - you won't regret it......
What does buttonState do for you?
You never set it to anything but 0
buttoncount == 1;
Go to the reference section of this site and study up on the difference between = and ==. They do two completely different things.
sterretje:
In the second if block, you don't assign the new value to buttoncount, but you do a compare
But I thought the new value was set by the first block when I put "buttoncount++"
PerryBebbington:
Learn NOT to use delay as soon as you can! Delay means the processor is sitting there doing nothing else other than waiting for the delay to end, so, in the case of reading buttons, if you press your button during the delay period you button press will be ignored.
Haha yeah I stared leaning this because I was into a usb rubber ducky type thing and they always cram delays down one's throat and I also thought that may be a problem contributing to me issue.
P.s. thanks for the karma whatever that is
After much thought and random typing I fixed it!
The issue was that since I was using a variable (or integer [sorry i only know js]) as a shortcut for digitalRead, once it was set to "LOW" it was stuck as "LOW". No idea why but I'm 91% sure that's what happened. Thank you all for your input it helped alot.
#include <Keyboard.h>
const int button = 3;
int buttoncount = 0;
void setup()
{
pinMode(button, INPUT_PULLUP);
Keyboard.begin();
}
void loop()
{
if (buttoncount == 0 && digitalRead(button) == LOW)
{
Keyboard.print("1");
Keyboard.releaseAll();
buttoncount++;
delay(500);
}
if (buttoncount == 1 && digitalRead(button) == LOW)
{
Keyboard.print("2");
Keyboard.releaseAll();
buttoncount = 0;
delay(500);
}
}
void loop()
{
buttonState = digitalRead(button);
if (buttoncount == 1 && buttonState == LOW)
{
Keyboard.print("1");
Keyboard.releaseAll();
buttoncount++;
delay(500);
}
if (buttoncount == 2 && buttonState == LOW)
{
Yes, if you look here you can see why. When you go into the first if statement with buttonState being LOW and buttoncount being 1, it will increment buttoncount to 2 and wait 500ms. Then it comes out and we get the next if statement. You aren't reading the button again with another digitalRead. buttonState still has the same value it's had since you set it last which is still LOW and now buttoncount is indeed 2. It's not going to wait for loop to run agian it's ready now. It should maybe have been an else if so the two were exclusive on one pass of loop.
By reading the pin again in your new code you've solved that problem. It looks like you've also fixed the = vs == problem too.
I was into a usb rubber ducky type thing and they always cram delays down one's throat
Does that mean something that isn't rude?
PerryBebbington:
Does that mean something that isn't rude?
Yeah a nicer way of saying that would be "they stress the importance of delays on slower systems" but where's the fun in that?
I can only think that the language in your part of the world has some colourful euphemisms!