/* External componets are 2 motors open motor close motor 4 switches open command close command emergency stop reset 3 leds fully open green fully closed green in motion blinking red the open motor is energised normally through pins 0 and 1 and the close motor is energized reverse thruogh pins 4 and 5 to open roof on open command the close motor is energised normally through pins 6 and 7 and the open motor is energized reverse thruogh pins 2 and 3 to close roofon close command in both cases reversal is done in the relay wiring the emergency stop button de-energizes all motors read on pin 12 the fully open switch is closed when roof hits open stop read by pin 8 the fully closed switch is closed when roof hits closed stop read by pin 9 the fully open led is on if the fully open switch is closed through analog pin A1 the fully closed led is on if the fully closed switch is closed through pin A0 the in motion led is on if neither fully open or closed are closed through pin A2 the reset switch is connected in hardware and resets the arduino Set up Constants Digital Pins 0-8 are outputs for relay drivers Pin 0 = Out motor 1 out plus Pin 1 = Out motor 1 out minus Pin 2 = Out motor 2 in plus Pin 3 = Out motor 2 in minus Pin 4 = In motor 1 out plus Pin 5 = In motor 1 out minus Pin 6 = In motor 2 in plus Pin 7 = In motor 2 in minus Digital Pins 8-9 are inputs for switches Pin 8 = Fully closed status when HIGH Pin 9 = Fully open status when HIGH Digital pin 10 and 11 are command pins Pin 10 = open command Pin 11 = close command Pin 12 is emergency stop command Digital Pins 10-12 are for status LEDs Pin A0 = Fully closed status led Pin A1 = Fully open status led Pin A2 = Roof moving status */ int openmotoropenplus = 0; int openmotoropenneg = 1; int openmotorcloseplus = 2; int openmotorcloseneg = 3; int closemotoropenplus = 4; int closemotoropenneg = 5; int closemotorcloseplus = 6; int closemotorcloseneg = 7; int closedstatus = 8; int openstatus = 9; int opencommand = 10; int closecommand = 11; int stop = 12; int openstatusled = A0; int closedstatusled = A1; int movingstatusled = A2; boolean bstatus; boolean bismoving; boolean bstop; // int iroofclosed; // int iroofopen; //void stopall() void femergency() // Close all relays { digitalWrite(openmotoropenplus, LOW); // sets the digital pin as output digitalWrite(openmotoropenneg, LOW); // sets the digital pin as output digitalWrite(openmotorcloseplus, LOW); // sets the digital pin as output digitalWrite(openmotorcloseneg, LOW); // sets the digital pin as output digitalWrite(closemotoropenplus, LOW); // sets the digital pin as output digitalWrite(closemotoropenneg, LOW); // sets the digital pin as output digitalWrite(closemotorcloseplus, LOW); // sets the digital pin as output digitalWrite(closemotorcloseneg, LOW); // sets the digital pin as output } void fcloseroof() { digitalWrite(movingstatusled, HIGH); digitalWrite(closedstatusled, LOW); digitalWrite(openmotorcloseplus, HIGH); digitalWrite(openmotorcloseneg, HIGH); digitalWrite(closemotorcloseplus, HIGH); digitalWrite(closemotorcloseneg, HIGH); bismoving = true; } void fopenroof() { digitalWrite(movingstatusled, HIGH); digitalWrite(openmotoropenplus, HIGH); digitalWrite(openmotoropenneg, HIGH); digitalWrite(closemotoropenplus, HIGH); digitalWrite(closemotoropenneg, HIGH); digitalWrite(openstatusled, LOW); bismoving = true; } void setup() { pinMode(openmotoropenplus, OUTPUT); // sets the digital pin as output pinMode(openmotoropenneg, OUTPUT); // sets the digital pin as output pinMode(openmotorcloseplus, OUTPUT); // sets the digital pin as output pinMode(openmotorcloseneg, OUTPUT); // sets the digital pin as output pinMode(closemotoropenplus, OUTPUT); // sets the digital pin as output pinMode(closemotoropenneg, OUTPUT); // sets the digital pin as output pinMode(closemotorcloseplus, OUTPUT); // sets the digital pin as output pinMode(closemotorcloseneg, OUTPUT); // sets the digital pin as output pinMode(openstatusled, OUTPUT); // sets the analog pin as output pinMode(closedstatusled, OUTPUT); // sets the analog pin as output pinMode(movingstatusled, OUTPUT); // sets the analog pin as output // Check to see if roof is closed or open if (digitalRead(closedstatus) == LOW) //roof not fully closed { if (digitalRead(openstatus) == LOW) //roof not fully open { // not open or closed, close it to give program a reference point digitalWrite(movingstatusled, HIGH); //start "roof is moving" flasher delay(5000); // wait for 5 seconds fcloseroof(); //and close the roof (default) } } } void loop() { // put your main code here, to run repeatedly: if (digitalRead(stop) == HIGH)// Stop has been pressed { femergency(); bstop = HIGH; return; } if (bstop = HIGH) { return; } if (bismoving = true)// roof is either opening or closing { if (digitalRead(closedstatus) == HIGH)// fully closed switch has triggered { digitalWrite(movingstatusled, LOW); digitalWrite(closedstatusled, HIGH); digitalWrite(openmotorcloseplus, LOW); digitalWrite(openmotorcloseneg, LOW); digitalWrite(closemotorcloseplus, LOW); digitalWrite(closemotorcloseneg, LOW); bismoving = false; } if (digitalRead(openstatus) == HIGH)// fully open switch has triggered { digitalWrite(movingstatusled, LOW); digitalWrite(openstatusled, HIGH); digitalWrite(openmotoropenplus, LOW); digitalWrite(openmotoropenneg, LOW); digitalWrite(closemotoropenplus, LOW); digitalWrite(closemotoropenneg, LOW); bismoving = false; } } if (digitalRead(closecommand) == HIGH)// command to close roof has been received { fcloseroof(); } if (digitalRead(opencommand) == HIGH)//command to open roof has been received { fopenroof(); } }