I ran out of time today, and I need to figure out what went wrong.
My understanding of C is minimal, as you can tell, and I think my problem is the code.
I want to take my time and understand what I did wrong, but I also want to finish this thing.
if you see anything glaringly obvious, please tell how that operation should work, so I can learn from my mistake.
/*
This is my first attempt at writing a functional code.
My goal is to automate the opening, closing and locking
of the door to my chicken coop. If you notice any problems
that will prevent the operation let me know
jakedixon@hotmail.com
*/
#include <Servo.h>
Servo Latch;
Servo Door;
int S0=0; //Swithch state one
int S1=0; //Swithch state two
int S2=0; //Swithch state three
int S3=0; //Swithch state four
int CD=0; //Close disable
int OD=0; //Open Disable
int TM=0; //time variable
int MD=0; //Mode state
int Val=0; //Light Value
int sensorPin = A0; //Analog input pin
void setup() {
Latch.attach(9);
Door.attach(10);
}
void loop() { //Read 4 position selector switch
S0= digitalRead(0); //Off
S1= digitalRead(1); //Manual open
S2= digitalRead(2); //Manual Close
S3= digitalRead(3); //Auto
if (S0=1){MD=0;} //convert switch to mode
if (S1=1){MD=1;}
if (S2=1){MD=2;}
if (S3=1){Val=analogRead(sensorPin);
if (Val>150){MD=1;}
if (Val<100){MD=2;}
}
if (MD=1){
if (CD=0){TM++;delay (20);
if(TM<16){Latch.write(135);} //.2 sec/30deg of movement therefore Rotate 90deg = 300mS or 15 cycles
if(TM>20){Door.write(170);} // delay, open main door 180deg=600ms 600ms/20 per cycle= 30 cycles
if(TM>52){CD=1;OD=0;TM=0;} //disable close, enable open reset TiMer
}
}
if (MD=2){
if (OD=0){TM++;delay (20);
if(TM<32){Door.write(10);} //.2 sec/30deg of movement therefore Rotate 90deg = 300mS or 15 cycles
if(TM>40){Latch.write(45);} // delay, open main door 180deg=600ms 600ms/20 per cycle= 30 cycles
if(TM>52){OD=1;CD=0;TM=0;} //disable close, enable open reset TiMer
}
}
}