Hi all,
I'm working on a fairly simple little device that will listen to an analog pressure sensor, and when the sensor picks up anything above 0, it will delay, then move a linear actuator (which will drop the pressure to 0 eventually), then standby for the next pressure reading. I've never worked with any of the various sleep modes with arduino but I'd like to have the arduino go to the deepest possible sleep as long as the pressure is at 0, and let that analog sensor act as the "wake-up switch" when pressure is detected again. Any tips on the best way to code this? Here is my code so far with no sleep code written yet.
const int dir = 5; //direction pin on motor control board
const int pwm = 6; //motor enable pin
int potPin = A1; //potentiometer inside linear actuator
int potVal = 0;
int wlsPin = A2; //pressure sensor
int wlsVal = 0;
void setup() {
pinMode(pwm, OUTPUT);
pinMode(dir, OUTPUT);
pinMode(wlsPin, INPUT);
pinMode(potPin, INPUT);
Serial.begin(9600);
}
///////////////////////////Arm Movement Protocols/////////////////////////////////////////
void extendArm(){ //protocol for extending arm
digitalWrite(pwm, HIGH);
digitalWrite (dir, LOW);
while(potVal > 15){
digitalWrite (pwm, HIGH);
digitalWrite (dir, LOW);
if((potVal < 20) && (potVal > 10)){
digitalWrite (pwm, LOW);
}
}
Serial.print("Extending Arm");
}
void retractArm(){ //protocol for retracting Arm
digitalWrite(pwm, HIGH);
digitalWrite(dir, HIGH);
while(potVal < 1005){
digitalWrite(pwm, HIGH);
digitalWrite(dir, HIGH);
if((potVal > 1000) && (potVal < 1010)){
digitalWrite(pwm, LOW);
}
}
Serial.print("Retracting Arm");
}
void loop() {
potVal = analogRead(potPin);
wlsVal = analogRead(wlsPin);
/////////////////////////Boolean Definitions////////////////////////////////////////
boolean armIsRetracted;
if((potVal < 1010) && (potVal > 1000)){
armIsRetracted = true; //boolean function for armIsRetracted
Serial.print("Arm is Retracted");
}
else{
armIsRetracted = false;
}
boolean armIsExtended;
if((potVal < 20) && (potVal > 10)){
armIsExtended = true; //boolean function for armIsExtended
Serial.print("armIsExtended");
}
else{
armIsExtended = false;
}
boolean pressureIsPresent;
if(wlsVal > 0){
pressureIsPresent = true; //boolean function for pressureIsPresent
Serial.println("Pressure is Present");
}
else{
pressureIsPresent = false;
}
boolean pressureNotPresent;
if(wlsVal<=0){
pressureNotPresent = true; //boolean function for pressureNotPresent
Serial.println("pressure not Present");
}
else{
pressureNotPresent = false;
}
//////////////////////////Hardware control loop////////////////////////////////////////////
if((pressureNotPresent) && (armIsExtended)) {} //if no pressure, do nothing
if(pressureIsPresent){
delay (15000);
retractArm(); //if pressure is present, delay x time then retract arm
}
if(armIsRetracted){
while (pressureIsPresent){} //if arm is retracted and pressure still present, do nothing
}
if((armIsRetracted) && (pressureNotPresent)){
delay (5000);
extendArm(); //if arm is retracted and pressure goes below 1, delay x time then extend arm
}
}