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