// Bed Turner F Real delay added. This is the first version to be used for real. // Nigel DADSWELL // Jan 2017 // // Variable Declaration int(motorruntime)=10; //SECONDS - Maximum time to complete a motor movement (set on motor drive board)to cope with slower actuator int(dwellminutes)=45; //Set dwell in MINUTES : Full dwell is used motor full up, but /3 flat and also for dwell // void setup() { // ******The setup function runs once when you press reset or power the board**** Serial.begin(9600); //Setup serial output pinMode(4, INPUT_PULLUP); //Set relay outputs to HIGH before pin assignment to stop relay spurious trigger during powerup pinMode(6, INPUT_PULLUP); pinMode(7, INPUT_PULLUP); pinMode(2, INPUT_PULLUP); //Center Dwell switch Switches to ground pinMode(3, OUTPUT); //Center Dwell Indicator digitalWrite(4, HIGH); // set to HIGH before pin assignment to stop pulse to LOW on startup pinMode(4, OUTPUT); //Fault Relay pinMode(5, OUTPUT); //Fault Indicator digitalWrite(6, HIGH); // set to HIGH before pin assignment to stop pulse to LOW on startup pinMode(6, OUTPUT); //Motor UP trigger digitalWrite(7, HIGH); // set to HIGH before pin assignment to stop pulse to LOW on startup pinMode(7, OUTPUT); //Motor DOWN trigger pinMode(14, INPUT); //UP Prox Sensor pinMode(15, INPUT); //DOWN Prox Sensor // Initialise Outputs (all off) digitalWrite(3, LOW); digitalWrite(5, LOW); // POWERUP TEST OF FAULT SYSTEM digitalWrite(5,HIGH); //turn on FAULT LIGHT digitalWrite(4,LOW); //turn on FAULT RELAY delay(3000); //digitalWrite(6,LOW); //Trigger motor UP to initialise the output //delay(500); //digitalWrite(6,HIGH); digitalWrite(5,LOW); //turn off FAULT LIGHT digitalWrite(4,HIGH); //turn off FAULT RELAY delay(1500); } // void loop() { // *****************MAIN LOOP********************* // int dwell=digitalRead(2); // Read dwell switch, if pressed dwell is selected, set dwell variable to yes if (dwell==HIGH) { // switch open is HIGH, switch to ground is LOW digitalWrite(3,LOW); // If dwell is selected, output to dwell indicator } else { digitalWrite(3,HIGH); //If dwell not HIGH then turn off dwell indicator } // FAULT CHECK AND ACTION int downread = digitalRead(15); //Read down prox sensor (1=far, 0=near) Serial.println(downread); //Send sensor reading to SERIAL MONITOR if (downread==1){ //FAULT if prox sensor FAR=1 (NEAR=0) digitalWrite(5,HIGH); //turn on FAULT LIGHT digitalWrite(4,LOW); //turn on FAULT RELAY } // END OF FAULT CHECK // digitalWrite(6,LOW); //Trigger motor UP delay(500); digitalWrite(6,HIGH); delay(motorruntime*1000); //Wait for motion to complete (seconds*1000) // FAULT CHECK AND ACTION downread = digitalRead(15); //Read down prox sensor (1=far, 0=near) Serial.println(downread); //Send sensor reading to SERIAL MONITOR if (downread==0){ //FAULT if prox sensor FAR=1 (NEAR=0) digitalWrite(5,HIGH); //turn on FAULT LIGHT digitalWrite(4,LOW); //turn on FAULT RELAY } // END OF FAULT CHECK // if (dwell==LOW) { delay(dwellminutes*60000/3); //If dwell= LOW, then wait dwell/3 minutes } digitalWrite(6,LOW); //Trigger motor UP delay(500); digitalWrite(6,HIGH); delay(motorruntime*1000); //Wait for motion to complete // FAULT CHECK AND ACTION downread = digitalRead(15); //Read down prox sensor (1=far, 0=near) Serial.println(downread); //Send sensor reading to SERIAL MONITOR if (downread==0){ //FAULT if prox sensor FAR=1 (NEAR=0) digitalWrite(5,HIGH); //turn on FAULT LIGHT digitalWrite(4,LOW); //turn on FAULT RELAY } // END OF FAULT CHECK delay(dwellminutes*60000); //Standard dwell //Reverse Direction // digitalWrite(7,LOW); //Trigger motor DOWN delay(500); digitalWrite(7,HIGH); delay(motorruntime*1000); //Wait for motion to complete // FAULT CHECK AND ACTION downread = digitalRead(15); //Read down prox sensor (1=far, 0=near) Serial.println(downread); //Send sensor reading to SERIAL MONITOR if (downread==0){ //FAULT if prox sensor FAR=1 (NEAR=0) digitalWrite(5,HIGH); //turn on FAULT LIGHT digitalWrite(4,LOW); //turn on FAULT RELAY } // END OF FAULT CHECK // if (dwell==LOW) { delay(dwellminutes*60000/3); //If dwell=LOW, then wait dwell minutes } digitalWrite(7,LOW); //Trigger motor DOWN delay(500); digitalWrite(7,HIGH); delay(motorruntime*1000); //Wait for motion to complete // FAULT CHECK AND ACTION downread = digitalRead(15); //Read down prox sensor (1=far, 0=near) Serial.println(downread); //Send sensor reading to SERIAL MONITOR if (downread==1){ //FAULT if prox sensor FAR=1 (NEAR=0) digitalWrite(5,HIGH); //turn on FAULT LIGHT digitalWrite(4,LOW); //turn on FAULT RELAY } // END OF FAULT CHECK digitalWrite(7,LOW); //Trigger motor DOWN ADDITIONAL DOWN COMMAND TO ENSURE THE MOTOR IS REALY FULLY DOWN delay(500); digitalWrite(7,HIGH); delay(dwellminutes*60000/3); //Standard dwell/3 : Bed is now flat : Dwell bed flat is less than the side } //End of Main Loop