Dear Forum,
I would like to toggle the state of an output pin each time I push a button. The button is a transient make button so it connects the poles together each time it is pressed for the duration of the press. Not sure what the proper name for this type of button is.
I have found an example on the arduino site for debouncing the switch. The example uses digitalread to check the state of a digital pin.
Because i am limited in the number of digital pins I can use for this task (I have 4 push buttons) and need to control most of the
digital outputs of the arduino uno, I thought I could use the analogread function instead to see if the input to one of these pins is high or low.
If the analog input is left floating, then it will be noisy and constantly switch between high a low states. Therefore, can I use a resistor (say 10K) to pull the input to ground so that it reads zero volts?
I would then use the switch to send 5volts to the analog input with the 5 volts pin of the arduino connected to the analog input side of the 10K resistor.
I have tried this but it is not very repeatable. It does toggle but not reliably. Does any one have any suggestions first for the electronics and second for the code.
The code i am using is below. The function is called in the main loop and the variables not defined in the function are declared as global variables.
Thank you,
Nick
int checkpushbuttons(int WhichButton)
{
float AnalogReading;
int AnalogState;
int buttonState;
int ledPin;
int lastButtonState;
AnalogReading = analogRead(WhichButton);
if (AnalogReading > 4.0)
{
AnalogState = 1;
}
else
{
AnalogState = 0;
}
if (WhichButton == 1)
{
lastButtonState = lastButtonState1;
}
if (WhichButton == 2)
{
lastButtonState = lastButtonState2;
}
if (WhichButton == 3)
{
lastButtonState = lastButtonState3;
}
if (WhichButton == 4)
{
lastButtonState = lastButtonState4;
}
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (AnalogState != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = AnalogState;
}
lastButtonState = AnalogState;
if (WhichButton == 1)
{
if (AnalogState == 1) // then it has been pressed and so should be high
{
lastButtonState1 = !lastButtonState1;
digitalWrite(ledPin1, lastButtonState1);
}
}
if (WhichButton == 2)
{
lastButtonState2 = !lastButtonState2;
digitalWrite(ledPin2, lastButtonState2);
}
if (WhichButton == 3)
{
lastButtonState3 = !lastButtonState3;
digitalWrite(ledPin3, lastButtonState3);
}
if (WhichButton == 4)
{
lastButtonState4 = !lastButtonState4;
digitalWrite(ledPin4, lastButtonState4);
}
}
NickH:
Because i am limited in the number of digital pins I can use for this task (I have 4 push buttons) and need to control most of the
digital outputs of the arduino uno, I thought I could use the analogread function instead to see if the input to one of these pins is high or low.
Analog pins can be used as digital pins. Use digitalRead instead of "analogread."
I'm not sure what "AnalogState" is or where you are going with that, but you might want to read a couple more tutorials and rethink your game plan with that code.
Maybe do a little research on the difference between analog and digital.
Thanks, I do know the difference between analog and digital. Perhaps my message was not clear.
Since i have run out of digital pins......
I am trying to read an analog voltage on the analog inputs and if the voltage is more than a particular value (4.0V according to the code), then I say it is "on" by setting a variable called AnalogState to 1 and if below, I say it is off and set it to zero. I am using an analog threshold to decide if something is true or false. By using a push switch to put ~5 volts into the analoginput, I can detect whether the button has been pushed. I am now looking for a nice way to ensure that the code responds to a proper press and not noise and some verification that putting a resistor onto the analog input in the same way that a resistor is used to pull up or pull down a digital pin will not cause any damage to the arduino.
NickH,
Just use the analog pins as digital inputs, treat them just the same, declared as pins 14 to 19. That is not really necessary, but it helps me keep track of what each pin is used for what.
Analog read returns 0 to 1023, so to check for 4+ volts in your code you needed to check for 768+ on analogRead, but if using digital, just wire the button to ground when closed and use somethin like this:
button1=14;
setup()
{
pinMode(button1, INPUT_PULLUP);
}
void loop()
{
int val1 = digitalRead(switchPin); // read input value and store it in val1
delay(3); //very simple debounce use your favorite
if (val1 == LOW)
{
Do what you need here
}
}
NickH:
Thanks, I do know the difference between analog and digital. Perhaps my message was not clear.
Since i have run out of digital pins......
I am trying to read an analog voltage on the analog inputs and if the voltage is more than a particular value (4.0V according to the code), then I say it is "on" by setting a variable called AnalogState to 1 and if below, I say it is off and set it to zero. I am using an analog threshold to decide if something is true or false. By using a push switch to put ~5 volts into the analoginput, I can detect whether the button has been pushed. I am now looking for a nice way to ensure that the code responds to a proper press and not noise and some verification that putting a resistor onto the analog input in the same way that a resistor is used to pull up or pull down a digital pin will not cause any damage to the arduino.
Nick
AnalogReading = analogRead(WhichButton);
if (AnalogReading > 4.0)
//zoomkat LED button toggle test 11-08-2012
int button = 5; //button pin, connect to ground as button
int press = 0;
boolean toggle = true;
void setup()
{
pinMode(13, OUTPUT); //LED on pin 13
pinMode(button, INPUT); //arduino monitor pin state
digitalWrite(5, HIGH); //enable pullups to make pin 5 high
}
void loop()
{
press = digitalRead(button);
if (press == LOW)
{
if(toggle)
{
digitalWrite(13, HIGH); // set the LED on
toggle = !toggle;
}
else
{
digitalWrite(13, LOW); // set the LED off
toggle = !toggle;
}
}
delay(500); //delay for debounce
}