0

I am trying to convert this code into something useable with a potentiometer and push button because I don't have a lcd shield and dont feel like wiring that many buttons. I want the pot to have options 1-4 depending on resistance and the push button will select. However I don't understand the code so I was wondering if anyone new a good way to convert it. Here is part of the code that I need to change:

    #define BUTTON_ADC_PIN           A0  // A0 is the button ADC input
#define LCD_BACKLIGHT_PIN         3  // D3 controls LCD backlight
// ADC readings expected for the 5 buttons on the ADC input
#define RIGHT_10BIT_ADC           0  // right
#define UP_10BIT_ADC            145  // up
#define DOWN_10BIT_ADC          329  // down
#define LEFT_10BIT_ADC          505  // left
#define SELECT_10BIT_ADC        741  // right
#define BUTTONHYSTERESIS         10  // hysteresis for valid button sensing window
//return values for ReadButtons()
#define BUTTON_NONE               0  // 
#define BUTTON_RIGHT              1  // 
#define BUTTON_UP                 2  // 
#define BUTTON_DOWN               3  // 
#define BUTTON_LEFT               4  // 
#define BUTTON_SELECT             5  // 
1

I think what may be most helpful is an explanation of some tools which would serve you well on this. In order to use the potentiometer to select different options, a simple solution would be along these lines:

  1. Read the potentiometer value.
  2. Check which range the value fits in.
  3. Perform a corresponding action.

Here is one idea for accomplishing this:

Read the potentiometer value:

int reading = analogRead(A0);

Check which range the value fits in.

if(reading < 1023/4){
    //Action 1
}else if(reading < (1023/4)*2){
    //Action 2
}else if(reading < (1023/4)*3){
    //Action 3
}else{
    //Action 4
}

Perform a corresponding action.

//Do something cool.
  • Yea, I know how to do that but I don't understand the code they are using for the buttons so I don't know how to integrate it. – NULL Apr 22 '15 at 15:36
  • If you look at the LCD shield schematic, you'll see that instead of four buttons using four inputs on the Arduino, they are all connected together with resistors and just one analog pin on the Arduino. Pressing different buttons provides different voltages to the Arduino analog pin... exactly the same as you! It turns out that very little code needs changing! – CharlieHanson Jun 21 '15 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.