Using a PushButton as a trigger in Arduino - button

I'm trying to work with a simple Arduino circuit that increments a counter variable on pressing a push button in the circuit (connected as INPUT to PIN 8). My code is as simple as follows:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int c = 0, btnPIN, btnVal;
void setup ()
{
btnPIN = 8;
pinMode(btnPIN, INPUT);
lcd.begin(16,2);
lcd.clear();
}
void void loop()
{
btnVal = digitalRead(btnPIN);
if (btnVal == LOW)
{
c++;
lcd.clear();
lcd.print(c);
}
}
Problem is the counter increases by more than 1 every time I push the button. A little bit of printing on the serial monitor indicates that each time the button is pressed and the voltage is LOW, the conditional code is executed multiple times and the counter increases by multiple times instead of 1.
Maybe, I need to write some logic that checks if the button was initially unpressed, then pushed and then released again and then these steps would trigger the required action.
The solution I'm currently working with is as under (which works just fine):
int btnStatus = 0;
void loop()
{
btnVal = digitalRead(btnPIN);
if (btnVal == LOW)
btnStatus = 1;
if(btnStatus == 1 && btnVal == HIGH)
{
c++;
lcd.clear();
lcd.print(c);
btnStatus = 0;
}
}
I'm not sure if there's a simpler solution available or if this approach is wrong for other reasons? Any advice would be most welcome!

Another problem you you may be having is that mechanical buttons bounce. That is, they jump between two positions several times quickly before settling to a final position. This is standard operation so it is necessary to "debounce" the button.
There are very many ways to do this, but Here is a tutorial using an Arduino.
The main issue, as you probably figured out, is that the loop function is getting called multiple times while the button is down. This is what is fixed by your code, and yours looks to be a good solution and I don't really see a simpler way. For another way, though, perhaps you could try just adding a call to delay at the end of loop to slow it down a bit. You would have to play with the delay amount a bit, but it could work. Really though, your solution looks just fine.

Your idea is correct, you need to track the previous state of the button to know if it is a new press or if it is simply being held down. Your code could be rewritten to look more like a state machine, however:
typedef enum {
BTN_STATE_RELEASED,
BTN_STATE_PRESSED
} ButtonState;
ButtonState btnStatus = BTN_STATE_RELEASED;
void increment_counter()
{
c++;
lcd.clear();
lcd.print(c);
}
void loop()
{
btnVal = digitalRead(btnPIN);
switch(btnStatus)
{
case BTN_STATE_PRESSED:
// Handle button release
if(btnVal == HIGH)
{
btnStatus = BTN_STATE_RELEASED;
}
break;
case BTN_STATE_RELEASED:
// Handle button press
if(btnVal == LOW)
{
increment_counter();
btnStatus = BTN_STATE_PRESSED;
}
break;
}
}
This was rewritten to use an enum to track the state of the system (a bit overkill for a simple button, but an important concept to know in case your system grows more complex).
The code to update the display was also moved into its own function to make it better separated between the display change and the actual update of the state.

Related

Arduino timer not counting accuratly

I am working on a school project for which I rotate a servo after a cable disconnects and a specific delay is over. This is my current code. We are using an Arduino Uno powered from the USB port
#include <Servo.h>
int reader=4;
int servo1Pin=8;
Servo servo1;
int value;
int pos=10;
int wacht=5000;
void setup() {
pinMode(reader, INPUT);
servo1.attach(servo1Pin);
}
void loop() {
value = digitalRead(reader);
servo1.write(pos);
if (value == LOW) {
delay(wacht);
pos=180;
}
else {
pos=10;
}
}
wacht is the specific delay. When we disconnected pin 4 to break that circuit we don't have a consistent time between the interruption of the power flow and the opening of the servo. It seems to vary from anywhere between 5 to 40 seconds of delay after triggering. Does anyone have any ideas to solve this issue?
try change
pinMode(reader, INPUT);
to
pinMode(reader, INPUT_PULLUP);
And for clarification delay don't use timer.
The order of your instructions seem strange. the usual order is:
Read
Decide
Act
Also, you may want to avoid sending useless instructions to the servo. For example, when you know the servo is already in the correct position. You should not rely on eternal code to do the right thing. In other terms, you have no idea how long servo1.write() takes to execute.
Which would give for your loop()
void loop()
{
if (digitalRead(reader) == LOW)
{
if (pos != 180)
{
delay(wacht); // delay() is only called once, when circuit breaks.
// this guarantees immediate response when circuit
∕/ closes again.
pos = 180;
servo1.write(pos);
}
}
else if (pos != 10)
{
pos = 10;
servo1.write(pos);
}
}
Also, have a look and implement Peter Plesník's answer. This will probably solve some of your problems. You input will definitely still have a random lag of up to 5 seconds when closing the circuit, though.

