This is my first stab at arduino programming. I am new to the whole programming thing.
I needed to replace the board on my Mighty Mule gate opener that fried and they wanted
350 bucks for a new one. I decided to try my hand at it. Any comments or criticism are
welcome as long as it is constructive.
code:
/*
Author:Michael Wellner
Email: xxxxxxxxxxxx@gmail.com
Date: 7/18/2016
Description: This is a sketch to openGate and shut my driveway gate.
This is my first attempt at arduino programming so take that into
consideration before giving me any grief.
Next phase for this code will be using some electronics current sensor for
detecting the load of the motors so it will know when to cut off the power of the
gate instead of using the delay as the timing to openGate or close the gate.
*/
int remoteState = 2;
int pwm = 9;
int openNow = 6;
int closeNow = 5;
int switch1 = 3;
int switch2 = 4;
int ledPin = 13;
int remotePress = LOW; // Getting the reading from the remote controller
int switch_1 = LOW; // Getting the reading from the gate openGate switch
int switch_2 = LOW; // Getting the reading from the gate closeGate switch
int bothOpen = 1; // setting state of bothOpen to "1"
void setup() {
Serial.begin(9600);
pinMode(remoteState, INPUT_PULLUP); //set up I/O
pinMode(switch1, INPUT); // openGate limit switch
pinMode(switch2, INPUT); // closeGate limit switch
pinMode(openNow, OUTPUT); // send openGate command to H-bridge
pinMode(closeNow, OUTPUT); // send closeGate command to H-bridge
pinMode(pwm, OUTPUT); // send speed command to H-bridge
analogWrite(pwm, 0); //make sure H-Bridge is off
pinMode(ledPin, OUTPUT); //setup our status LED
if(remotePress == LOW) {
delay(2000);
analogWrite(pwm, 0); // Stops motor
Serial.println("STOPPED the MOTOR no signal recieved");
}
}
void loop() {
while(remotePress == LOW) {
delay(500);
remotePress = digitalRead(remoteState);
Serial.println("remotePress"); // for debugging purposes
Serial.println(remotePress); // for debugging purposes
}
Serial.println("remotePress"); // gets readings from the switches and prints them for
Serial.println(remotePress); // for me to see what values they are sending.
switch_1 = digitalRead(switch1); // for debugging purposes
Serial.println("switch_1");
Serial.println(switch_1);
switch_2 = digitalRead(switch2);
Serial.println("switch_2");
Serial.println(switch_2);
bothOpen = switch_1 + switch_2;
Serial.println("bothOpen");
Serial.println(bothOpen);
delay(1000);
if(switch_2 == LOW) { //sends loop to "openGate" module
openGate(); }
else if(switch_1 == LOW) { //sends loop to "closeGate" module
closeGate(); }
else(bothOpen != 1); { //sends loop to "problem" module to stop program
problem(); }
}
void openGate() { // opens gate till the gate meets open limit switch
Serial.println("gate is opening"); // for debugging purposes
digitalWrite(ledPin, HIGH);
digitalWrite(openNow, HIGH);
analogWrite(pwm, 245);
while (switch_1 == HIGH) {
delay(50);
switch_1 = digitalRead(switch1);
Serial.println("switch_1"); // for debugging purposes
Serial.println(switch_1); // for debugging purposes
}
digitalWrite(openNow, LOW);
analogWrite(pwm, 0);
digitalWrite(ledPin, LOW);
remotePress = LOW;
}
void closeGate() { // opens gate till the gate meets close limit switch
Serial.println("gate is closing"); // for debugging purposes
digitalWrite(ledPin, HIGH);
digitalWrite(closeNow, HIGH);
analogWrite(pwm, 245);
while (switch_2 == HIGH) {
delay(50);
switch_2 = digitalRead(switch2);
Serial.println("switch_2"); // for debugging purposes
Serial.println(switch_2); // for debugging purposes
}
digitalWrite(closeNow, LOW);
analogWrite(pwm, 0);
digitalWrite(ledPin, LOW);
remotePress = LOW;
}
void problem() {
Serial.println("Code WTF....Something is wrong with the limit switches ?"); // for debugging purposes
while(0 == 0){} // stops program here because there is a problem machanically.
}