Button how to?

Hello everyone,

i'm working on a little project where i have two buttons. By pressing those button i should increase or decrese some values.

Now the connections seems to be wired fine, but the buttons doesn't seems to work always.

I connected the two button to two digital pin on arduino (inpud mode).

I read the pin value using digitalRead.

Looking at the serial monitor it seems that arduino lock up for a while, then become active again.

Where is the problem?
There are some special operations that i should do?

(deleted)

Please post your code and a schematic of the switch wiring. A common mistake is to let the switch pin float when the switch is open. That is remedied by pulling the switch to ground or Vcc with a pulldown or pullup resistor.

Preferred momentary switch wiring (pullup):

Enable the internal pullup with pinMode(pin, INPUT_PULLUP) and R1 is unnecessary.

i followed this tutorial

https://www.arduino.cc/en/tutorial/pushbutton

i connected the button as in the previous tutorial.

i also tried this tutorials

https://www.arduino.cc/en/tutorial/button

here's the relevant part of the code i used

while(digitalRead(inPinUp)==HIGH || digitalRead(inPinDown)==HIGH){};
delay(100);
if (digitalRead(inPinUp)==HIGH){
  power += 10;
} else{
  power=power;
}
if (digitalRead(inPinDown)==HIGH){
  power -= 10;
} else{
  power=power;
}

With the tutorial code, or your own?

Hi,
Please post the entire code, relevant bits are not necessary all the relevant bits.

Thanks.. Tom.. :slight_smile:

You did something wrong in your code, maybe a misplaced semicolon or something else.

Try to understand how to debounce switches without using delay(), as demonstrated in the StateChangeDetection example. Then learn how to use that code pattern with two (or more) switches, and make one increment the power, and the other one decrement the power.

I've written yet another library for handling buttons, which may simplify the coding to you. But I think that everybody should understand the basic coding patterns first, and use libraries only for the sake of short and easily readable source code.

aarg:
With the tutorial code, or your own?

Hi, i found the code above in this very forum (Button function: click, double click, more... - Interfacing - Arduino Forum)

In the code posted you have an empty while loop and trap the execution there if either pin reads high. The only way execution can proceed to the two if statements is if both pins are low. So I don't think that's going to work out.

DrDiettrich:
You did something wrong in your code, maybe a misplaced semicolon or something else.

Try to understand how to debounce switches without using delay(), as demonstrated in the StateChangeDetection example. Then learn how to use that code pattern with two (or more) switches, and make one increment the power, and the other one decrement the power.

I've written yet another library for handling buttons, which may simplify the coding to you. But I think that everybody should understand the basic coding patterns first, and use libraries only for the sake of short and easily readable source code.

Thank you for linking your library.

I don't understand the button example, tho. Could you please post alittle example commenting the code?

What AButton button(4) mean? Button in the pin 4?

Delta_G:
In the code posted you have an empty while loop and trap the execution there if either pin reads high. The only way execution can proceed to the two if statements is if both pins are low. So I don't think that's going to work out.

Yes, i removed this. Trying other code, but still don't work

Ok. When you're ready to post that we can help.

AmigaBlitter,
Several times, responders have requested that you post your code.
You have not done so.
Please post your code. Not a link to a tutorial out on the web, your code. Copied from your development environment.
Hint, hit Ctrl T to autoformat the code before you copy.
Hint 2, past using the code tags.

Here's the wires


image hosting

The green wire goes to pin 7, while the yellow to pin 6.

(deleted)

I really tried the code i the forum i linked

#include <LiquidCrystal.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 10

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
// RS EN D4 D5 D6 D7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// this constant won't change:
const int  inPinUp = 7;    // the pin that the pushbutton is attached to
const int  inPinDown = 6;    // the pin that the pushbutton is attached to
int val=0;
int SensorPin=10;
OneWire ds(SensorPin);

int power = 50; //Power level fro 0 to 99%
//int fan1=8; //digital pin 8


void setup(){
Serial.begin(9600);

//pinMode(8,OUTPUT);
pinMode(inPinUp, INPUT);
pinMode(inPinDown, INPUT);
// Start up the library
sensors.begin(); // IC Default 9 bit. 
// Lcd setup
lcd.begin(16, 2);
}


void loop(){
char option;

val = digitalRead(inPinUp);  // read input value
  if (val == HIGH) {         // check if the input is HIGH (button released)
    power=power;
  } else {
    power +=10;
  }
  
if(Serial.available() > 0)
{
option = Serial.read();
if(option == 'a') 
power += 5;
else if(option == 'z')
power -= 5;

if(power > 99) power = 99;
if(power < 0) power = 0;

}
sensors.requestTemperatures(); // Send the command to get temperatures
//lcd.clear();
  //lcd.setCursor(0, 0); // bottom left
  //lcd.print("Temperatura di: ");
  //lcd.setCursor(0, 1); // bottom left
  //lcd.print (sensors.getTempCByIndex(0));
  //lcd.print (" C"); 
//Serial.println("DONE");
 
Serial.print("Temperature for Device 1 is: ");
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
Serial.print("Power=");
Serial.print(power);
Serial.print(" ");
}

Hi,
Do you have a DMM to take some voltage measurements?

Thanks... Tom.... :slight_smile:

Give this a try:

Button_wires_2.jpg

Thank you LarryD. I tried your configuration too, but doesn't seems to work.

Trying this simple code:

val=digitalRad(inPinUp); the button that should increase prower

if (val==LOW) { //check if the button is pressed (LOW)
power +=10;
} else [
power=power;
}

[code]

this code doesn't work, the power continue to increase each cycle, without pressing any button

Hi,
Can you measure the voltages at A, B and C with reapect to gnd with the button pressed and not pressed please?
Button_wires_2abc.jpg
Thanks.. Tom.. :slight_smile:

Button_wires_2abc.jpg