Help with programing - new to arduino coding

Hi Guys,

I have a 3x button panel that uses resistors for controls and wired into my Arduino. so far I have adapted this code that I found online and I have it working with only 1 of the buttons that controls 1 of 3 leds on my breadboard.

Can anyone help me with the coding and I cant workout how to get the other 2x buttons to work on the line “// set the LED using the state of the button for testing:” and then change the code so I get one press on and one press off.

Hope this make sense.

Thanks for any help guys :smiley:

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 0;     // the number of the pushbutton pin
const int ledPin2 = 2;      // the number of the LED pin for testing
const int ledPin3 = 3;      // the number of the LED pin for testing
const int ledPin4 = 4;      // the number of the LED pin for testing

const int BUTTON1 = 1;
const int BUTTON2 = 2;
const int BUTTON3 = 3;

const int BUTTON1LOW = 350;
const int BUTTON1HIGH = 500;
const int BUTTON2LOW = 400;
const int BUTTON2HIGH = 650;
const int BUTTON3LOW = 700;
const int BUTTON3HIGH = 800;

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 100;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = analogRead(buttonPin);
  int tmpButtonState = LOW;             // the current reading from the input pin

  if (reading > BUTTON3LOW && reading < BUTTON3HIGH) {
    //Read switch 3
    tmpButtonState = BUTTON3;
  } else if (reading > BUTTON2LOW && reading < BUTTON2HIGH) {
    //Read switch 2
    tmpButtonState = BUTTON2;
  } else if (reading > BUTTON1LOW && reading < BUTTON1HIGH) {
    //Read switch 1
    tmpButtonState = BUTTON1;
  } else {
    //No button is pressed;
    tmpButtonState = LOW;
  }

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to a buttonState),  and you've waited
  // long enough since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (tmpButtonState != 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 = tmpButtonState;
    Serial.println(buttonState);
  }

  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = tmpButtonState;

  // set the LED using the state of the button for testing:
  if (buttonState == HIGH) {     
    digitalWrite(ledPin2, HIGH); //If the button input signal is high, the LED will light
  } 
  else {
    digitalWrite(ledPin2, LOW); //LED goes out
  }
}
if (buttonState == HIGH) {

HIGH is 1. You set buttonState to 1, 2, or 3 depending on which button was pressed. But this can only be true if buttonState is 1.

Maybe you mean

if (buttonState > 0) {

I think you've confused code for digital buttons with your analog ones. There shouldn't be any need to debounce these.

Delta_G:

if (buttonState == HIGH) {

HIGH is 1. You set buttonState to 1, 2, or 3 depending on which button was pressed. But this can only be true if buttonState is 1.

Maybe you mean

if (buttonState > 0) {

I think you've confused code for digital buttons with your analog ones. There shouldn't be any need to debounce these.

Mmmm, really?? like I said I don't really do coding and this worked with only a few tweaks. any help with tweeking the code or making it simpler would be great

Delta_G:

[code]if (buttonState > 0) {

I think you've confused code for digital buttons with your analog ones. There shouldn't be any need to debounce these.

If I change this to this every button lights the same led, how would I add the other buttons to the code?

Thanks for that info and I did realise (in the end) what you meant,

I have now changed the code to:

// set the LED using the state of the button for testing:
  if (buttonState == 1) {     
    digitalWrite(ledPin2, HIGH); //If the button input signal is high, the LED will light
  } 
  else {
    digitalWrite(ledPin2, LOW); //LED goes out
  }
  if (buttonState == 2) {     
    digitalWrite(ledPin3, HIGH); //If the button input signal is high, the LED will light
  } 
  else {
    digitalWrite(ledPin3, LOW); //LED goes out
  }
  if (buttonState == 3) {     
    digitalWrite(ledPin4, HIGH); //If the button input signal is high, the LED will light
  } 
  else {
    digitalWrite(ledPin4, LOW); //LED goes out
  }
}

If I use:

if (buttonState > 0) {

0 being the number of the button pressed, then the leds don’t work or random outputs on with one button press

First thing I’d do is see what you are getting from the analog read.

Insert the following after the analog read step

Serial.println(reading);
delay(500);

Then bring up the serial terminal and watch to see what values are printed as you press each of the three buttons.

Jim.

AverageGuy:
First thing I'd do is see what you are getting from the analog read.

Insert the following after the analog read step

Serial.println(reading);
delay(500);

Then bring up the serial terminal and watch to see what values are printed as you press each of the three buttons.

Jim.

Hi Jim

that is already working in this code, I see the buttons press's on the serial monitor (1,2,3)

Sorry on post 4# I should of said it working for each button now, each button pressed works a different LED. Cool Thanks for the hit Delta_G :grinning:

What do I tweak for the latching affected? one push (LED on) another push (LED off) ← both off the same button.

// set the LED using the state of the button for testing:
  if (buttonState == 1) {     
    digitalWrite(ledPin2, HIGH); //If the button input signal is high, the LED will light
  } 
  else {
    digitalWrite(ledPin2, LOW); //LED goes out
  }
  if (buttonState == 2) {     
    digitalWrite(ledPin3, HIGH); //If the button input signal is high, the LED will light
  } 
  else {
    digitalWrite(ledPin3, LOW); //LED goes out
  }
  if (buttonState == 3) {     
    digitalWrite(ledPin4, HIGH); //If the button input signal is high, the LED will light
  } 
  else {
    digitalWrite(ledPin4, LOW); //LED goes out
  }
}