Sorry, I've been off the forums for a few days.
Let's see, two buttons, a potentiometer, and saving the settings...
You can make two local Long
variables to store when a button is first pressed. The button would wait a certain period of time before entering the subroutine (which wouldn't actually need to be a function). Some boolean values could be used to control when settings are saved and when settings should be read from the stored values...
I've attached some code that verifies OK, but I haven't actually tested whatsoever. I think it would run pretty well though, although it would be great if others would give it a glance. The code doesn't have to block whatsoever waiting on the buttons to be held down long enough.
The pin value constants are all set to 0, so that would need to be changed. The EEPROM locations would also need to be set according to your needs (not sure if you're storing anything else). Also there is no software debounce for the buttons, hardware debounce would be preferable or adding a "readPin(int pin)" type of method that could handle a software debounce.
The code should allow for a button to be held for five seconds, the value of the potentiometer adjusting the value for the position, and then having that value saved to EEPROM when the button is pressed again. An LED will glow indicating that a position is being adjusted, and then turn off when the position is stored.
I've tried to comment a bunch to explain things, but feel free to correct or ask about it!
EDIT: It was really annoying downloading the attachment so I included the code in the post
#include <EEPROM.h>
//These variables store the positions for open and closed
int positionOpen;
int positionClosed;
//These variables store the locations in EEPROM where the saved positions are stored
const int positionOpenLocation = 0;
const int positionClosedLocation = 0;
//These constants just store the pin values
const int setOpenValuePin = 0;
const int setClosedValuePin = 0;
const int potPin = 0;
const int LEDPin = 0;
//These values are used to store a button press and to check and see if it's still pressed for the required time
long buttonOpenTime = 0;
long buttonClosedTime = 0;
//These booleans keep track of when a button has been pressed, time stored, and we haven't registerred a low yet
boolean buttonOpenPressed = false;
boolean buttonClosedPressed = false;
//These booleans control what parts of the loop execute, ensuring we're not setting both at the same time, etc
boolean settingOpen = false;
boolean settingClosed = false;
//How long you want to have to hold the buttons down, in ms
const long buttonHoldTime = 5000;
void setup(){
pinMode(LEDPin, OUTPUT);
//Read the stored values from memory, allows values to persist after power failure or reset
//You only want these next two lines after the values have been saved at least once
//or else your sevo might go to some random location
positionOpen = EEPROM.read(positionOpenLocation);
positionClosed = EEPROM.read(positionClosedLocation);
}
void loop(){
//If you're not currently setting the closed value, and the setOpen value is at HIGH
if(!settingClosed && digitalRead(setOpenValuePin) == HIGH){
//if this is the first read of a high set the buttonOpenPressed to true and record the time
if(!buttonOpenPressed){
buttonOpenPressed = true;
buttonOpenTime = millis();
//if the button's been pressed and you've been held down longer than the buttonHoldTime, set settingOpen to true
}else if(buttonOpenPressed && millis() - buttonOpenTime > buttonHoldTime){
settingOpen = true;
buttonOpenPressed = false;
digitalWrite(LEDPin, HIGH);
}
}
//if you've previously registered a press, but you now read low
if(buttonOpenPressed && digitalRead(setOpenValuePin) == LOW) buttonOpenPressed = false;
//if you're setting the open value and you've pressed the button to save it
if(settingOpen && digitalRead(setOpenValuePin == HIGH)){
digitalWrite(LEDPin, LOW);
EEPROM.write(positionOpenLocation, positionOpen);
settingOpen = false;
}
//if you're setting the open position
if(settingOpen){
//Read the potentiometer position and store it, dividing by 4 because of the EEPROM's storage size
positionOpen = analogRead(potPin) / 4;
}
if(!settingOpen && digitalRead(setClosedValuePin) == HIGH){
//if this is the first read of a high set the buttonClosedPressed to true and record the time
if(!buttonClosedPressed){
buttonClosedPressed = true;
buttonClosedTime = millis();
//if the button's been pressed and you've been held down longer than the buttonHoldTime, set settingClosed to true
}else if(buttonClosedPressed && millis() - buttonClosedTime > buttonHoldTime){
settingClosed = true;
buttonClosedPressed = false;
digitalWrite(LEDPin, HIGH);
}
}
//if you've previously registered a press, but you now read low
if(buttonClosedPressed && digitalRead(setClosedValuePin) == LOW) buttonClosedPressed = false;
//if you're setting the closed value and you've pressed the button to save it
if(settingClosed && digitalRead(setClosedValuePin == HIGH)){
digitalWrite(LEDPin, LOW);
EEPROM.write(positionClosedLocation, positionClosed);
settingClosed = false;
}
//if you're setting the closed position
if(settingClosed){
//Read the potentiometer position and store it, dividing by 4 because of the EEPROM's storage size
positionClosed = analogRead(potPin) / 4;
}
}
ChickenDoor.txt (3.72 KB)