Trying to build a counter/stopwatch with arduino. is this wired correctly?

i am trying to make a stopwatch and counter project for arduino. Code aside, is this wired correctly?
The top button is to start the stopwatch, second is the button to start the counter (every press increases by one), and the bottom button should be to reset any of them. the green light is to show the stopwatch is selected and blue is to show the counter is selected. the lcd is to display everything obviusly. Also, what is the best way to learn to code this and how long would it take? Thanks.
This is the code as per your requirement. I have defined pins as per above connections. counter mode has minutes and seconds and it does not contain milliseconds as I am encountering problem to implement it. You can suggest me if you have got any way. Counter mode selection button is the same button which is going to be used to increment the counter.
#include <LiquidCrystal.h>
LiquidCrystal mylcd(7,6,5,4,3,2);
int counter_sel=12,stopwatch_sel=11,reset1=10,stopwatch_led=9,counter_led=8;
void setup()
{
mylcd.begin(16,2);
mylcd.setCursor(0,0);
mylcd.print("Counter and ");
mylcd.print("Stopwatch");
delay(1000);
pinMode(counter_sel,INPUT);
pinMode(stopwatch_sel, INPUT);
pinMode(reset1, INPUT);
pinMode(counter_led,OUTPUT);
pinMode(stopwatch_led, OUTPUT);
}
void loop()
{ int state1=digitalRead(counter_sel);
int state2=digitalRead(stopwatch_sel);
if (state1==0) {delay(300); counter(); } // call counter function
else if (state2==0) {delay(300); stopwatch(); } // call stopwatch function
}
void counter()
{ mylcd.clear();
digitalWrite(counter_led,1);
mylcd.setCursor(0,0);
mylcd.print("Counter Mode :");
short int i=0;
while(1)
{
int rst=digitalRead(reset1);
if (rst==0) { delay(300); break;}
int state1=digitalRead(counter_sel);
if (state1==0) { i++; delay(200);}
mylcd.setCursor(0,1);
mylcd.print(i);
}
digitalWrite(counter_led,0);
}
void stopwatch()
{
mylcd.clear();
digitalWrite(stopwatch_led,1);
long int ms=millis();
byte sec=0, mins=0;
mylcd.setCursor(0,0);
mylcd.print("Stopwatch Mode : ");
while(1)
{
mylcd.setCursor(0,1);
int state1=digitalRead(reset1);
if (state1==0){delay(300); break; }
if (sec==59) {mins++; sec=0;}
if ((millis()-ms)>1000) {sec++; ms=millis(); }
mylcd.print(mins);
mylcd.setCursor(3,1);
mylcd.print(":");
mylcd.setCursor(5,1);
mylcd.print(sec);
mylcd.print(":");
//mylcd.print(millis()-ms);
}
digitalWrite(stopwatch_led,0);
}
As much as I can see, and considering what you explained above, the connections are correct. But the thing is you need to make it clear as much as you can, because due to intersecting point in LCD connection, it would be very much harder to debug and resolve the connection problem, for that you must make it neat. To learn to code this stuff there is no rocket science, just use your wisdom, and start reading books, blogs, articles on arduino ide(which is too simple too use), c programming and microcontrollers , and youtube videos are the great source to learn to code, you should have handful experience of c programming, that's all.

my while loop only take the first value even if it redefined in arduino

hello i try to make an interruption in my arduino code to execute only one time a part of it. i use 2 while loop, the first is suppose to detecte when i press a button and the second when i release it. between the two the code i want to execute only one time for one press.
but even if the condition of the while is good it don't want to stop.
my serial monitor
my code :
void loop()
{
long button = cs_4_6.capacitiveSensor(5);
while(button < 1000)
{
long button = cs_4_6.capacitiveSensor(5);
Serial.println(button);
Serial.println("not ok");
}
if(nbButton[3] == 0)
{
nbButton[counter] = 1;
counter++;
}
else
{
int nbButton[] = {0, 0, 0, 0};
}
while(button > 1000)
{
long button = cs_4_6.capacitiveSensor(5);
Serial.println(button); // print sensor output 2
Serial.println("ok");
}
delay(50);
}
You are declaring long button each time in your while loops. As you are already declaring it at the beginning of your loop there is no need to do it at any other place. The while loops are taking your first declaration, which is not changing, whereas the Serial.print takes the new one defined only in the while loops.
In your two while loops change long button = cs_4_6.capacitiveSensor(5); to button = cs_4_6.capacitiveSensor(5);
Hope it helps,

