Go Down

Topic: Rotating door automation (Read 5 times) previous topic - next topic

TKHouse

This is a great project. Please post a video when finished!

Out of curiosity, are there any concerns as far as the chickens being in the way of the door when it is being closed? If so, do you have any plans for handling that?

ChickenMechanic

Thanks.

I Haven't got any sensors to detect obstruction if that's what you mean. I have added code to make the sweep of the servo adjustable so the door closes slowly. I'm hoping that should be enough.

At the minute I have the hardware mostly finished. The main thing I need is a replacement servo arm as I managed to strip the teeth from the plastic one that the servo came with. A metal one is ordered. The power baluns are done and the servo mounting enclosure.

What I would like to do is fine tune the starting and finishing position of the servo.

Is it possible to use a potentiometer to adjust a value BUT have the adjusted value saved even when the pot is removed?

TKHouse



Is it possible to use a potentiometer to adjust a value BUT have the adjusted value saved even when the pot is removed?


That is certainly doable. You could have something as simple as a button (could even use one already on the project) that, when held down for a while, begins a subroutine that lets you use the pot to set the starting position, press button again to save value to EEPROM, set finish position with pot, press button again to save to EEPROM.

You can also have the subroutine change the appropriate pins to an input instead of an output, that way you wouldn't have a pin set as an input all the time and not really doing anything, although I doubt it would have any unintended results anyway.

Your code would simply load the start and finish values from the EEPROM on power up. Note the first time the device powers up the contents of the EEPROM could be random values, so be mindful of that.

ChickenMechanic

I suppose another way to do it would be 2 buttons, one to set the open value, one to set the closed value.

Press and hold to enter the subroutine ( maybe with an LED to show it was active) and press agin to save the setting.

What would the code for something like that look like?

TKHouse

#24
Nov 07, 2012, 09:43 pm Last Edit: Nov 07, 2012, 09:47 pm by TKHouse Reason: 1
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

Code: [Select]

#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;
}

}


Go Up