Go Down

Topic: need help to modify this push button code (Read 1 time) previous topic - next topic

UKHeliBob

No ! 
The number of times the button has been pressed is not relevant.  buttonState simply tells the program whether a button is currently pressed or not.  The state that I am talking about indicates which level of the menu you are in so that button presses can be interpreted and acted on appropriately .  Button 1 will mean different things depending on the current state.  To expand on my example, if you are at the top level of the menu and press button 2 this might take you to a menu allowing you to delete fingerprints.  That might be state 3.  You decide on the state numbers.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

taz3m

May be something like this may help to see things clearer :

Code: [Select]


//definition of Variables

int State = 0


if (ButtonState==1)
    {State = 1}



Then in your code you define at State = 1, what happen depending on your requirements.

taz . .
tz
.....

robtillaart

Your code-style has too much comments imho (not to bash you, just want to learn you how to type less and more tot the point :)

Code can explain itself if you choose good names for variables, functions and constants.
Comment should tell primarly the WHY, not the HOW (that should the code do) and sometimes explain the WHAT

Some examples

Quote
delay(1500);                              // delay for 3000ms

This kind above is obfuscating at best, code will always win when there is a difference between code and comment.
A better comment could be // milliseconds as that explains the unit of delay (WHAT)

Quote
if (adc_key_in==0)        // Right button menu

is much better, but can be improved by using consts or #defines, creating self explaining code

something like this
Code: [Select]

// at the top of sketch you define
#define RIGHT_BUTTON_MENU 0
#define UP_BUTTON_MENU 1
#define DOWN_BUTTON_MENU 2
#define LEFT_BUTTON_MENU 3
#define SELECT_BUTTON_MENU 4
etc

if (adc_key_in == RIGHT_BUTTON_MENU)
....

if (adc_key_in == LEFT_BUTTON_MENU)
....


From the big block above loop() only this line explains something, the rest can be seen in the code, no need to comment
Code: [Select]

* Non-Stop looping


Give it a try!
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

mr_hacker90

thank you. thank you very much. wow. i'm very grateful to all of u. much much clear. thank you for the explanation & example provided.

mr_hacker90

by the way, is this code correct in your opinion. i manage to scroll down to every operation. add fingerprint, delete fingerprint and so on. by when i want to go to second menu, it won't go to the second menu. need advice on that. perhaps maybe something wrong with my coding.

Code: [Select]

#include <LiquidCrystal.h>
#define RIGHT_BUTTON_MENU 0
#define UP_BUTTON_MENU 1
#define DOWN_BUTTON_MENU 2
#define LEFT_BUTTON_MENU 3
#define SELECT_BUTTON_MENU 4

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int analogPin = A0;
int adc_key_old;
int NUM_KEYS = 5;
int key=-1;
int adc_key_val[5] ={30, 150, 360, 535, 760};

// Variables will change:
int adc_key_in=0;
int adc_key_in_last = 0;     // previous state of the button
int adc_key_in_PushCounter = 0;   // counter for the number of button presses

int State1 = 0;
int State2 = 0;
int State3 = 0;
int State4 = 0;
int State5 = 0;
int State6 = 0;
int State7 = 0;

void setup ()
{
  // initialize serial:
  Serial.begin(57600);
 
  lcd.begin(16, 2);                         // set the lcd dimension
  lcd.setCursor(0,0); 
  lcd.print("  CYTRON TECH.");              // display the text
  lcd.setCursor(0,1);                       // set lcd.setCursor (column,row)
  lcd.print(" Eg. LCD Shield");
  delay(1500);  // delay for 1500ms
 
  lcd.clear();
  adc_key_old = analogRead(analogPin);      // store the unpress key value
}

void loop()
{

  adc_key_in = analogRead(analogPin);     // read ADC value
  adc_key_in = get_key(adc_key_in);
 
  // compare the buttonState to its previous state
  if (adc_key_in != adc_key_in_last) {
   
    // if the state has changed, increment the counter
    if (adc_key_in == DOWN_BUTTON_MENU) {

      // if the current state is HIGH then the button
      // wend from off to on:
      adc_key_in_PushCounter++;
      delay (300);
     
      if (adc_key_in_PushCounter==1) {
        //{State1 = 1}
        lcd.clear();
        lcd.setCursor(0,0);                       // set lcd.setCursor (column,row)
        lcd.print("Add Fingerprint");
       
        if (adc_key_in == SELECT_BUTTON_MENU) {
          addFinger0;
        }
      }
     
      else if (adc_key_in_PushCounter==2){
        lcd.clear();
        lcd.setCursor(0,0);                       // set lcd.setCursor (column,row)
        lcd.print("Delete");
        lcd.setCursor(0,1);                       // set lcd.setCursor (column,row)
        lcd.print("Fingerprint");
      }
     
      else if (adc_key_in_PushCounter==3){
        lcd.clear();
        lcd.setCursor(0,0);                       // set lcd.setCursor (column,row)
        lcd.print("Search");
        lcd.setCursor(0,1);                       // set lcd.setCursor (column,row)
        lcd.print("Fingerprint");
      }

      else if (adc_key_in_PushCounter==4){
        lcd.clear();
        lcd.setCursor(0,0);                       // set lcd.setCursor (column,row)
        lcd.print("Empty Database");
      }

      else if (adc_key_in_PushCounter==5){
        lcd.clear();
        lcd.setCursor(0,0);                       // set lcd.setCursor (column,row)
        lcd.print("Search Database");
      }
    }
       
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      //lcd.setCursor(0,0);                       // set lcd.setCursor (column,row)
      //lcd.print("off");
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  adc_key_in_last = adc_key_in;
}

int get_key(unsigned int input)
{
  int k;

    for (k = 0; k < NUM_KEYS; k++)
    {
      if (input < adc_key_val[k])
      {

      return k;
      }
    }
   
  if (k >= NUM_KEYS)
    k = -1;     // No valid key pressed

    return k;
}

void addFinger0(){
  // set the cursor to column 0, line 1
  lcd.setCursor(0, 1);
  // Print a message to the LCD.
  lcd.print("Add Fingerprint1");
  delay(1500);
}

Go Up