Hi,
I'm new to the arduino and I am trying to use the arduino UNO to open and close a gate, based on the amount of light.
So I found this code:
and I want to change the code so it will open and close by reading the value of a LDR.
I am still waiting for my arduino UNO to arrive, but thought I might as well do some coding up front, when it arrives
I think I best start with some examples found in the IDE program, but doing some of the code might help me learn faster.
I made some changes to the code and was wondering if anyone could check it to see if it will work the way I want to.
I replaced (or at least tried to..) the button-switch with code for the LDR.
here's the code I changed, please someone look over it and tell me where I've gone wrong, or not.
const int activate = 2; //D2 pin on arduino
const int gateOpen = 3; //D3 pin on arduino
const int gateClosed = 4; //D4 pin on arduino
const int bridgeOneA = 5; //D5 pin on arduino
const int bridgeTwoA = 6; //D6 pin on arduino
const int enable = 7; //A0 pin on arduino
const int statLED = 13; //D13 pin on arduino
const int ldr_pin = 0; //A0 pin on arduino
int ldr_value;
int ldr_cutoff = 400; //ADC value, change from light to dark, vice versa
unsigned long currentTime = 0;
boolean gateState = false; //false = closed true = open
void setup() {
Serial.begin(9600);
pinMode(activate, INPUT); //set up I/O
pinMode(gateOpen, INPUT);
pinMode(gateClosed, INPUT);
pinMode(bridgeOneA, OUTPUT);
pinMode(bridgeTwoA, OUTPUT);
pinMode(enable, OUTPUT);
digitalWrite(enable, LOW); //make sure H-Bridge is off
pinMode(statLED, OUTPUT); //setup our status LED
}
void loop() {
ldr_value=analogRead(ldr_pin);
if (ldr_value<ldr_cutoff) {
(digitalRead(activate) == HIGH && gateState == false) { //check to see if the button is pressed and the gate is closed
digitalWrite(enable, HIGH); //enable h-bridge
digitalWrite(bridgeOneA, HIGH); //configure for CW rotation
digitalWrite(bridgeTwoA, LOW);
while(1){ //run motor until switch is tripped
if (digitalRead(gateOpen) == LOW) { //check switch state
gateState = true;
digitalWrite(statLED, LOW); //turn off LED
digitalWrite(enable, LOW); //disable h-bridge
digitalWrite(bridgeOneA, LOW); //reset h-bridge config
break;
}
if (millis() > currentTime + 500) { //flash status LED once
digitalWrite(statLED, HIGH);
delay(500);
currentTime = millis();
}
else {
digitalWrite(statLED, LOW);
}
}
}
}
ldr_value=analogRead(ldr_pin);
if (ldr_value>ldr_cutoff) {
(digitalRead(activate) == HIGH && gateState == true) { //check to see if the button is pressed and the gate is open
digitalWrite(enable, HIGH);
digitalWrite(bridgeOneA, LOW); //configure for CCW rotation
digitalWrite(bridgeTwoA, HIGH);
while(1){
if (digitalRead(gateOpen) == LOW) {
gateState = false;
digitalWrite(statLED, LOW);
digitalWrite(enable, LOW);
digitalWrite(bridgeTwoA, LOW);
break;
}
if (millis() > currentTime + 500) { //flash status LED once
digitalWrite(statLED, HIGH);
delay(500);
currentTime = millis();
}
else {
digitalWrite(statLED, LOW);
}
}
}
}
}
Thanks for reading, and I hope someone can help me with verifying the code.