Pages: [1]   Go Down
Author Topic: Trying to use bounce library to read multiple buttons as a function  (Read 831 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, I'm trying to use the bounce library and have a separate function to be called to read button presses. 

My idea for the menu was to use case/switch statements to go from one part of the menu to the next using ok button to go to next menu state. This is my full sketch for this part. I'm using seperate sketches for different parts of my whole project then I'll add them together.  I didn't add all the states. You'll see in code. I can't get past the first part so haven't coded past that.  I wait too long to post then I'm in a rush to leave. I'm sorry if this isn't very clear.

Code:
#include <Bounce.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
int backlight = 13;


#define upSwitchPin 22
#define downSwitchPin 24
#define leftSwitchPin  26
#define rightSwitchPin 28
#define editSwitchPin 30
#define stopSwitchPin 32
#define okSwitchPin 34
byte buttonPressed;

Bounce upSwitchBounced = Bounce(upSwitchPin, 5);
Bounce downSwitchBounced = Bounce(downSwitchPin, 5);
Bounce leftSwitchBounced = Bounce(leftSwitchPin, 5);
Bounce rightSwitchBounced = Bounce(rightSwitchPin, 5);
Bounce editSwitchBounced = Bounce(editSwitchPin, 5);
Bounce stopSwitchBounced = Bounce(stopSwitchPin, 5);
Bounce okSwitchBounced = Bounce(okSwitchPin, 5);

byte currentProfile = 1;
byte profileSteps = 3;
byte profileEditStep = 1;

double rampRateStep1 = 1;
double rampRateStep2 = 1;
double rampRateStep3 = 1;
double rampRateStep4 = 1;
double rampRateStep5 = 1;
double rampRateStep6 = 1;
double rampRateStep7 = 1;
double rampRateStep8 = 1;
double rampRateStep9 = 1;

int dwellTimerStep1 = 30;
int dwellTimerStep2 = 30;
int dwellTimerStep3 = 30;
int dwellTimerStep4 = 30;
int dwellTimerStep5 = 30;
int dwellTimerStep6 = 30;
int dwellTimerStep7 = 30;
int dwellTimerStep8 = 30;
int dwellTimerStep9 = 30;

int temperatureStep1 = 120;
int temperatureStep2 = 180;
int temperatureStep3 = 225;
int temperatureStep4 = 150;
int temperatureStep5 = 130;
int temperatureStep6 = 150;
int temperatureStep7 = 170;
int temperatureStep8 = 190;
int temperatureStep9 = 220;
int Setpoint2 = 200;
typedef enum REFLOW_STATE
{
  REFLOW_STATE_IDLE,
  REFLOW_STATE_EDIT_MENU_BOTTOM_HEAT,
  REFLOW_STATE_EDIT_MENU_STEP1,
  REFLOW_STATE_EDIT_MENU_STEP1_RAMP,
  REFLOW_STATE_EDIT_MENU_STEP1_TARGET,
  REFLOW_STATE_EDIT_MENU_STEP1_DWELL,
  REFLOW_STATE_EDIT_MENU_STEP2_RAMP,
  REFLOW_STATE_EDIT_MENU_STEP2_TARGET,
  REFLOW_STATE_EDIT_MENU_STEP2_DWELL,
  REFLOW_STATE_EDIT_MENU_STEP3_RAMP,
  REFLOW_STATE_EDIT_MENU_STEP3_TARGET,
  REFLOW_STATE_EDIT_MENU_STEP3_DWELL,
  REFLOW_STATE_EDIT_MENU_STEP4_RAMP,
  REFLOW_STATE_EDIT_MENU_STEP4_TARGET,
  REFLOW_STATE_EDIT_MENU_STEP4_DWELL,
  REFLOW_STATE_EDIT_MENU_STEP5_RAMP,
  REFLOW_STATE_EDIT_MENU_STEP5_TARGET,
  REFLOW_STATE_EDIT_MENU_STEP5_DWELL,
  REFLOW_STATE_EDIT_MENU_STEP6_RAMP,
  REFLOW_STATE_EDIT_MENU_STEP6_TARGET,
  REFLOW_STATE_EDIT_MENU_STEP6_DWELL,
  REFLOW_STATE_EDIT_MENU_STEP7_RAMP,
  REFLOW_STATE_EDIT_MENU_STEP7_TARGET,
  REFLOW_STATE_EDIT_MENU_STEP7_DWELL,
  REFLOW_STATE_EDIT_MENU_STEP8_RAMP,
  REFLOW_STATE_EDIT_MENU_STEP8_TARGET,
  REFLOW_STATE_EDIT_MENU_STEP8_DWELL,
  REFLOW_STATE_EDIT_MENU_STEP9_RAMP,
  REFLOW_STATE_EDIT_MENU_STEP9_TARGET,
  REFLOW_STATE_EDIT_MENU_STEP9_DWELL,
  REFLOW_STATE_EDIT_MENU_PEDIT,
  REFLOW_STATE_EDIT_MENU_IEDIT,
  REFLOW_STATE_EDIT_MENU_DEDIT,
 
 
 
} reflowState_t;

reflowState_t reflowState;

void buttonState(){
while ( upSwitchBounced.update()) {  //need to read all buttons here
if (upSwitchBounced.read() == HIGH);
  buttonPressed = 1; // variable to hold button pressed. How else could I do this?
  if (downSwitchBounced.read() == HIGH);
  buttonPressed = 2;
  //add rest of buttons code here
}
}
 

void setup() {
 pinMode(upSwitchPin, INPUT);
pinMode (downSwitchPin, INPUT);
pinMode (leftSwitchPin, INPUT);
pinMode (rightSwitchPin, INPUT);
pinMode (editSwitchPin, INPUT);
pinMode (stopSwitchPin, INPUT); 
pinMode (okSwitchPin, INPUT);
  lcd.begin(20, 4);
  pinMode(backlight, OUTPUT);
  digitalWrite(backlight, HIGH);
  lcd.clear();
  lcd.setCursor(8, 0);
  lcd.print("IDLE");
 
}

void loop() {
 
 
 reflowState = REFLOW_STATE_IDLE;


switch (reflowState)
{
 
case REFLOW_STATE_IDLE:
{
 
  lcd.setCursor(8, 0);
  lcd.print("IDLE");
 buttonState();
 if (buttonPressed == 1); {//not working goes straight to next state without button push
 reflowState = REFLOW_STATE_EDIT_MENU_BOTTOM_HEAT; 

}
   
  break;
}
  case REFLOW_STATE_EDIT_MENU_BOTTOM_HEAT:
{
  lcd.setCursor(8, 0);
  lcd.print("    ");
  lcd.setCursor(3, 0);
  lcd.print("Profile ");
  lcd.print(currentProfile);
  lcd.print(" Edit");
  lcd.setCursor(2, 2);
  lcd.print("Bottom Heat: ");
  lcd.print(Setpoint2);   
 
  delay(200);
  reflowState = REFLOW_STATE_EDIT_MENU_STEP1_RAMP;
}
}

}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49386
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
double rampRateStep1 = 1;
double rampRateStep2 = 1;
double rampRateStep3 = 1;
double rampRateStep4 = 1;
double rampRateStep5 = 1;
double rampRateStep6 = 1;
double rampRateStep7 = 1;
double rampRateStep8 = 1;
double rampRateStep9 = 1;
I like arrays. Apparently, you don't. Makes life a lot more difficult that in needs to be.

Code:
while ( upSwitchBounced.update()) {  //need to read all buttons here
if (upSwitchBounced.read() == HIGH);
  buttonPressed = 1; // variable to hold button pressed. How else could I do this?
  if (downSwitchBounced.read() == HIGH);
  buttonPressed = 2;
  //add rest of buttons code here
}
Is upSwitchBounced.update() ever going to return false? Why is this done using a while statement?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm an absolute beginner is the problem. Thanks for the tip on arrays.
I was in a rush and posted wrong code actually...I have 3 different versions saved.  What I'm trying to do is create a function that will read which button is pressed and maybe save a corresponding number in a variable to use in the current menu state. I don't know if that's a good way to do it but it seems logical to me.

I borrowed that while loop from somewhere.on web.
 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49386
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What I'm trying to do is create a function that will read which button is pressed and maybe save a corresponding number in a variable to use in the current menu state. I don't know if that's a good way to do it but it seems logical to me.
It is. But, arrays would help a lot. You could manage all the switches in a for loop. You can create an array of Bounce objects, too.
Logged

Greenville, IL
Offline Offline
Edison Member
*
Karma: 15
Posts: 1330
Warning Novice on board! 0 to 1 chance of errors!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


 Directly from the examples provided with the IDE under the category "digital".

Check it out and ask for more help if you need it.

Code:
/*
  State change detection (edge detection)
 
 Often, you don't need to know the state of a digital input all the time,
 but you just need to know when the input changes from one state to another.
 For example, you want to know when a button goes from OFF to ON.  This is called
 state change detection, or edge detection.
 
 This example shows how to detect when a button or button changes from off to on
 and on to off.
 
 The circuit:
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 * LED attached from pin 13 to ground (or use the built-in LED on
   most Arduino boards)
 
 created  27 Sep 2005
 modified 30 Aug 2011
 by Tom Igoe

This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/ButtonStateChange
 
 */

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    }
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off");
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastButtonState = buttonState;

 
  // turns on the LED every four button pushes by
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
 
}



Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow, you're quick! Thanks a lot I'll try my best and report back with fixed code.
Logged

Pages: [1]   Go Up
Jump to: