Here is a snippet of my code (ps i'm just learning make-shift electronics and arduino code, however I have a decent knowledge of C/C++)
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
sevenSegWrite(count);
count++;
Serial.println(count);
if (count>9){
count=0;
}
delay(100);
}
obviously it's a pushbutton thing sketch. I'm experimenting with a seven segment display. Here's my problem, the numbers are continuously counting up when I'm NOT pressing the button and then freeze when I push the button. Now, if i change if (buttonState == HIGH) to if (buttonState == LOW) then the number changes when I push the button (and also continuously counts when I hold the button, but thats not a problem).
I have also checked the switch to make sure it's off-on-off (both with the packaging and also my multimeter). What's going on??
your code doesn't check if it has counted before... as long as the button is or isn't pressed, it will keep counting.
if you do
buttonState = digitalRead(buttonPin);
unsigned char flag = 0;
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH && flag == 0) {
flag = 1;
// turn LED on:
sevenSegWrite(count);
count++;
Serial.println(count);
if (count>9){
count=0;
}
else { flag = 0;};
}
It might just work.
don't use delay unless you have a good (you don't) reason.
Normally when you wire up a button to a digital input, there's a pull-up resistor which holds the input at 5V. When you push the button it grounds the digital input.
5V is HIGH and ground is LOW.
Exactly what you told it: increment the count every 100 milliseconds while the button is up (HIGH) or down (LOW).
Are you trying to get it to increment every time it's pushed? If so, you're looking for edge detection. In other words, you want to know the single instance it is pushed down (goes from HIGH to LOW) or released (LOW to HIGH). To accomplish that you need to keep track of the last value you read and compare it to the current value. If they are different, you've detected an edge.
bubulindo:
your code doesn't check if it has counted before... as long as the button is or isn't pressed, it will keep counting.
if you do
buttonState = digitalRead(buttonPin);
unsigned char flag = 0;
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH && flag == 0) {
flag = 1;
// turn LED on:
sevenSegWrite(count);
count++;
Serial.println(count);
if (count>9){
count=0;
}
else { flag = 0;};
}
It might just work.
don't use delay unless you have a good (you don't) reason.
Negative, display stays on 0 (default when starting up is 0) and serial monitor also records no feedback.
Exactly what you told it: increment the count every 100 milliseconds while the button is up (HIGH) or down (LOW).
Are you trying to get it to increment every time it's pushed? If so, you're looking for edge detection. In other words, you want to know the single instance it is pushed down (goes from HIGH to LOW) or released (LOW to HIGH). To accomplish that you need to keep track of the last value you read and compare it to the current value. If they are different, you've detected an edge.
However HIGH is when current is flowing into the input and LOW is when its "not" (not talking about floating lines ATM). So when the button is not being pushed, it's LOW isnt it? and when the button is pushed then it's HIGH.
bag06a:
However HIGH is when current is flowing into the input and LOW is when its "not" (not talking about floating lines ATM). So when the button is not being pushed, it's LOW isnt it? and when the button is pushed then it's HIGH.
HIGH is when there is a voltage potential of at least X volts across the input pin and ground.
LOW is when there is a voltage potential of at most, Y volts across the input pin and ground.
So it depends on how your have it wired/configured with a pull up or pull down resistor.
In your case (assuming it's not floating), it is wired/configured with a pull-up resistor, meaning it is "pulled" high. When you press the button, you tie the input pin to ground, causing it to read LOW.
Code is fixed. I implemented the edge detection and that worked. I think I also changed something else before implementing edge-detection (cant remember what lol)
new code is
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH && oldButtonState == LOW) {
// turn LED on:
sevenSegWrite(count);
count++;
if (count>9){
count=0;
}
}
oldButtonState = buttonState;
thanks guys.
I know this may have been an elementary problem, but we all gotta start somewhere....right?