1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. PLEASE: Read the FORUM RULES BEFORE registering!

    Dismiss Notice

Anyone here have some knowledge of C code?

Discussion in 'CNC IN THE HOME SHOP' started by markba633csi, Jul 8, 2017.

  1. markba633csi

    markba633csi United States Active Member Active Member

    Likes Received:
    402
    Trophy Points:
    83
    City:
    Mountain View
    State:
    California

    -Return to Top-

    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
    {
     
  2. drcoelho

    drcoelho United States H-M Supporter - Premium Content H-M Supporter-Premium

    Likes Received:
    3
    Trophy Points:
    3
    City:
    los altos
    State:
    California

    -Return to Top-

    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
     
    dlane, silverhawk and markba633csi like this.
  3. markba633csi

    markba633csi United States Active Member Active Member

    Likes Received:
    402
    Trophy Points:
    83
    City:
    Mountain View
    State:
    California

    -Return to Top-

    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
     
  4. silverhawk

    silverhawk United States Active Member Active Member

    Likes Received:
    96
    Trophy Points:
    28
    City:
    SLC
    State:
    Utah

    -Return to Top-

    That looks like code for an embedded processor, probably an arduino of some sort. Drcoelho is right on with the logic.
     
  5. markba633csi

    markba633csi United States Active Member Active Member

    Likes Received:
    402
    Trophy Points:
    83
    City:
    Mountain View
    State:
    California

    -Return to Top-

    It is Arduino, written some time ago by a guy named Chuck Fellows, on another forum; homemade 4th axis/stepper driven dividing head
    Mark
     

Share This Page