Anyone here have some knowledge of C code?

markba633csi

Mark Silva
H-M Supporter Gold Member
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
{
 
Here is what this code does:
- it is reading integer values from the sensor
- the first do loop keeps looping until it sees a value less than or equal to 1000
- then it sets the display cursor at 9,0 position and displays whatever value is read from the sensor
- then it waits a small delay
- then it returns one of four values: RIGHT, UP, DOWN, LEFT or SELECT
- seems like this is a dial sensor and it returns a range of values based on its position, e.g.
if sensor is 0-49 return RIGHT, if 50-194 return UP, if 195-379 return DOWN, if 380-554 return LEFT, if 555-789 return SELECT
 
Ah I see, it would be normally a pot but in this case it's pushbuttons all in parallel (I presume)with each one giving a different voltage or resistance value to the adc to represent the 5 different modes. I'll have to wait till the hardware gets here to see exactly how it's done.
Thanks dr!
Mark
 
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
{

That looks like code for an embedded processor, probably an arduino of some sort. Drcoelho is right on with the logic.
 
It is Arduino, written some time ago by a guy named Chuck Fellows, on another forum; homemade 4th axis/stepper driven dividing head
Mark
 
Back
Top