Multipul buttons???

im getting my self a little confused.

ive got an lcd screen and a few buttons, i want the lcd to display something different depending on the button pressed. i know how to write to the lcd and how to use the clear function to wipe it.

this is what my code currently looks like.

/*
 * Mathew Buer - 18/05/2015 @ 12:00

  The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
 */


// this constant won't change:
const int  buttonPin = 7;    // the pin that the pushbutton is attached to
const int ledPin = 8;       // 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
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 

void setup() {
  // set up the LCD's number of columns and rows:
  // tell the lcd where were startubg
  lcd.begin(16, 2); 
  // set the line of the lcd we wish to print to
  lcd.setCursor(0,0); 
  // message were going to print.
  lcd.print("  Lessage Line 1"); 
  // set the lcd to line 2
  lcd.setCursor(0,1); 
  // set the message
  lcd.print("Message Line 2"); 
  // delay for showing the message
  delay(5000);
  lcd.clear();
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT_PULLUP);
  // 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 % 2 == 0) {
  // set the line of the lcd we wish to print to
  lcd.clear();
  lcd.setCursor(0,0); 
  // message were going to print.
  lcd.print("BUTTON PRESSED"); 
  }
 else
  lcd.clear();
  lcd.print("TEST");
}

how in order to use button2 to print "Hello World" woudli need to set new varibles for the button state ? ie

int buttonPushCounter2 = 0;   // counter for the number of button presses
int buttonState2 = 0;         // current state of the button
int lastButtonState2 = 0;     // previous state of the button

or is there an eaiser way as this just seems long winded.

Regards

or is there an eaiser way as this just seems long winded.

That's what you need to do. If you have more than 2 switches, arrays become useful.

at the moment im just playing around with code trying to learn it, i find i learn stuff quicker if i fiddle,

basically at the moment im playing with printing to a lcd,

the the idea was

button 1 will display (this is button 1)
button 2 will display (this is button 2)
button 3 will display (this is button 3)
button 4 will display (this is button 4)

obvs ill use the lcd.clear(); function else the lcd will fill up with crap.

i dont get arrays, would you provide a sample of an array that i could use so i can see how it works and try to understand it,

Regards

i dont get arrays, would you provide a sample of an array that i could use so i can see how it works and try to understand it,

byte switchPins[] = {7, 8, 9};

byte currState[3];
byte prevState[3];

void loop()
{
   for(byte i=0; i<3; i++)
   {
       currState[i] = digitalRead(switchPin[i]);
       if(currState[i] != prevState[i])
       {
          if(currState[i] == HIGH)
          {
             // The switch attached to the ith pin just became HIGH
          }
       }
       prevState[i] = currState[i];
   }
}

thanks for the example,

am i right in thinking that the you would give each switch a pin, ie

int switchPins[] = {7, 8, 9};
byte currState[3];
byte prevState[3];

void setup () }
// set all buttons as inputs

  pinMode(switchPins, INPUT);

void loop()
{
   for(byte i=0; i<3; i++)
   {
       currState[i] = digitalRead(switchPin[i]);
       if(currState[i] != prevState[i])
       {
          if(currState[i] == HIGH)
          {
             lcd.setCursor(0,1); 
             lcd.print("BUTTON 1")
             lcd.print("BUTTON 2")
             lcd.print("BUTTON 3")
          }
       }
       prevState[i] = currState[i];
   }
}

or am i totally wrong ? im new to coding so just trying to find my feet, ive read a few tutorials about arrays but tbh they confused the hell outta me lol..

am i right in thinking that the you would give each switch a pin

Yes. The values in the {} are the pin numbers.

This

void setup () }
// set all buttons as inputs

  pinMode(switchPins, INPUT);

needs to be

void setup ()
{
   // set all buttons as inputs
   for(byte b=0; b<3; b++)
  {
     pinMode(switchPins, INPUT);
  }
}

Hi,

const byte switchPins[] = {7, 8, 9}; // int is way larger than byte , so use byte for your switchpins

// now you have a byte array of three pins ( pin 7, 8, and 9)

// const just means  these settings will NEVER change 

// your array starts with zero (0,1,2,)  so switchPins[0] = your pin 7


byte currState[3];
byte prevState[3];

void setup () {  // open {  first

 for(byte j=0; j<3;j++)  // so if j =0 and j is less than 3, which means 0, 1,2    j = j +1 means J++



 { pinMode(switchPins[j], INPUT_PULLUP); }// now you use the byte array of switchPins

// inside the for loop       it means  you can set

//pinMode(switchPins[0], INPUT_PULLUP)   == pin7 is now set to INPUT_PULLUP
//pinMode(switchPins[1], INPUT_PULLUP)   == pin8 is  now set to INPUT_PULLUP
//pinMode(switchPins[2], INPUT_PULLUP)   == pin9 is now set to INPUT_PULLUP


} //  and then end it with }

@siutoejai

thanks a million for the breakdown of what everything dose. this has helpped me unstand what everything is doing :smiley:

  • karma for you Dude :smiley:

@PaulS

thanks for the inital replys and help, + karma for you also.

this is what really makes a forum, Dedicated members,

im a avid user of the opencart forums and i try to help as much as i can there, and ill do the same here, once ive learned C++ ill be more then happy to help others achieve what there after :smiley:

Hi, pal.

You are welcome. :slight_smile: