- Joined
- Apr 30, 2015
- Messages
- 12,251
Specifically Arduino code. I downloaded a sketch for a digital stepper motor dividing head and there is a routine for servicing the pushbuttons I don't follow. Trying to understand it before I have a board in hand.
Mark S.
ps I think "valies" should read "values" but I'm wondering if these values are analog voltages to adc inputs? Maybe that's what he had to do if he didn't have enough digital inputs for the 5 or 6 pushbuttons...?
/********************* read the buttons **************************/
int read_LCD_buttons()
{
adc_key_in = 0;
do
{
adc_key_in = analogRead(0); // read the value from the sensor
} while( adc_key_in > 1000 );
lcd.setCursor( 9,0 );
lcd.print( " ");
lcd.setCursor( 9,0 );
lcd.print( adc_key_in);
delay(200);
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnERR; // when all others fail, return this...
}
/********************* Set Up ************************************/
void setup()
{
lcd.begin(16, 2); // start the library
do
{
Mark S.
ps I think "valies" should read "values" but I'm wondering if these values are analog voltages to adc inputs? Maybe that's what he had to do if he didn't have enough digital inputs for the 5 or 6 pushbuttons...?
/********************* read the buttons **************************/
int read_LCD_buttons()
{
adc_key_in = 0;
do
{
adc_key_in = analogRead(0); // read the value from the sensor
} while( adc_key_in > 1000 );
lcd.setCursor( 9,0 );
lcd.print( " ");
lcd.setCursor( 9,0 );
lcd.print( adc_key_in);
delay(200);
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnERR; // when all others fail, return this...
}
/********************* Set Up ************************************/
void setup()
{
lcd.begin(16, 2); // start the library
do
{