|
| 1 | +#include <LiquidCrystal.h> |
| 2 | +LiquidCrystal lcd(8,9,4,5,6,7); |
| 3 | + |
| 4 | +// define some values used by the panel and buttons |
| 5 | +int lcd_key = 0; |
| 6 | +int adc_key_in = 0; |
| 7 | +#define btnRIGHT 0 |
| 8 | +#define btnUP 1 |
| 9 | +#define btnDOWN 2 |
| 10 | +#define btnLEFT 3 |
| 11 | +#define btnSELECT 4 |
| 12 | +#define btnNONE 5 |
| 13 | + |
| 14 | +const int Coil = 2; |
| 15 | +const int Starter = 3; |
| 16 | +const int GenStatus = 11;//=0 ---> gen is on |
| 17 | +const int RedLight = 12; //Red light |
| 18 | +bool ActualGenStatus; |
| 19 | +int GenStartIterations = 0;//# of times the system tries to |
| 20 | + |
| 21 | +//start the generator |
| 22 | +int RawVolts = 0; |
| 23 | +int BatteryMilliVolts = 0; |
| 24 | +int mapValue = 31000; |
| 25 | +int TimeDelay = 100; |
| 26 | +int SecondTimer = 0; |
| 27 | +int Starter_Timer = 0; |
| 28 | +int Stop_Timer = 0; |
| 29 | +int genStartTime = 25; //We want to change this value |
| 30 | + |
| 31 | +//Timers |
| 32 | +int timer200ms = 0; |
| 33 | +int timer2sec = 0; |
| 34 | +int timerDelay = 0; //Counter for the delay to reach the DelayTime value |
| 35 | + |
| 36 | +//Keeps track of the menu |
| 37 | +int Menu = 0; |
| 38 | +char* AutomationStatus = "N/A"; |
| 39 | +#define ChangeMinVolt 1 |
| 40 | +#define ChangeMaxVolt 2 |
| 41 | +#define ChangeDelay 3 |
| 42 | +#define ChangeStartDelay 4 |
| 43 | + |
| 44 | +//Values to change |
| 45 | +int MinimumVoltage = 11500; |
| 46 | +int MaximumVoltage = 13500; |
| 47 | +int DelayTime = 1 * 10; //The delay for the coils to start again ( seconds * 10) |
| 48 | + |
| 49 | +//Keeps track of the status of the signals |
| 50 | +bool CoilStatus = true; |
| 51 | +bool StarterStatus = true; |
| 52 | +int buttonPressed = 1; |
| 53 | +int prevButtonPressed = 1; |
| 54 | + |
| 55 | +//For setting the GenStartDelay while device is running |
| 56 | +int DelayHolder = 0; |
| 57 | + |
| 58 | + |
| 59 | +void setup() { |
| 60 | + pinMode(2,OUTPUT); |
| 61 | + pinMode(3,OUTPUT); |
| 62 | + pinMode(12,OUTPUT); //Red LED |
| 63 | + pinMode(11,INPUT_PULLUP); |
| 64 | + Serial.begin(9600); |
| 65 | + lcd.begin(16,2); |
| 66 | + lcd.setCursor(0,0); |
| 67 | + lcd.print("Battery= "); |
| 68 | + lcd.setCursor(14,0); |
| 69 | + lcd.print("mV"); |
| 70 | + lcd.setCursor(0,1); |
| 71 | + lcd.print("StartTime= "); |
| 72 | + lcd.print(DelayHolder / 10); |
| 73 | + lcd.print("s"); |
| 74 | + |
| 75 | + //Both Coil and Starter must be OFF at first |
| 76 | + digitalWrite(Coil, HIGH); |
| 77 | + digitalWrite(Starter, HIGH); |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | +void loop() { |
| 82 | + |
| 83 | + //If device is not ready then menu |
| 84 | + if(Menu <= 4) |
| 85 | + { |
| 86 | + DisplayMenu(); |
| 87 | + } else |
| 88 | + { |
| 89 | + readVolts(); //Continuously reads the voltage |
| 90 | + } |
| 91 | + |
| 92 | + delay(TimeDelay); |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +//Function for reading the voltage |
| 98 | +void readVolts() |
| 99 | +{ |
| 100 | + //lcd.clear(); |
| 101 | + lcd.setCursor(0,0); |
| 102 | + lcd_key = read_LCD_buttons(); |
| 103 | + RawVolts = analogRead(A1); //Reads the voltage from the generator |
| 104 | + BatteryMilliVolts = map(RawVolts,0,1023,0,mapValue); //Value is assigned to the variable |
| 105 | + |
| 106 | + ActualGenStatus = !(digitalRead(GenStatus)); |
| 107 | + Starter_Timer++; |
| 108 | + SecondTimer++; |
| 109 | + if (SecondTimer == 10) |
| 110 | + { |
| 111 | + |
| 112 | + lcd.print("Battery = "); |
| 113 | + lcd.print(BatteryMilliVolts / 1000); |
| 114 | + lcd.print("."); |
| 115 | + lcd.print((BatteryMilliVolts % 1000) / 100); |
| 116 | + lcd.print("V"); |
| 117 | + SecondTimer = 0; |
| 118 | + } |
| 119 | + |
| 120 | + if(!(CoilStatus == false and StarterStatus == false)) |
| 121 | + { |
| 122 | + if(lcd_key == btnUP) |
| 123 | + { |
| 124 | + DelayHolder += 10; |
| 125 | + } else if(lcd_key == btnDOWN) |
| 126 | + { |
| 127 | + DelayHolder -= 10; |
| 128 | + } |
| 129 | + } |
| 130 | + //additional feature to change the start time while the device is running |
| 131 | + if(CoilStatus == false and StarterStatus == false) |
| 132 | + { |
| 133 | + if(genStartTime != DelayHolder) |
| 134 | + { |
| 135 | + genStartTime = DelayHolder; |
| 136 | + } |
| 137 | + } |
| 138 | + lcd.setCursor (0, 1); |
| 139 | + lcd.print("Automation: "); |
| 140 | + lcd.print(AutomationStatus); |
| 141 | + |
| 142 | + if(BatteryMilliVolts < MinimumVoltage && ActualGenStatus == 0) { |
| 143 | + // Start Generator |
| 144 | + |
| 145 | + if(CoilStatus == true) |
| 146 | + { |
| 147 | + digitalWrite(Coil,LOW);//sets coil hot at 12 Volts |
| 148 | + CoilStatus = false; |
| 149 | + } |
| 150 | + //delay 200 ms |
| 151 | + if(timer200ms < 2) //This should delay it by 200ms (Works) |
| 152 | + { |
| 153 | + timer200ms++; |
| 154 | + return; //Using Return since its a function and can be changed to "break" when its in the loop |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + //see if generator is on |
| 159 | + //by checking ActualGenStatus from the beginning |
| 160 | + |
| 161 | + while(ActualGenStatus == 0 && GenStartIterations <= 2){ |
| 162 | + ActualGenStatus = !(digitalRead(GenStatus));//Checks the generator if its on |
| 163 | + |
| 164 | + digitalWrite(Starter,LOW); //turn on starter |
| 165 | + StarterStatus = false; |
| 166 | + //delay 2 seconds using timer2sec |
| 167 | + if(timer2sec < genStartTime) //Delay works |
| 168 | + { |
| 169 | + timer2sec++;//this delays 2 seconds for starter |
| 170 | + return; //Using Return since its a function and can be changed to "break" when its in the loop |
| 171 | + } |
| 172 | + |
| 173 | + digitalWrite(Starter,HIGH);//turn off starter |
| 174 | + StarterStatus = true; |
| 175 | + |
| 176 | + //digitalWrite(RedLight, HIGH);//Testing purposes |
| 177 | + |
| 178 | + |
| 179 | + //delay 15 seconds using timer30sec |
| 180 | + if(timerDelay < DelayTime) |
| 181 | + { |
| 182 | + timerDelay++; |
| 183 | + return; |
| 184 | + } |
| 185 | + ActualGenStatus = !(digitalRead(GenStatus));//read generator status (on or off) |
| 186 | + if(ActualGenStatus == 1){ |
| 187 | + GenStartIterations = 0; |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + GenStartIterations++; |
| 192 | + |
| 193 | + timer200ms = 0; //Resets the value of 1st timer |
| 194 | + timer2sec = 0; |
| 195 | + timerDelay = 0; |
| 196 | + |
| 197 | + |
| 198 | + }//bracket for while loop |
| 199 | + |
| 200 | + if(ActualGenStatus == 0 && GenStartIterations == 3){ |
| 201 | + //KILL THE PROGRAM, END IT!!!//turn red light on! |
| 202 | + digitalWrite(Coil, HIGH); //Coil is off |
| 203 | + CoilStatus = true; |
| 204 | + digitalWrite(RedLight, HIGH); //Turns on Red LED |
| 205 | + while(1){ |
| 206 | + |
| 207 | + StarterFailure(); |
| 208 | + }; //Stops the program |
| 209 | + } |
| 210 | + |
| 211 | + |
| 212 | + GenStartIterations = 0; |
| 213 | + Starter_Timer = 0; |
| 214 | + } else if(BatteryMilliVolts > MaximumVoltage && ActualGenStatus == 1) { |
| 215 | + GenStartIterations = 0; |
| 216 | + digitalWrite(Coil, HIGH); |
| 217 | + CoilStatus = true; |
| 218 | + digitalWrite(Starter, HIGH); |
| 219 | + StarterStatus = true; |
| 220 | + // Stop Generator |
| 221 | + } |
| 222 | + ActualGenStatus = !(digitalRead(GenStatus)); |
| 223 | + if(ActualGenStatus == 1){ |
| 224 | + digitalWrite(Starter, HIGH); |
| 225 | + StarterStatus = true; |
| 226 | + GenStartIterations = 0; |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +//Displays a message to let the user know that the system failed |
| 231 | +void StarterFailure() |
| 232 | +{ |
| 233 | + lcd.clear(); |
| 234 | + lcd.setCursor(0,0); |
| 235 | + lcd.print("Starter Failure!"); |
| 236 | + delay(1000); |
| 237 | +} |
| 238 | + |
| 239 | +void DisplayMenu() |
| 240 | +{ |
| 241 | + lcd.clear(); |
| 242 | + lcd.setCursor(0,0); |
| 243 | + |
| 244 | + lcd_key = read_LCD_buttons(); |
| 245 | + |
| 246 | + //btnNONE - indicates if no buttons are pressed |
| 247 | + if(lcd_key == btnNONE) |
| 248 | + { |
| 249 | + prevButtonPressed = buttonPressed; |
| 250 | + buttonPressed = 1; |
| 251 | + } else |
| 252 | + { |
| 253 | + prevButtonPressed = buttonPressed; |
| 254 | + buttonPressed = 0; |
| 255 | + } |
| 256 | + |
| 257 | + if((prevButtonPressed == 0 && buttonPressed == 0)) |
| 258 | + { |
| 259 | + return; |
| 260 | + } |
| 261 | + |
| 262 | + |
| 263 | + if(Menu == 0) |
| 264 | + { |
| 265 | + lcd.print("Select to start!"); |
| 266 | + if(lcd_key == btnSELECT) //When Select is pressed, then we can start the program |
| 267 | + { |
| 268 | + Menu++; //Goes to the next menu |
| 269 | + } |
| 270 | + } else if(Menu == ChangeMinVolt) |
| 271 | + { |
| 272 | + lcd.print("Min Volt: "); |
| 273 | + lcd.setCursor(0,1); |
| 274 | + lcd.print(MinimumVoltage/1000); |
| 275 | + lcd.print("."); |
| 276 | + lcd.print((MinimumVoltage%1000) / 100); |
| 277 | + lcd.print("V"); |
| 278 | + if(lcd_key == btnUP) //Increases the Minimum Voltage by 100 |
| 279 | + { |
| 280 | + MinimumVoltage = MinimumVoltage + 100; |
| 281 | + if(MinimumVoltage > 12000) //Ceiling |
| 282 | + {MinimumVoltage = 12000;} |
| 283 | + } else if (lcd_key == btnDOWN) //Decreases the Minimum Voltage by 100 |
| 284 | + { |
| 285 | + MinimumVoltage = MinimumVoltage - 100; |
| 286 | + if(MinimumVoltage < 11000) //Floor |
| 287 | + {MinimumVoltage = 11000;} |
| 288 | + } else if (lcd_key == btnRIGHT) //Goes to the next value to change |
| 289 | + { |
| 290 | + Menu++; //Menu is now equals to 2 |
| 291 | + } |
| 292 | + } else if (Menu == ChangeMaxVolt) |
| 293 | + { |
| 294 | + lcd.print("Max Volt: "); |
| 295 | + lcd.setCursor(0,1); |
| 296 | + lcd.print(MaximumVoltage/1000); |
| 297 | + lcd.print("."); |
| 298 | + lcd.print((MaximumVoltage%1000)/ 100 ); |
| 299 | + lcd.print("V"); |
| 300 | + if(lcd_key == btnUP) //Increases the Maximum Voltage by 100 |
| 301 | + { |
| 302 | + MaximumVoltage = MaximumVoltage + 100; |
| 303 | + if(MaximumVoltage > 14000) //Ceiling |
| 304 | + {MaximumVoltage = 14000;} |
| 305 | + } else if (lcd_key == btnDOWN) //Decreases the Minimum Voltage by 100 |
| 306 | + { |
| 307 | + MaximumVoltage = MaximumVoltage - 100; |
| 308 | + if(MaximumVoltage < 13000) //Floor |
| 309 | + {MaximumVoltage = 13000;} |
| 310 | + } else if (lcd_key == btnRIGHT) //Goes to the next value to change |
| 311 | + { |
| 312 | + Menu++; //Menu is now equals to 3 |
| 313 | + } else if (lcd_key == btnLEFT) //Goes back to prev value to change |
| 314 | + { |
| 315 | + Menu--; //Menu is now equals to 2 |
| 316 | + } |
| 317 | + } else if(Menu == ChangeDelay) |
| 318 | + { |
| 319 | +//The actual delay time should be 20 - 60 seconds |
| 320 | +//We are changing it for testing purposes |
| 321 | + |
| 322 | + lcd.print("Delay Time: "); |
| 323 | + lcd.setCursor(0,1); |
| 324 | + lcd.print(DelayTime / 10); //Displays it in seconds |
| 325 | + lcd.print("s"); |
| 326 | + if(lcd_key == btnUP) |
| 327 | + { |
| 328 | + DelayTime = DelayTime + 10; |
| 329 | + } else if(lcd_key == btnDOWN) |
| 330 | + { |
| 331 | + DelayTime = DelayTime - 10; |
| 332 | + } else if(lcd_key == btnRIGHT) |
| 333 | + { |
| 334 | + Menu++; //Menu is now equal to 4 |
| 335 | + lcd.clear(); |
| 336 | + } else if(lcd_key == btnLEFT) |
| 337 | + { |
| 338 | + Menu--; //Menu is now equal to 3 |
| 339 | + } |
| 340 | + } else if(Menu == ChangeStartDelay) |
| 341 | + { |
| 342 | +//The actual genStartTime should be 2 - 3 seconds in increments of 0.1 seconds |
| 343 | +//We are changing it for testing purposes |
| 344 | + lcd.print("Gen Start Time: "); |
| 345 | + lcd.setCursor(0,1); |
| 346 | + lcd.print(genStartTime / 10); |
| 347 | + lcd.print("."); |
| 348 | + lcd.print(genStartTime % 10); |
| 349 | + lcd.print("s"); |
| 350 | + if(lcd_key == btnUP) |
| 351 | + { |
| 352 | + genStartTime = genStartTime + 1; |
| 353 | + if(genStartTime > 30) //Ceiling |
| 354 | + {genStartTime = 30;} |
| 355 | + } else if(lcd_key == btnDOWN) |
| 356 | + { |
| 357 | + genStartTime = genStartTime - 1; |
| 358 | + if(genStartTime < 20)//Floor |
| 359 | + {genStartTime = 20;} |
| 360 | + } else if(lcd_key == btnRIGHT) |
| 361 | + { |
| 362 | + Menu++; //Menu is now equal to 4 |
| 363 | + DelayHolder = genStartTime; |
| 364 | + lcd.clear(); |
| 365 | + } else if(lcd_key == btnLEFT) |
| 366 | + { |
| 367 | + Menu--; //Menu is now equal to 3 |
| 368 | + } |
| 369 | + } |
| 370 | +} |
| 371 | + |
| 372 | +// read the buttons |
| 373 | +int read_LCD_buttons() |
| 374 | +{ |
| 375 | + adc_key_in = analogRead(0); // read the value from the sensor |
| 376 | + if (adc_key_in > 1000) return btnNONE; |
| 377 | + if (adc_key_in < 50) return btnRIGHT; |
| 378 | + if (adc_key_in < 250) return btnUP; |
| 379 | + if (adc_key_in < 450) return btnDOWN; |
| 380 | + if (adc_key_in < 650) return btnLEFT; |
| 381 | + if (adc_key_in < 850) return btnSELECT; |
| 382 | + |
| 383 | + return btnNONE; // when all others fail, return this... |
| 384 | +} |
0 commit comments