Hi,
Well, certainly not quite junk, but useless all the same. I've two buttons and am using one to increment and one to decrement a buttonCount variable each time they are pressed. The idea is it works like a volume display. The result is written to the serial monitor.
I want it to display '1' and then '2' as I press the up button twice, then '1' as I press the down button once.
The fault is not quite uniform each time, but it broadly goes like this -
A whole continuing string of zeros, then 'up' a whole string of twos, then 'up' a whole string of fours then 'up' a whole string of sixes then 'down' a whole string of fives then 'down' a whole string of threes etc.
I think there are two problems -
a) continually writes buttonCount when it should do it once only after a state change
b) counts in twos - no idea.
I would be very grateful for some help
Regards,
Al
The code is here -
//declarations
//constant variables
const int upButton = 7;
const int downButton = 8;
//variable variables
//buttonCount will later become my speed variable
int buttonCount = 0;
//quicker or slower
int upButtonState = 0;
//quicker or slower
int downButtonState = 0;
//last time through the loop,for comparison
int lastButtonState = 0;
void setup()
{
// put your setup code here, to run once:
pinMode (upButton, INPUT);
pinMode (downButton, INPUT);
Serial.begin(9600);
}
void loop()
{
// put your main code here, to run repeatedly:
//first do the upButtonState checking
upButtonState = digitalRead(upButton);
if (upButtonState != lastButtonState)
{
if (upButtonState == HIGH)
{
buttonCount ++;
}
delay (50);
}
lastButtonState = upButtonState;
Serial.print(buttonCount);
//now do the downButtonState checking
downButtonState = digitalRead(downButton);
if (downButtonState != lastButtonState)
// if they are different, now need to decrement the buttonCount
{
if (downButtonState == HIGH)
{
buttonCount --;
}
delay (50);
}
lastButtonState = downButtonState;
Serial.print(buttonCount);
}