Untitled

 avatar
unknown
plain_text
2 years ago
16 kB
5
Indexable
#include <EEPROM.h>

int Test_address = 2;

unsigned int currentTimer = 0;
unsigned long previousMillis1 = 0;//0;4294967295
 
unsigned long previousMillis2 = 0;//0;4294967295
const long interval = 100;
 
bool MUST_STOP = false;
 
#define LOCK_PIN 6
#define STOP_BUTTON 7
 
#define RESET_NOTOR1_BUTTON 38
#define RESET_NOTOR2_BUTTON 34
 
/* Motor #1 power variables start*/
#define MOTOR_1_MAINS A9 //13  //mains power to motor #1
#define MOTOR_1_UP 12     //mains to motor #1 up
#define MOTOR_1_DOWN 11   //mains to motor #1 up
 
bool MOTOR_1_MAINS_BOOL = false;
bool MOTOR_1_UP_BOOL= false;
bool MOTOR_1_DOWN_BOOL= false;
/* Motor #1 power variables end*/
 
/* Motor #2 power variables start*/
#define MOTOR_2_MAINS 10  //mains power to motor #1
#define MOTOR_2_UP 8      //mains to motor #1 up
#define MOTOR_2_DOWN 9    //mains to motor #1 up
 
bool MOTOR_2_MAINS_BOOL = false;
bool MOTOR_2_UP_BOOL = false;
bool MOTOR_2_DOWN_BOOL = false;
/* Motor #2 power variables end*/
 
/*Gates Current State start*/
bool isGateOpenState = false;
bool isGateClosedState = true;
bool isGateMovingState = false;
 
bool isOpeningGate = false;
bool isClosingGate = false;
/*Gates Current State end*/
 
/*Variables for RF remotes start*/
int A_KEY = 5;
int B_KEY = 4;
int C_KEY = 2;
int D_KEY = 3;
 
int a_previousVal = 0;
int b_previousVal = 0;
int c_previousVal = 0;
int d_previousVal = 0;
/*Variables for RF remotes end*/
 
 
void setup() {
     
     //EEPROM.write(Test_address, 21);
  // put your setup code here, to run once:
  if(a_previousVal == 1)
    a_previousVal = 0;
 
  if(b_previousVal == 1)
    b_previousVal = 0;
 
  if(c_previousVal == 1)
    c_previousVal = 0;
 
  if(d_previousVal == 1)
    d_previousVal = 0;
 
  Serial.begin(9600);


  delay(500);

/*
  byte value;
  value = EEPROM.read(isGateOpenState_address);
  isGateOpenState = value;
  Serial.println("Isopened = " + String(isGateOpenState));





  byte value1;
  value1 = EEPROM.read(isGateClosedState_address);
  isGateClosedState = value1;
  Serial.println("Isclosed = " + String(isGateClosedState));
  */


// 1 = true, 2 = false; open/close 21 = gate closed. 12 = gate opened.
  byte value2;
  value2 = EEPROM.read(Test_address);
  Serial.println("test = " + String(value2));

  isGateOpenState = returnBool(String(value2)[0]);
  isGateClosedState = returnBool(String(value2)[1]);


  Serial.println("Isopened = " + String(isGateOpenState));
  Serial.println("Isclosed = " + String(isGateClosedState));

  //RF inputs
  pinMode(A_KEY, INPUT_PULLUP);
  pinMode(B_KEY, INPUT_PULLUP);
  pinMode(C_KEY, INPUT_PULLUP);
  pinMode(D_KEY, INPUT_PULLUP);
 
 
  pinMode(LOCK_PIN, OUTPUT);
  digitalWrite(LOCK_PIN, HIGH);
  pinMode(RESET_NOTOR1_BUTTON, INPUT_PULLUP);
  pinMode(RESET_NOTOR2_BUTTON, INPUT_PULLUP);
  //motor #1
  pinMode(MOTOR_1_MAINS, OUTPUT);
  pinMode(MOTOR_1_UP, OUTPUT);
  pinMode(MOTOR_1_DOWN, OUTPUT);
 
 
  digitalWrite(MOTOR_1_MAINS, HIGH);
  digitalWrite(MOTOR_1_UP, HIGH);
  digitalWrite(MOTOR_1_DOWN, HIGH);
 
  //motor #2
  pinMode(MOTOR_2_MAINS, OUTPUT);
  pinMode(MOTOR_2_UP, OUTPUT);
  pinMode(MOTOR_2_DOWN, OUTPUT);
 
 
  digitalWrite(MOTOR_2_MAINS, HIGH);
  digitalWrite(MOTOR_2_UP, HIGH);
  digitalWrite(MOTOR_2_DOWN, HIGH);

  Serial.println("Main setup code run.");
  delay(500);
 
  pinMode(STOP_BUTTON, INPUT_PULLUP);
  
  Serial.println("Stop button setup code run.");
}
 
