Pages: [1]   Go Down
Author Topic: Potentiomer as alternative to multiple switches on an analog input.  (Read 520 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm new to this Arduino thing...so forgive me if there's anything wrong with the code or my concept.  Any suggestions or alternate ways to do the same thing would be great.

I'm working on a project that will need to read and store a number of values ( as integers -- milliseconds from 0 to 1000 or so) to be used in a photo delay trigger.  I decided in needed 10 values for the various delays so I was simply going to have a bunch of pots to allow me to dial in the values I wanted, read these as the sketch ran, and use them in the delay loops.  So I realized I'd use up all those ports which I didn't really want to do.

In my testing of this POT vs switches idea, I've found it to be much easier than having to deal with the odd-ball values that the switch/voltage divider schemes come up with.  It's just a nice clean 0-10 set of values.

1.  I want to have a pot let me set the delay value .. from 0-1023
2.  I want to store the values in Eprom once they are selected.

I've set up a 10k pot from +5v to gnd, and the wiper on an analog pin.
and a second 10k pot  from +5v to gnd, and the wiper on another analog pin.
There's also a 'save setting' SP/NO pushbutton switch connected to a digital port.

When the menu select pot is at '0' (basically grounded) the sketch decides that is not in 'programming mode'. As soon as the pot moves out of 0 ohms the program will start reading it and decided at what point the wiper is at giving values from 0 to 1023.

(code tags added by moderator)
Code:
int menuSelectPot = 0;       // pot wiper connected to analog pin 0

int valueSelectPot = 0;       // pot wiper connected to analog pin 1

int saveSettingSwitch = 8;  // switch to 'capture' the current setting if the
                                        // menu is on.

int myMenu=0;                  // value to store which menu is currently selected

int settingValue = 0;          // variable to store the read value

void setup()
{
  pinMode(menuSelectPot, INPUT);  
  pinMode(valueSelectPot, INPUT);  
  pinMode(saveSettingSwitch, INPUT);  
  readSettings();
}

void readSettings()
{
// for now this is just a dummy function
// it will read the 10 stored settings
// from eProm and put them in an array
// and store them as integers in an array of 10 positions
}

void loop()
{

  //read the pot -- values will be from 0 to 1023
  //convert to values from 0 to 10 simply by dividing by 100
  myMenu = analogRead(menuSelectPot)/100;  

  //display the results on the lcd...
  doMenuDisplay(myMenu);

}

void doMenuDisplay(int menu)
{
  if (menu > 0 )
  {
    lcd.print("Menu is ON: ");
    delay(1000);

    // ... at this point 2 things will happen...
    //show the value on the screen
    settingValue = analogRead(menuValuePot);  
    lcd.Print('Menu #');
    lcd.Print(myMenu);
    lcd.Print("   Value:");
    lcd.Println(settingValue);

    //check to see if we have pushed the save setting button
    
    if (digitalRead(saveSettingSwitch)==HIGH)
    {
    // save the value to an eprom location
    //
    //  not coded yet ....
    //
    delay(500);   // just to give us time to insure the setting is saved and our
                        // finger is off the save button.
    // will probably put a status message on the lcd to confirm save of the setting
    delay(1500);

    }
  }
  else
  {
    lcd.Println('Menu OFF');

  }
  

}

« Last Edit: August 14, 2012, 03:04:43 pm by CrossRoads » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 525
Posts: 26554
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Could also just go with a mechanical rotary encoder with detents. Uses 2 inputs also.
Example
http://www.digikey.com/product-detail/en/ACZ16NBR1E-15FD1-24C/102-1785-ND/1923380

Lot more solid feel for positions.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Jr. Member
**
Karma: 1
Posts: 97
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would go with a button or two and the potentiometer.
With a long press go to your menu (menu=1) then longpresses save your set values from the potentiometer.So you dont have to build delays to set the pot, just let it scan as long as youre in menu for example and the buttons sets the right time when to save.. short presses switch between your values, for that just a little variable that counts and is coded to write to the right position.
And after ten there is automatically menue exit or waiting for a long pressed button else it will start with first value.

If you use 2 or more buttons you can build a nice menue with <back>|menu|<next> buttons for example the gets really user friendly smiley

If you already got this idea, than its fine, if havent looked to deep to your code yet smiley
Just thought how i would get your idea done smiley
Logged

Pages: [1]   Go Up
Jump to: