• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
  • Guest,  We want to wish You and Your Family a Healthy, Happy Thanksgiving! Click the "X" at the top right corner to remove this notice)
  • PLEASE: Read the FORUM RULES BEFORE registering!

4

Anyone here have some knowledge of C code?

3
Like what you see?
Click here to donate to this forum and upgrade your account!
10

markba633csi

Active Member
Active Member
Joined
Apr 30, 2015
Messages
1,328
Likes
542
#1
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
{
 

drcoelho

H-M Supporter - Premium Member
H-M Supporter-Premium Member
Joined
Feb 28, 2017
Messages
7
Likes
3
#2
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
 

markba633csi

Active Member
Active Member
Joined
Apr 30, 2015
Messages
1,328
Likes
542
#3
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
 

silverhawk

Active Member
Active Member
Joined
Dec 6, 2015
Messages
152
Likes
113
#4
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.
 

markba633csi

Active Member
Active Member
Joined
Apr 30, 2015
Messages
1,328
Likes
542
#5
It is Arduino, written some time ago by a guy named Chuck Fellows, on another forum; homemade 4th axis/stepper driven dividing head
Mark
 
[6]
5 [7]