LED from Arduino board doesn't switch off using the click of a button

So I've been busy dealing with my programming homework during the afternoon, and I cannot seem to solve the issue in the code found below.
The exercise is that I need to toggle a LED by the press of a button. In my code, the LED goes on when I click the button, but it doesn't turn off when I click the button again.
int pinButton = 5;
int LED = 10;
int currentState;
int previousState;
void setup() {
Serial.begin(9600);
pinMode(pinButton, INPUT);
pinMode(LED, OUTPUT);
}
void toggleLed(){
if (previousState == 1 && currentState == 0){
digitalWrite(LED, HIGH);
Serial.println(currentState);
Serial.println(previousState);
delay(100);
} else {
digitalWrite(LED, LOW);
Serial.println(currentState);
Serial.println(previousState);
delay(100);
}
}
void loop() {
int currentState = digitalRead(pinButton);
if (currentState == 0 && previousState == 1) {
Serial.println("Knop is losgelaten");
toggleLed();
}
previousState = currentState;
}
I guess that in the first if statement the else code block doesn't make sense, because in this case previousState and currentState will always be 1 and 0 respectively.
Do you guys have any tips?
This Arduino is an Arduino Uno by the way.
I can see in your code, that you are not switching the light off again after you pressed the Button. It would have gone off if you would have put the if statement into a "while" statement.
EDIT: Expanded on most points to address the additional questions in the comments.
You have two different variables named currentState, a global one and one local to loop(). The global one is defined near the top of the program, not inside another function. In loop(), you start with this line:
int currentState = digitalRead(pinButton);
Because you've included the int there, this actually creates a second variable that happens to have the same name as the global one. The values of the two versions of currentState can change independently. Within loop(), every time you reference currentState, you'll be referencing the local copy. Everywhere else (like in toggleLed) will be referencing the global copy. If you just wanted to set the global one (which is what I assume you meant to do), then you need to drop the int:
currentState = digitalRead(pinButton);
This will change the global version of currentState without creating a new one.
You're not doing any debouncing, so you could see sporadic results depending upon the type of button. With many buttons and switches, the transition between on and off can be noisy. Debouncing filters out that noise. Edit: The delay in toggleLed() is probably sufficient to debounce, but usually debouncing is done before responding to a state change.
You should probably initialize previousState. Since it's a global, and globals have static storage duration, and it's not otherwise initialized, the system will ensure that it's initialized to 0. Explicit initialization would be more obvious to anyone who has to read the code, especially if they don't know all the rules of C as well. Also, you probably want to initialize it to LOW rather than 0. Which leads to my final point.
The return value of digitalRead() is HIGH or LOW. On the Uno, it probably doesn't matter that you use 1 or 0 instead of HIGH or LOW, but I believe it might matter if you ever port your code to certain other boards. The HIGH and LOW constants exist to hide details, but that helps only if you use them consistently. Using HIGH and LOW some of the time and 1 and 0 other times just makes it harder for people to understand your code, and it might cause portability problems if you ever want to try your code on a different board.
You could achieve that by reading the state of the pin defined as 'LED'. If it is in a HIGH state, turn it LOw and vice versa.
void toggleLed(){
// check if pin state is low, turn it on
if (digitalRead(LED) == 0)
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}

Repeat blink sketch for a specified time

I need help with an Arduino sketch , I want to repeat the blink sketch for a specified amount of time (3minutes for example) , then Stop .As we know , the loop() keeps runing forever which is not what I want . Any ideas how I can achieve this , blinking an LED for X minutes and Stopping ?
You should probably make use of some timer library. A simple (maybe naive) way to achieve what you want to do is to make use of a boolean that is set to 0 when 3 minutes has passed or simply digitalWrite the led to low when the timer has passed.
Check this link:
http://playground.arduino.cc/Code/Timer
I suggest that you use int after(long duration, callback).
Below is a (very) simple example of how you probably could do:
#include "Timer.h"
Timer t;
LED = 1;
void setup() {
int afterTime = t.after(180000, cancelLED);
}
void loop() {
t.update();
if(LED) {
//The "write HIGH" statement in your sketch here.
}
else {
//Write the led to LOW
}
}
void cancelLED() {
LED = 0;
}
I haven't used the library myself, I just checked the the docs and wrote an example to give you some ideas. Don't expect it to work right away.

Resources