Components and supplies
Arduino UNO
Fan Accessory, AC Motor
Arduino 4 Relays Shield
Project description
Code
Arduino sketch
arduino
1String serialin; //incoming serial data 2String str; //store the state of opened/closed/safe pins 3 4//relays 5#define stop 4 6#define sensor 5 7#define close 6 8#define open 7 9 10//input sensors 11#define opened 11 // roof open sensor 12#define closed 12 // roof closed sensor 13#define safe 13 // scope safety sensor 14 15int led = 10; // the pin the scope safe LED is connected to 16 17unsigned long end_time; 18bool lost = false; //roof not reporting state 19 20void setup() { 21end_time=millis()+60000; //roof lost reset timer ~60 seconds change to suit your rquirments to determine if roof is lost 22 23 //Begin Serial Comunication(configured for 9600baud) 24 Serial.begin(9600); 25 //pin relay as OUTPUT 26 pinMode(open, OUTPUT); 27 pinMode(close, OUTPUT); 28 pinMode(stop, OUTPUT); 29 pinMode(sensor, OUTPUT); 30 31 //pins as INPUT 32 pinMode(closed, INPUT_PULLUP); 33 pinMode(opened, INPUT_PULLUP); 34 pinMode(safe, INPUT_PULLUP); 35 36 //Relay state 37 digitalWrite(open,LOW); 38 digitalWrite(close,LOW); 39 digitalWrite(stop,LOW); 40 digitalWrite(sensor,LOW); 41 42 Serial.write("RRCI#"); //init string 43 44} 45 46void loop() { 47 Timer(); 48 49 if (digitalRead(safe) == LOW){ 50 51 digitalWrite(led, HIGH); // Turn the LED on 52 } 53 else if (digitalRead(safe) == HIGH){ 54 55 digitalWrite(led, LOW); // Turn the LED on 56 } 57 58 //Verify connection by serial 59 while (Serial.available()>0) { 60 //Read Serial data and alocate on serialin 61 62 serialin = Serial.readStringUntil('#'); 63 64 65 if (serialin == "on"){ // turn scope sensor on 66 digitalWrite(sensor,HIGH); 67 68 } 69 70 if (serialin == "off"){ // turn scope sensor off 71 digitalWrite(sensor,LOW); 72 73 } 74 75 if (serialin == "x"){ 76 digitalWrite(open,LOW); 77 78 } 79 80 else if (serialin == "open"){ 81 if (digitalRead(sensor) == LOW){//turn on IR scope sensor 82 digitalWrite(sensor,HIGH); 83 } 84 delay(1000); 85 if (digitalRead(safe) == LOW){//open only if scope safe 86 digitalWrite(open,HIGH); 87 //delay(1000); 88 digitalWrite(sensor,LOW); 89 } 90 } 91 if (serialin == "y"){ 92 digitalWrite(close,LOW); 93 94 } 95 96 else if (serialin == "close"){ 97 if (digitalRead(sensor) == LOW){//turn on IR scope sensor 98 digitalWrite(sensor,HIGH); 99 } 100 delay(1000); 101 102 103 if (digitalRead(safe) == LOW){//close only if scope safe 104 digitalWrite(close,HIGH); 105 //delay(1000); 106 digitalWrite(sensor,LOW); 107 } 108 109 } 110 } 111 112 if (digitalRead(closed) == LOW) { //stop relays when 'closed' reached 113 if (digitalRead(close) == HIGH){//relays low if it was high 114 digitalWrite(close,LOW); 115 } 116 117 } 118if (digitalRead(opened) == LOW) { //stop relays when 'opened' reached 119 120 if (digitalRead(open) == HIGH){//relays low if it was high 121 digitalWrite(open,LOW); 122 } 123 } 124if (serialin == "Parkstatus"){ // exteranl query command to fetch RRCI data 125 126 Serial.println("0#"); 127 serialin = ""; 128 } 129 130if (serialin == "get"){ // exteranl query command to fetch RRCI data - Two Pipelines(||) to make a boolean OR Comparission 131 132 if (digitalRead(opened) == LOW){ 133 str += "opened,"; 134 135 } 136 else if (digitalRead(closed) == LOW){ 137 str += "closed,"; 138 } 139 140 if ((digitalRead(closed) == HIGH) && (digitalRead(opened) == HIGH)){ 141 str += "unknown,"; 142 } 143 144 if (digitalRead(safe) == LOW){ 145 str += "safe,"; 146 147 } 148 else if (digitalRead(safe) == HIGH){ 149 str += "unsafe,"; 150 151 } 152 153 if ((digitalRead(closed) == HIGH) && (digitalRead(opened) == LOW) && (lost == false)){ 154 str += "not_moving_o#"; 155 end_time=millis()+60000; //reset the timer 156 } 157 158 if ((digitalRead(closed) == LOW) && (digitalRead(opened) == HIGH) && (lost == false)){ 159 str += "not_moving_c#"; 160 end_time=millis()+60000; //reset the timer 161 } 162 if ((digitalRead(closed) == HIGH) && (digitalRead(opened) == HIGH) && (lost == false)){ 163 str += "moving#"; 164 165 } 166 else if ((digitalRead(closed) == HIGH) && (digitalRead(opened) == HIGH) && (lost == true)){ 167 str += "unknown#"; 168 } 169 if (str.endsWith(",")) { 170 str += "unknown#"; 171 } 172 173 Serial.println(str); //send serial data 174 serialin = ""; 175 str = ""; 176//delay(100); 177 178} 179 if (serialin == "Status"){ 180 Serial.println("RoofOpen#"); 181 182 } 183serialin = ""; 184//str = ""; 185 } 186 187void Timer(){ // detect roof lost 188 if(millis()>=end_time){ 189 //60 s have passed!! 190 if ((digitalRead(closed) == HIGH) && (digitalRead(opened)) == HIGH){ 191 lost = true; //where the heck is the roof position? 192 } 193 194 195 } 196 if ((digitalRead(closed) == LOW) or (digitalRead(opened)) == LOW){ 197 lost = false; //roof state is known 198 end_time=millis()+60000; //reset the timer 199 } 200 201}
Downloadable files
Fritzing Drawing
Fritzing Drawing
Aleko Board
Aleko Board
Fritzing Drawing
Fritzing Drawing
Aleko Board
Aleko Board
Comments
Only logged in users can leave comments
cfar
3 Followers
•0 Projects
0