void loop() {
  ResetMotor1Button();
  ResetMotor2Button();
 
  RF_INPUT_LOOP();
  StopButton();
}
 
bool resetMotor1ButtonWasOn = false;
 
void ResetMotor1Button() {
  int buttonValue = digitalRead(RESET_NOTOR1_BUTTON);
  if (buttonValue == HIGH) {
      resetMotor1ButtonWasOn = true;
    // If button pushed, turn LED on
    //EmergencyStop();
      MUST_STOP = false;
      isClosingGate = false;
      isOpeningGate = false;
      isGateOpenState = false;
      isGateClosedState = true;
      isGateMovingState = false;
      currentTimer = 0;  
         //EEPROM.write(isGateOpenState_address, isGateOpenState);    
        //EEPROM.write(isGateClosedState_address, isGateClosedState);
      CloseMotor_1_On();
    Serial.println("Reset motor1 button on.. Turn close motors on..");
  }
  if(buttonValue == LOW){
    // Serial.println("Button not down.");
    if(resetMotor1ButtonWasOn){
     
      Serial.println("Reset motor1 button off.. Turn close motors off..");
      CloseMotor_1_Off();
     
      MUST_STOP = false;
      isClosingGate = false;
      isOpeningGate = false;
      isGateOpenState = false;
      isGateClosedState = true;
      isGateMovingState = false;
      currentTimer = 0;  
      resetMotor1ButtonWasOn = false;
    }
  }
}
 
 
bool resetMotor2ButtonWasOn = false;
 
void ResetMotor2Button() {
  
  int buttonValue = digitalRead(RESET_NOTOR2_BUTTON);
  if (buttonValue == HIGH) {
      resetMotor2ButtonWasOn = true;
    // If button pushed, turn LED on
    //EmergencyStop();
      MUST_STOP = false;
      isClosingGate = false;
      isOpeningGate = false;
      isGateOpenState = false;
      isGateClosedState = true;
      isGateMovingState = false;
      currentTimer = 0;  
     
      CloseMotor_2_On();
    //EEPROM.write(isGateOpenState_address, isGateOpenState);    
    //EEPROM.write(isGateClosedState_address, isGateClosedState);
    Serial.println("Reset motor2 button on.. Turn close motors on..");
  }
  if(buttonValue == LOW){
    // Serial.println("Button not down.");
    if(resetMotor2ButtonWasOn){
     
      Serial.println("Reset motor2 button off.. Turn close motors off..");
      CloseMotor_2_Off();
     
      MUST_STOP = false;
      isClosingGate = false;
      isOpeningGate = false;
      isGateOpenState = false;
      isGateClosedState = true;
      isGateMovingState = false;
      currentTimer = 0;  
      resetMotor2ButtonWasOn = false;
    }
  }
  
}
 
 
void StopButton() {
  int buttonValue = digitalRead(STOP_BUTTON);
  if (buttonValue == LOW) {
    // If button pushed, turn LED on
    EmergencyStop();
    Serial.println("Emergency Stop button pressed..");
  } else {
    // Serial.println("Button not down.");
  }
}
 
void RF_INPUT_LOOP() {
  if (a_previousVal == 0 && digitalRead(A_KEY) == 1) {
    a_previousVal = 1;
    Event_A_BUTTON_DOWN();
  } else if (a_previousVal == 1 && digitalRead(A_KEY) == 0) {
    Serial.println("A Button Up.");
    a_previousVal = 0;
  }
 
  if (b_previousVal == 0 && digitalRead(B_KEY) == 1) {
    b_previousVal = 1;
    Event_B_BUTTON_DOWN();
  } else if (b_previousVal == 1 && digitalRead(B_KEY) == 0) {
    Serial.println("B Button Up.");
    b_previousVal = 0;
  }
 
  if (c_previousVal == 0 && digitalRead(C_KEY) == 1) {
    c_previousVal = 1;
    Event_C_BUTTON_DOWN();
  } else if (c_previousVal == 1 && digitalRead(C_KEY) == 0) {
    Serial.println("C Button Up.");
    c_previousVal = 0;
  }
  if (d_previousVal == 0 && digitalRead(D_KEY) == 1) {
    d_previousVal = 1;
    Event_D_BUTTON_DOWN();
  } else if (d_previousVal == 1 && digitalRead(D_KEY) == 0) {
    Serial.println("D Button Up.");
    d_previousVal = 0;
  }
}    
 
void Event_A_BUTTON_DOWN() {
  Serial.println("A Button down.");
  if (currentTimer == 0 && isGateClosedState){
    OpenGates();
  }
}
 
void Event_B_BUTTON_DOWN() {
  Serial.println("B Button down.");
  if (currentTimer == 0 && isGateOpenState) {
    CloseGates();
  }
}
 
void Event_C_BUTTON_DOWN() {
  Serial.println("C Button down.");
 
      //CloseMotor_2_On();
}
 
void Event_D_BUTTON_DOWN() {
  Serial.println("D Button down.");
  EmergencyStop();
}
 
void OpenGates() {
  unsigned int delayBetweenOpening = 8000;
  unsigned int singleGateOpeningTime = 16100;
  unsigned int delayForLock = 1000;
 
  unsigned int fullOpenTime = singleGateOpeningTime + delayBetweenOpening + delayForLock + 1000;
  isOpeningGate = true;
  
  currentTimer = fullOpenTime;
 
  Serial.println("Opening gate timer started: " + String(currentTimer));
  Serial.println("Previous MS = " + String(previousMillis1));
 
  unsigned long currentMillis = millis();
  Serial.println("current ms = " + String(currentMillis));
 
  while (isOpeningGate && !MUST_STOP) {
    RF_INPUT_LOOP();
    StopButton();
 
    unsigned long currentMillis = millis();
 
    if(previousMillis1 > currentMillis){
      Serial.println("roll over must have happened right?");
    }
 
    if (currentMillis - previousMillis1 >= interval && currentTimer >= 100) {
      previousMillis1 = currentMillis;
      currentTimer -= interval;
      Serial.println(currentTimer);
    }
 
 
    if (currentTimer == (fullOpenTime - 100)) {
      //1 second into opening time...
      digitalWrite(LOCK_PIN, LOW);  //open lock
      Serial.println(F("Timer Started. Opened Lock."));
    }
 
    if (currentTimer == (fullOpenTime - delayForLock)) {
      //1 second into opening time...
      Serial.println(F("1 second into timer, can start Motor 1."));
 
      OpenMotor_1_On();
    }
 
    if (currentTimer == (fullOpenTime - (delayBetweenOpening + delayForLock))) {
      //1 second into opening time...
      Serial.println(String(delayBetweenOpening + delayForLock) + " seconds into opening, can start Motor 2.");
 
      OpenMotor_2_On();
    }
 
    if (currentTimer == (fullOpenTime - (singleGateOpeningTime + delayForLock))) {
      //1 second into opening time...
      Serial.println(String(fullOpenTime - currentTimer) + " seconds into opening, can stop Motor 1.");
 
      OpenMotor_1_Off();
    }
 
    if (currentTimer == (fullOpenTime - (singleGateOpeningTime + delayForLock + delayBetweenOpening) - 1000)) {
      //1 second into opening time...
      Serial.println(String(fullOpenTime - currentTimer) + " seconds into opening, can stop Motor 2.");
      OpenMotor_2_Off();
    }
 
    if (currentTimer == 0 || currentTimer <= 99) {
      isOpeningGate = false;
      Serial.println(F("Timer ended. Close Lock."));
      digitalWrite(LOCK_PIN, HIGH);  //Close lock
      isGateOpenState = true;
      
      isGateClosedState = false;
      isGateMovingState = false;
      isClosingGate = false;
      currentTimer = 0;
      //EEPROM.write(isGateOpenState_address, 1);    
      //EEPROM.write(isGateClosedState_address, 0);    
      EEPROM.write(Test_address, 12);
    }
  }
}
 
void CloseGates() {
  unsigned int delayBetweenClosing = 20000;
  unsigned int singleGateClosingTime = 27000;
  unsigned int delayForLock = 1000;
 
  unsigned int fullCloseTime = singleGateClosingTime + delayBetweenClosing + delayForLock;
 
  Serial.println(String(fullCloseTime));
 
  isClosingGate = true;
  currentTimer = fullCloseTime;
 
  Serial.println(fullCloseTime);
  Serial.println("Closing gate timer started: " + String(currentTimer));
  Serial.println("IsClosingGate" + String(isClosingGate));
  Serial.println("Must stop? " + String(MUST_STOP));
 
 
  while (isClosingGate && !MUST_STOP) {
    RF_INPUT_LOOP();
    StopButton();
 
    unsigned long currentMillis = millis();
    if (currentMillis - previousMillis2 >= interval && currentTimer >= 100) {
      previousMillis2 = currentMillis;
      currentTimer -= interval;
      Serial.println(currentTimer);
    }
 
    if (currentTimer == (fullCloseTime - 100)) {
      //1 second into opening time...
    }
 
    if (currentTimer == (fullCloseTime - delayForLock)) {
      //1 second into opening time...
      Serial.println(F("1 second into timer, can start Motor 2."));
 
      CloseMotor_2_On();
    }
 
    if (currentTimer == (fullCloseTime - (delayBetweenClosing + delayForLock))) {
      //1 second into opening time...
      Serial.println(String(delayBetweenClosing + delayForLock) + " seconds into timer, can start Motor 2.");
 
      CloseMotor_1_On();
    }
 
    if (currentTimer == (fullCloseTime - (singleGateClosingTime + delayForLock))) {
      //1 second into opening time...
      Serial.println(String(fullCloseTime - currentTimer) + " seconds into timer, can stop Motor 2.");
 
      CloseMotor_2_Off();
    }
 
    if (currentTimer == (fullCloseTime - (singleGateClosingTime + delayForLock + delayBetweenClosing) + 1400)) {
      //1 second into opening time...
      Serial.println(String(fullCloseTime - currentTimer) + " seconds into timer, can stop Motor 1.");
      CloseMotor_1_Off();
    }
 
    if (currentTimer == 0) {
      isClosingGate = false;
      Serial.println(F("Timer ended. Gate Closed."));
 
      isOpeningGate = false;
      isGateOpenState = false;
      isGateClosedState = true;
      isGateMovingState = false;
      currentTimer = 0;
      //EEPROM.write(isGateOpenState_address, 0);    
      //EEPROM.write(isGateClosedState_address, 1);    
      EEPROM.write(Test_address, 21);
    }
  }
}
 
void OpenMotor_1_On() {
  MOTOR_1_MAINS_BOOL = true;
  Toggles(MOTOR_1_MAINS_BOOL, MOTOR_1_MAINS);
 
  MOTOR_1_UP_BOOL = true;
  Toggles(MOTOR_1_UP_BOOL, MOTOR_1_UP);
}
 
void OpenMotor_1_Off() {
  MOTOR_1_MAINS_BOOL = false;
  Toggles(MOTOR_1_MAINS_BOOL, MOTOR_1_MAINS);
 
  MOTOR_1_UP_BOOL = false;
  Toggles(MOTOR_1_UP_BOOL, MOTOR_1_UP);
}
 
void OpenMotor_2_On() {
  MOTOR_2_MAINS_BOOL = true;
  Toggles(MOTOR_2_MAINS_BOOL, MOTOR_2_MAINS);
 
  MOTOR_2_UP_BOOL = true;
  Toggles(MOTOR_2_UP_BOOL, MOTOR_2_UP);
}
 
void OpenMotor_2_Off() {
  MOTOR_2_MAINS_BOOL = false;
  Toggles(MOTOR_2_MAINS_BOOL, MOTOR_2_MAINS);
 
  MOTOR_2_UP_BOOL = false;
  Toggles(MOTOR_2_UP_BOOL, MOTOR_2_UP);
}
 
void CloseMotor_1_On() {
  MOTOR_1_MAINS_BOOL = true;
  Toggles(MOTOR_1_MAINS_BOOL, MOTOR_1_MAINS);
 
  MOTOR_1_DOWN_BOOL = true;
  Toggles(MOTOR_1_DOWN_BOOL, MOTOR_1_DOWN);
}
 
void CloseMotor_1_Off() {
  MOTOR_1_MAINS_BOOL = false;
  Toggles(MOTOR_1_MAINS_BOOL, MOTOR_1_MAINS);
 
  MOTOR_1_DOWN_BOOL = false;
  Toggles(MOTOR_1_DOWN_BOOL, MOTOR_1_DOWN);
}
 
void CloseMotor_2_On() {
  MOTOR_2_MAINS_BOOL = true;
  Toggles(MOTOR_2_MAINS_BOOL, MOTOR_2_MAINS);
 
  MOTOR_2_DOWN_BOOL = true;
  Toggles(MOTOR_2_DOWN_BOOL, MOTOR_2_DOWN);
}
 
void CloseMotor_2_Off() {
  MOTOR_2_MAINS_BOOL = false;
  Toggles(MOTOR_2_MAINS_BOOL, MOTOR_2_MAINS);
 
  MOTOR_2_DOWN_BOOL = false;
  Toggles(MOTOR_2_DOWN_BOOL, MOTOR_2_DOWN);
}
 
 
void Toggles(bool toggleBool, int pinNumber) {
  //true bool in = high = turn off LED = turn of relay power
  if (toggleBool) {
    //true
    digitalWrite(pinNumber, LOW);
  }
  if (!toggleBool) {
    //false
    digitalWrite(pinNumber, HIGH);
  }
}
 
void EmergencyStop() {
  Serial.println(MOTOR_1_UP_BOOL);
 
  Serial.println(MOTOR_1_DOWN_BOOL);
 
  Serial.println(MOTOR_2_UP_BOOL);
 
  Serial.println(MOTOR_2_DOWN_BOOL);
  if(MOTOR_1_UP_BOOL == true || MOTOR_1_DOWN_BOOL == true || MOTOR_2_UP_BOOL == true || MOTOR_2_DOWN_BOOL == true){    
    MUST_STOP = true;
    Serial.println("called emergency,,,,");
    OpenMotor_1_Off();
    CloseMotor_1_Off();
 
    OpenMotor_2_Off();
    CloseMotor_2_Off();
    currentTimer = 0;
  }else{
    Serial.println("No motors on no point to emergency stop...");
  }
}

bool returnBool(char state){
  switch(state){
    case '2':
      return false;
    case '1': 
      return true;
    default:
      return false;
  }
}
Editor is loading...