i plan on using a dc motor connected to a pulley to raise and lower a door until it hits an upper and lower limit switch, in repsonse to being activated by a photoresistor recieving a particular light level twice a day. this is the code i did i think it's probably okay hopefully
const int photocellPin = A0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
boolean x = true;
const int in3 = 7; // this is the pins for the h-bridge
const int in4 = 6;
const int toplimitswitch = 8;
const int bottomlimitswitch = 9;
void setup() {
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(toplimitswitch, INPUT); // top limit switch
pinMode(bottomlimitswitch, INPUT); // bottom limit switch
Serial.begin(9600);
}
void loop() {
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading
delay(100);
if ((photosensor << 300) && (x = true)) {
digitalWrite(in3, HIGH); // turn the motor in one direction (closes door)
digitalWrite(in4, LOW);
if (digitalRead(bottomlimitswitch) = HIGH) {
digitalWrite(in3, LOW); // turn the motor off
digitalWrite(in4, LOW);
x = false;
}
}
else if ((photosensor >> 800) && (x = false)) {
digitalWrite(in3, LOW); // turn the motor in other direction (opens door)
digitalWrite(in4, HIGH);
if (digitalRead(toplimitswitch) = HIGH) {
digitalWrite(in3, LOW); // turn the motor off
digitalWrite(in4, LOW);
x = true;
}
}
}
i was wondering if i added a manual override switch to that what would it involve if it's not too difficult. from what i can gather i would require one button to switch it from automatic mode (using photo resistor) to manual and another to make the door go up and down manually
i found this on switches:
const byte switchPin = 8;
byte oldSwitchState = HIGH; // assume switch open because of pull-up resistor
const unsigned long debounceTime = 10; // milliseconds
unsigned long switchPressTime; // when the switch last changed state
void setup ()
{
Serial.begin (115200);
pinMode (switchPin, INPUT_PULLUP);
} // end of setup
void loop ()
{
// see if switch is open or closed
byte switchState = digitalRead (switchPin);
// has it changed since last time?
if (switchState != oldSwitchState)
{
// debounce
if (millis () - switchPressTime >= debounceTime)
{
switchPressTime = millis (); // when we closed the switch
oldSwitchState = switchState; // remember for next time
if (switchState == LOW)
{
Serial.println ("Switch closed.");
} // end if switchState is LOW
else
{
Serial.println ("Switch opened.");
} // end if switchState is HIGH
} // end if debounce time up
} // end of state change
// other code here ...
} // end of loop
which i thought is quite easy i can just take my limit switch code and pop it in there, but i was wondering if i would need to use it twice once to go from automatic to manual and a second time for the door going up and down and whether i am going about it wrong, it seems it would have no way to go back from manual to automatic once it's in that function (is that the right word?) if that makes sense.
const int photocellPin = A0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the sensor divider
boolean x = true;
const int in3 = 7; // this is the pins for the h-bridge
const int in4 = 6;
const int toplimitswitch = 8;
const int bottomlimitswitch = 9;
const byte switchPin = 9;
byte oldSwitchState = HIGH; // assume switch open because of pull-up resistor
const unsigned long debounceTime = 10; // milliseconds
unsigned long switchPressTime; // when the switch last changed state
void setup() {
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(toplimitswitch, INPUT); // top limit switch
pinMode(bottomlimitswitch, INPUT); // bottom limit switch
Serial.begin(9600);
Serial.begin (115200); // is this needed at a higher number seems odd to have both
pinMode (switchPin, INPUT_PULLUP);
}
void loop() {
// see if switch is open or closed
byte switchState = digitalRead (switchPin);
// has it changed since last time?
if (switchState != oldSwitchState)
{
// debounce
if (millis () - switchPressTime >= debounceTime)
{
switchPressTime = millis (); // when we closed the switch
oldSwitchState = switchState; // remember for next time
if (switchState == LOW)
{
Serial.println ("Switch closed.");
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.println(photocellReading); // the raw analog reading
delay(100);
if ((photosensor << 300) && (x = true)) {
digitalWrite(in3, HIGH); // turn the motor in one direction (closes door)
digitalWrite(in4, LOW);
if (digitalRead(bottomlimitswitch) = HIGH) {
digitalWrite(in3, LOW); // turn the motor off
digitalWrite(in4, LOW);
x = false;
}
} // ?
else if ((photosensor >> 800) && (x = false)) {
digitalWrite(in3, LOW); // turn the motor in other direction (opens door)
digitalWrite(in4, HIGH);
if (digitalRead(toplimitswitch) = HIGH) {
digitalWrite(in3, LOW); // turn the motor off
digitalWrite(in4, LOW);
x = true;
}
}
} // end if switchState is LOW
else
{
Serial.println ("Switch opened.");
//HERE I WOULD BE REPEATING THE SWITCH CODE for manual up and down?????????????
} // end if switchState is HIGH
} // end if debounce time up
} // end of state change
}
am i on the right track? thanks for any help