Go Down

Topic: +++ SOLVED +++ More buttons , to show text at lcd or serialprint (Read 664 times) previous topic - next topic

ArduinoPat

Apr 21, 2012, 10:03 pm Last Edit: Apr 22, 2012, 08:44 pm by ArduinoPat Reason: 1
Hello all,

I 'am new with the arduino , but the examples in the getting started book is not the problem for me for first of this begin with the arduino,
Now i will do an experiment with more buttons and text ( display at serial or LCD )  , with one button it will works ;-)
But now i will make a sketch with more buttons,
So when i Push button one , the screen display as example button 1 is pressed
And when i push button two , the screen displays as example button 2 is pressed , and further and further,

Now i have make a simple sketch as example with one button (Door contact ) , but i like to make this sketch with more buttons:


***************************************************************************************************************************************

/*
*  Door alarm switch test
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);


int switchPin = 2;              // switch is connected to pin 2
int val;                        // variable for reading the pin status
int buttonState;                // variable to hold the last button state

void setup() {

  lcd.init();
  lcd.backlight();
    // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  pinMode(switchPin, INPUT);    // Set the switch pin as input
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}


void loop(){
      val = digitalRead(switchPin);      // read input value and store it in val

      if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {         // check if the button is NOT pressed
      lcd.clear();
      Serial.println("Door contact 1 Restored "); // put at serial
      lcd.println("  Door contact 1  "); // put text at row 1 lcd
      lcd.setCursor(0, 1); // use row  2
      lcd.print("    Restored  ");// row 2 at lcd
   
     
     
    } else {                         // the button is pressed...
      lcd.clear();
      Serial.println("Door contact 1 Activated ");// at serial
      lcd.println(" Move Detection  ");// row 1 at lcd
      lcd.setCursor(0, 1);//row  2
      lcd.print("** Activated **");//row 2 at lcd
   
     

    }
  }

  buttonState = val;                 // save the new state in our variable
}



This sketch is working correct , for now ,
I hope someone can help me further !!!

smiley-mr-green
Patrick ,The Netherlands

PaulS

Unfortunately, that is a pretty lousy example. I prefer variable names that really indicate the value that they hold.

Code: [Select]
currState = digitalRead(switchPin);
if(currState !=prevState)
{
}
prevState = currState;

Now, looking at this, it is easy to modify to deal with 2 or more switches:
Code: [Select]
currState1 = digitalRead(switchPin1);
if(currState1 !=prevState1)
{
}
prevState1 = currState1;

currState2 = digitalRead(switchPin2);
if(currState2 !=prevState2)
{
}
prevState2 = currState2;


ArduinoPat

Thank's for reply PaulS,

But it will not work yet  :0

With one button it works !!

But when i make this sketch for two buttons it will not work anymore,

look at this sketch :

/*
*  Alternating switch
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);


int switchPin = 2;                  // switch is connected to pin 2
int switchPin2 = 4;                 // switch is connected to pin 4
int prevState;                      // variable for reading the pin status
int prevState2;                     // variable for reading the pin status
int currState;                      // variable to hold the last button state
int currState2;                     // variable to hold the last button state

void setup() {
 
  lcd.init();
  lcd.backlight();
    // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
 
  pinMode(switchPin, INPUT);     // Set the switch pin as input
  pinMode(switchPin2, INPUT);    // Set the switch pin as input

}
  void loop(){
  currState = digitalRead(switchPin);
 
  if(currState !=prevState){
  if(currState == LOW){
  lcd.println("   ");
  lcd.clear();
 
  currState2 = digitalRead(switchPin2);

  if(currState2 !=prevState2){
  if(currState2 == LOW){
  lcd.println("   ");
  lcd.clear();
 
 
 
 
  }
  else {
 
  prevState = currState;
  prevState2 = currState2;
 
  currState = digitalRead(switchPin);
  if(currState !=prevState)
  lcd.print(0, 0);
  lcd.print("BUTTON-1=PRESSED    ");
 
  currState2 = digitalRead(switchPin2);
  if(currState2 !=prevState2)
  lcd.print(0, 0);
  lcd.print("BUTTON-2=PRESSED    ");
 
 
  }
  }
  prevState = currState;
  prevState2 = currState2;

}



This will not work , but with one button it works , when i push the button the LCD shows BUTTON-1= PRESSED
When i loss the button the lcd screen is empty , and that is what i will, but then with more buttons ????


This is working :

/*
*  Alternating switch
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);


int switchPin = 2;              // switch is connected to pin 2
int prevState;                     // variable for reading the pin status
int currState;                // variable to hold the last button state

void setup() {
 
  lcd.init();
  lcd.backlight();
    // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
 
  pinMode(switchPin, INPUT);    // Set the switch pin as input

}
  void loop(){
  currState = digitalRead(switchPin);
 
  if(currState !=prevState){
  if(currState == LOW){
  lcd.println("   ");
  lcd.clear();
  }
  else {
   
  prevState = currState;
  currState = digitalRead(switchPin);
  if(currState !=prevState)
  lcd.print(0, 0);
  lcd.print("BUTTON-1=PRESSED    ");
 
  }
  }
  prevState = currState;


}

Maby you can give me some information what i have to do ?

Thank's for read this !!


Patrick, The Netherlands.
Patrick ,The Netherlands

PaulS

If you open the first of those sketches, and use Tools + Auto format, you will see that you are only dealing with one switch if the other is pressed.

I don't think this meets the requirements you have defined.

You appear to want the two switches to be completely independent.

ArduinoPat

Hello PaulS,


I will use more buttons to switch over , and shows at the lcd screen at wath kind of  input the switch is ,

can you give me a complete example please ?


Thank's for reading this ,


Patrick
Patrick ,The Netherlands

PaulS

Quote
can you give me a complete example please ?

You have a complete example for one switch. Simply copy all the code in loop, and paste it after the code currently in loop, and change the names in the pasted code
Code: [Select]
void loop()
{
   // Code to deal with switch one
   {
   }

   // Code to deal with switch two
   {
   }
}


Do not paste bits of code inside if or else blocks.

ArduinoPat

#6
Apr 22, 2012, 08:14 pm Last Edit: Apr 22, 2012, 08:17 pm by ArduinoPat Reason: 1
Dear PaulS,

It's working , i have make a simple test with 3 Buttons, during pushing one button it will shows at the display
BUTTON-1=PRESSED
BUTTON-2=PRESSED
BUTTON-3=PRESSED

find attached my sketch,

/*
*  Alternating switch
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);


int switchPin = 2;              // switch is connected to pin 2
int switchPin1 = 4;              // switch is connected to pin 4
int switchPin2 = 7;              // switch is connected to pin 7
int prevState;                     // variable for reading the pin status
int currState;                // variable to hold the last button state


void setup() {
 
  lcd.init();
  lcd.backlight();
    // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
 
  pinMode(switchPin, INPUT);     // Set the switch pin as input
  pinMode(switchPin1, INPUT);    // Set the switch pin as input
  pinMode(switchPin2, INPUT);    // Set the switch pin as input

}
  void loop(){
  currState = digitalRead(switchPin);
 
  if(currState !=prevState){

  if(currState == LOW){
  lcd.println("   ");
  lcd.clear();
 
  }
   
   
  else {
   
  prevState = currState;
  currState = digitalRead(switchPin);
  if(currState !=prevState)
  lcd.print(0, 0);
  lcd.print("BUTTON-1=PRESSED    ");
 
  }
  }
  prevState = currState;

currState = digitalRead(switchPin1);
 
  if(currState !=prevState){
  if(currState == LOW){
  lcd.println("   ");
  lcd.clear();
  }
  else {
   
  prevState = currState;
  currState = digitalRead(switchPin1);
  if(currState !=prevState)
  lcd.print(0, 0);
  lcd.print("BUTTON-2=PRESSED    ");
 
  }
  }
  prevState = currState;
 
 
  currState = digitalRead(switchPin2);
 
  if(currState !=prevState){
  if(currState == LOW){
  lcd.println("   ");
  lcd.clear();
  }
  else {
   
  prevState = currState;
  currState = digitalRead(switchPin2);
  if(currState !=prevState)
  lcd.print(0, 0);
  lcd.print("BUTTON-3=PRESSED    ");
 
  }
  }
  prevState = currState;
  }


Find the example at youtube , $)


Many thank's for your explain,

Best regards ,

Patrick , The Netherlands




Patrick ,The Netherlands

Go Up