Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.05 KB | None | 0 0
  1. // Car alarm project G3orG3 @2016
  2.  
  3. /*
  4. TO ADD:
  5. -MORE PINS
  6. -RGB LED
  7. -music visualizer
  8. -zener diodes
  9. -voltage down using 2 resistors
  10. -
  11.  
  12. EXPANDER CONNECTIONS:
  13. 11 cs/ss
  14. 12 sck
  15. 13 mosi
  16. 14 miso
  17.  
  18. // Connect pins #15, 16 and 17 of the expander to ground (address selection)
  19. // Connect pin #9 of the expander to 5V (power)
  20. // Connect pin #10 of the expander to ground (common ground)
  21. // Connect pin #18 through a ~10kohm resistor to 5V (reset pin, active low)
  22.  
  23.  
  24.  
  25.  
  26. OUTSIDE - need to be connected outsite the box
  27. INSIDE - inside with the arduino
  28.  
  29. NEED UPDATE
  30. PINOUT:
  31. 0-(TX) bluetooth
  32. 1-(RX) bluetooth
  33. 2-Expander 11 (cip select) ss/cs
  34. 3-(PWM)CAR-MOSFET-PWM-USAlights (OUTPUT)
  35. 4-BOARD-BUZZER
  36. 5-(PWM)CAR-MOSFET-PWM-LEDstrip-RED (OUTPUT)
  37. 6-(PWM)
  38. 7-RST (INPUT)
  39. 8-SDA (INPUT) cip select
  40.  
  41. 9-(PWM)CAR-MOSFET-PWM-LEDstrip-BLUE (OUTPUT)
  42. 10-(PWM)CAR-MOSFET-PWM-LEDstrip-GREEN (OUTPUT)
  43. 11-(PWM)RFID-MOSI + Expander 13(INPUT)
  44. 12-MISO + Expander 14 (INPUT)j
  45. 13-SCK + Expander 12 (INPUT)
  46.  
  47. 0-
  48. 1-
  49. 2-POTENTIOMETER for menu, dimming (INPUT)
  50. 3-CAR-VOLTAGE-determiner (scaled down) (INPUT)
  51. 4-LCD+EXPANDER-SDA (INPUT)
  52. 5-LCD+EXPANDER-SCL (INPUT)
  53. 6-SENSOR-temperature (INPUT)
  54. 7-
  55.  
  56. EXPANDER A
  57. 8-BUTTON-More/custom/ok
  58. 7-SWITCH-led Strip (INPUT)
  59. 6-SWITCH-USA lights (INPUT)
  60. 5-SWITCH-alarm (INPUT)
  61. 4-SENSOR-piezo (INPUT)
  62. 3-SENSOR-PIR (INPUT)
  63. 2-
  64. 1-
  65.  
  66. EXPANDER B
  67. 9-CAR-NPN-lock (OUTPUT)
  68. 10-CAR-NPN-unlock (OUTPUT)
  69. 11-CAR-NPN-alarmhorn (OUTPUT)
  70. 12-CAR-NPN-truck-open (OUTPUT)
  71. 13-CAR-RELAY(12V line - car is locked ) (INPUT)
  72. 14-CAR-IGNITION(12V line - engine is started) (INPUT)
  73. 15-CAR-RELAY-RADIO (OUTPUT)
  74. 16-
  75.  
  76. */
  77.  
  78. // INCLUDING / DEFINE LIBRARYs
  79.  
  80.  
  81.  
  82. #include <Wire.h>
  83. #include <LiquidCrystal_I2C.h>
  84. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
  85. #include <LCD.h>
  86. #include <LcdBarGraphX.h>
  87. #include <EEPROM.h>
  88. #include <SPI.h> // We use this library, so it must be called here.
  89. #include <MCP23S17.h> // Here is the new class to make using the MCP23S17 easy.
  90. MCP mcp(0, 2);
  91. //#include <SoftwareSerial.h>
  92.  
  93. //SoftwareSerial BTSerial(8, A1); // RX | TX
  94.  
  95. //led strip
  96. const int ledPin = 6; //led strip toggler //EXPANDER
  97. const int ledRPin = 5; //led RED
  98. const int ledGPin = 10; //led GREEN
  99. const int ledBPin = 9; //led BLUE
  100.  
  101. //USA lights
  102. const int usaPin = 7; // USA LIGHTS toggler //EXPANDER
  103. const int usaoutPin = 3; // USA LIGHTS
  104.  
  105.  
  106. //truck unlocker
  107. const int truckopenPin = 12; // truck unlocker MOTOR //EXPANDER
  108. // sensor pin
  109.  
  110. //alarm + locker
  111. const int sensorVPin = 3; // piezo vibration sensor module //EXPANDER
  112. const int alarmPin = 5; // ALARM toggler //EXPANDER
  113. const int lockPin = 9; // pin to the transistor who lock the door //EXPANDER
  114. const int unlockPin = 10; // pin to the transistor who unlock the door //EXPANDER
  115. const int carlockPin = 13; // car locked ? //EXPANDER
  116. const int contactPin = 14; // car contact on ? //EXPANDER
  117. const int triggerPin = 11; // horn, lights, car wipers ... //EXPANDER
  118.  
  119.  
  120. //more
  121. const int potPin = A2; // potentiometer
  122. const int buzzerPin = 4; // buzzer
  123. const int tempPin = A6; // temperature
  124. const int voltagePin = A7; // car volage
  125. const int buttonPin = 8; // button //EXPANDER
  126. const int blkey = 16; //EXPANDER
  127. const int blon = 2; //EXPANDER
  128.  
  129.  
  130. // Settings
  131. int redVal = 0; // Variables to store the values to send to the pins
  132. int grnVal = 0;
  133. int bluVal = 0;
  134. int truckVal = 0;
  135. // int RFIDVal = 1;
  136.  
  137. int alarmtime = 15; // time for alarm to turn off IN SECOUNDS
  138. int timeToKnock = 3; // amout of time to hear knocks in SECOUNDS !
  139. int knocks = 3; // how many knocks
  140. int usalight = 50; // % of the lights
  141. int ledlight = 55; //% of the interior light
  142.  
  143.  
  144. //progress bars
  145. LcdBarGraphX lbg(&lcd, 7, 9, 1);
  146. LcdBarGraphX lbg1(&lcd, 7, 0, 1);
  147. LcdBarGraphX lbg2(&lcd, 16, 0, 1);
  148. LcdBarGraphX lbg3(&lcd, 16, 0, 0);
  149.  
  150. //progressbar animation
  151. void filldown() {
  152. for (int i = 0; i < 81; i++)
  153. {
  154. lbg2.drawValue( i, 80);
  155. delay(3);
  156. }
  157. }
  158.  
  159. //progressbar animation
  160. void fillup() {
  161. for (int i = 0; i < 81; i++)
  162. {
  163. lbg3.drawValue( i, 80);
  164. delay(3);
  165. }
  166. }
  167.  
  168. //custom car animation
  169. byte off[8] = {
  170. 0b00000,
  171. 0b10001,
  172. 0b10001,
  173. 0b01010,
  174. 0b00100,
  175. 0b01010,
  176. 0b10001,
  177. 0b10001
  178. };
  179. byte on[8] = {
  180. 0b00000,
  181. 0b00001,
  182. 0b00010,
  183. 0b10100,
  184. 0b11000,
  185. 0b10000,
  186. 0b00000,
  187. 0b00000
  188. };
  189.  
  190. #define NOTE_B0 31
  191. #define NOTE_C1 33
  192. #define NOTE_CS1 35
  193. #define NOTE_D1 37
  194. #define NOTE_DS1 39
  195. #define NOTE_E1 41
  196. #define NOTE_F1 44
  197. #define NOTE_FS1 46
  198. #define NOTE_G1 49
  199. #define NOTE_GS1 52
  200. #define NOTE_A1 55
  201. #define NOTE_AS1 58
  202. #define NOTE_B1 62
  203. #define NOTE_C2 65
  204. #define NOTE_CS2 69
  205. #define NOTE_D2 73
  206. #define NOTE_DS2 78
  207. #define NOTE_E2 82
  208. #define NOTE_F2 87
  209. #define NOTE_FS2 93
  210. #define NOTE_G2 98
  211. #define NOTE_GS2 104
  212. #define NOTE_A2 110
  213. #define NOTE_AS2 117
  214. #define NOTE_B2 123
  215. #define NOTE_C3 131
  216. #define NOTE_CS3 139
  217. #define NOTE_D3 147
  218. #define NOTE_DS3 156
  219. #define NOTE_E3 165
  220. #define NOTE_F3 175
  221. #define NOTE_FS3 185
  222. #define NOTE_G3 196
  223. #define NOTE_GS3 208
  224. #define NOTE_A3 220
  225. #define NOTE_AS3 233
  226. #define NOTE_B3 247
  227. #define NOTE_C4 262
  228. #define NOTE_CS4 277
  229. #define NOTE_D4 294
  230. #define NOTE_DS4 311
  231. #define NOTE_E4 330
  232. #define NOTE_F4 349
  233. #define NOTE_FS4 370
  234. #define NOTE_G4 392
  235. #define NOTE_GS4 415
  236. #define NOTE_A4 440
  237. #define NOTE_AS4 466
  238. #define NOTE_B4 494
  239. #define NOTE_C5 523
  240. #define NOTE_CS5 554
  241. #define NOTE_D5 587
  242. #define NOTE_DS5 622
  243. #define NOTE_E5 659
  244. #define NOTE_F5 698
  245. #define NOTE_FS5 740
  246. #define NOTE_G5 784
  247. #define NOTE_GS5 831
  248. #define NOTE_A5 880
  249. #define NOTE_AS5 932
  250. #define NOTE_B5 988
  251. #define NOTE_C6 1047
  252. #define NOTE_CS6 1109
  253. #define NOTE_D6 1175
  254. #define NOTE_DS6 1245
  255. #define NOTE_E6 1319
  256. #define NOTE_F6 1397
  257. #define NOTE_FS6 1480
  258. #define NOTE_G6 1568
  259. #define NOTE_GS6 1661
  260. #define NOTE_A6 1760
  261. #define NOTE_AS6 1865
  262. #define NOTE_B6 1976
  263. #define NOTE_C7 2093
  264. #define NOTE_CS7 2217
  265. #define NOTE_D7 2349
  266. #define NOTE_DS7 2489
  267. #define NOTE_E7 2637
  268. #define NOTE_F7 2794
  269. #define NOTE_FS7 2960
  270. #define NOTE_G7 3136
  271. #define NOTE_GS7 3322
  272. #define NOTE_A7 3520
  273. #define NOTE_AS7 3729
  274. #define NOTE_B7 3951
  275. #define NOTE_C8 4186
  276. #define NOTE_CS8 4435
  277. #define NOTE_D8 4699
  278. #define NOTE_DS8 4978
  279.  
  280.  
  281. // 2226,f8,949be1 -Geo phone
  282. // AT+RNAME?<PR> OK
  283. // AT+FSAD=<PR> OK
  284.  
  285. void setup() {
  286.  
  287. lcd.setCursor(1, 0); //Start at character 4 on line 0
  288. lcd.print("Status:");
  289.  
  290. //``````````````` LCD EXPANDER
  291.  
  292.  
  293. lcd.begin(16, 2); // initialize the lcd for 16 chars 2 lines, turn on backlight
  294. SPI.begin(); // start the SPI library
  295. mcp.begin(); // expander bank A / 0
  296. lcd.createChar(7, on); //create custom car
  297. lcd.createChar(8, off); //create custom car
  298.  
  299. lcd.setCursor(0, 1); //Start at character 4 on line 0
  300. lcd.print("Starting LCD,MCP");
  301. delay(1000);
  302. tone(buzzerPin, NOTE_B5, 100);
  303.  
  304.  
  305. //```````````````BLUETOOTH
  306. lcd.setCursor(0, 1); //Start at character 4 on line 0
  307. lcd.print("Check Bluetooth");
  308. delay(500);
  309. tone(buzzerPin, NOTE_B5, 100);
  310.  
  311.  
  312. mcp.pinMode(blon, OUTPUT);
  313. mcp.digitalWrite(blon, HIGH); //power on the bluetooth
  314. mcp.pinMode(blkey, OUTPUT);
  315. mcp.digitalWrite(blkey, HIGH); //power on AT COMMAND on bluetooth
  316. delay(100);
  317. // BTSerial.begin(38400); // HC-05 default speed in AT command more
  318.  
  319. delay(100);
  320.  
  321. // BTSerial.write("AT+NAME"); //example command
  322. //if (BTSerial.available()) {
  323. // lcd.print(BTSerial.read());
  324. // }
  325. // BTSerial.findBaud(); // search for bluetooth band
  326.  
  327.  
  328. /*
  329. // Play 1-up sound
  330. tone(buzzerPin, NOTE_E6, 125);
  331. delay(130);
  332. tone(buzzerPin, NOTE_G6, 125);
  333. delay(130);
  334. tone(buzzerPin, NOTE_E7, 125);
  335. delay(130);
  336. tone(buzzerPin, NOTE_C7, 125);
  337. delay(130);
  338. tone(buzzerPin, NOTE_D7, 125);
  339. delay(130);
  340. tone(buzzerPin, NOTE_G7, 125);
  341. */
  342.  
  343. // for (int i = 0; i < 20; i++) //Start-up animation
  344. // {
  345. // lcd.backlight();
  346. // delay(i * 4);
  347. // lcd.noBacklight();
  348. // delay(i * 4);
  349. // }
  350. // lcd.backlight(); // finish with backlight on
  351.  
  352.  
  353.  
  354. //```````````````PINS
  355. lcd.setCursor(0, 1); //Start at character 4 on line 0
  356. lcd.print("Register pins");
  357. delay(500);
  358. tone(buzzerPin, NOTE_B5, 100);
  359.  
  360. //set the pins from arduino
  361. pinMode(buzzerPin, OUTPUT);
  362. pinMode(ledRPin, OUTPUT);
  363. pinMode(ledGPin, OUTPUT);
  364. pinMode(ledBPin, OUTPUT);
  365. pinMode(potPin, INPUT);
  366. // pinMode(micPin, INPUT);
  367. // pinMode(sMicPin, INPUT);
  368. pinMode(voltagePin, INPUT);
  369. pinMode(tempPin, INPUT);
  370. pinMode(usaoutPin, OUTPUT);
  371.  
  372. //set the pins from expander 0
  373.  
  374. mcp.pinMode(lockPin, OUTPUT);
  375. mcp.pinMode(unlockPin, OUTPUT);
  376. mcp.pinMode(triggerPin, OUTPUT);
  377. mcp.pinMode(truckopenPin, OUTPUT);
  378. mcp.pinMode(carlockPin, INPUT);
  379. mcp.pinMode(contactPin, INPUT);
  380.  
  381. // mcp.pinMode() radio relay
  382.  
  383. //set the pins from expander 1
  384. mcp.pinMode(buttonPin, INPUT);
  385. mcp.pullupMode(buttonPin, HIGH);
  386. mcp.pinMode(alarmPin, INPUT);
  387. mcp.pullupMode(alarmPin, HIGH);
  388. mcp.pinMode(usaPin, INPUT);
  389. mcp.pullupMode(usaPin, HIGH);
  390. mcp.pinMode(ledPin, INPUT);
  391. mcp.pullupMode(ledPin, HIGH);
  392.  
  393. mcp.pinMode(sensorVPin, INPUT);
  394. // mcp.pinMode(sensorPPin, INPUT);
  395.  
  396.  
  397. lcd.setCursor(0, 1); //Start at character 4 on line 0
  398. lcd.print("Restore settings");
  399. delay(500);
  400. tone(buzzerPin, NOTE_B5, 100);
  401.  
  402. /*
  403. // check();
  404. tone(buzzerPin, NOTE_G7, 125);
  405. delay(100);
  406.  
  407. // RESTORE SETTINGS
  408. redVal = EEPROM.read(0);
  409. grnVal = EEPROM.read(1);
  410. bluVal = EEPROM.read(2);
  411. truckVal = EEPROM.read(3);
  412. //RFIDVal = EEPROM.read(4);
  413. alarmtime = EEPROM.read(5);
  414. timeToKnock = EEPROM.read(6);
  415. knocks = EEPROM.read(7);
  416. usalight = EEPROM.read(8);
  417. ledlight = EEPROM.read(9);
  418.  
  419. delay(200);
  420. // analogWrite(ledRPin, redVal); // Write values to LED pins
  421. // analogWrite(ledGPin, grnVal);
  422. // analogWrite(ledBPin, bluVal);
  423.  
  424. tone(buzzerPin, NOTE_G7, 125);
  425. lcd.clear();
  426. delay(100);
  427. */
  428.  
  429.  
  430. lcd.setCursor(0, 1); //Start at character 4 on line 0
  431. lcd.print("All Done!");
  432. delay(500);
  433. tone(buzzerPin, NOTE_B5, 100);
  434.  
  435. }
  436.  
  437.  
  438. void unlock_door()
  439. {
  440. mcp.digitalWrite(unlockPin, HIGH);
  441. delay(200);
  442. mcp.digitalWrite(unlockPin, LOW);
  443. delay(1000);
  444. }
  445.  
  446. void lock_door()
  447. {
  448. mcp.digitalWrite(lockPin, HIGH);
  449. delay(200);
  450. mcp.digitalWrite(lockPin, LOW);
  451. delay(1000);
  452. }
  453.  
  454. int usaStatus = 0; //usa toggler status
  455. int ledStatus = 0; //led toggler status
  456. int alarmStatus = 0; //alarm toggler status
  457. //int rfidStatus = 0; //
  458. int menudisplaymode = 0; // used for percente / progressbar
  459. int clicktime = (2500 / 5 / 100); //time to enter menu/select
  460. int menupage = 0; // menu page
  461. int menusel = 0; // menu selected item
  462. int menumode = 0;
  463. int menudone = 0; //the value of the setting that must be applied
  464. int before = 0; //used to enter settings old pot
  465. int beforepot = 0; //used to navigate in homepage old pot
  466. int cur = 0;
  467. int count = 0;
  468. int idlecount = 0;
  469. int potVal = 0;
  470. int settingspos = 0; //cursor pos for settings
  471. int lcdf = 0; //used for lcd clearing function
  472. int lcdclear = 0; //used for clear lcd timer
  473. int active = 1; //used to know if the homepage is active or not
  474. int alarmtrig = 0; //alarm triggered
  475.  
  476. int blcon = 0; // is the bluetooth connected to a device ?
  477. int blinterval = 0; // bluetooth function interval
  478. int blstate = 0; // 0 scan 1 receive
  479.  
  480.  
  481.  
  482. /* --------------EEPROM
  483. EEPROM.write(addr, val);
  484. EEPROM.read(address);
  485.  
  486. ADDRs -
  487.  
  488. -HOMEPAGE
  489. 0-red
  490. 1-green
  491. 2-blue
  492. 3-truckVal 1/0
  493. 4-RFIDVal 1/0
  494. 5-alarmtime
  495. 6-timeToKnock
  496. 7-knocks
  497. 8-usalight
  498. 9-ledlight
  499.  
  500.  
  501. */
  502. void click_sound() {
  503. tone(buzzerPin, NOTE_B5, 100);
  504. }
  505.  
  506. void toggle_sound() {
  507. tone(buzzerPin, 5000, 50);
  508. }
  509.  
  510. void error_sound() {
  511. tone(buzzerPin, 1000, 200);
  512. }
  513.  
  514.  
  515. void loop()
  516. {
  517. delay(100);
  518.  
  519. //BEGIN HOMEPAGES !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  520. if (lcdf == 0) {
  521. if (menumode <= 0) {
  522.  
  523. if (active == 1) {
  524. if (menumode == 0) { //Homepage 0 -- temperature + lock/unlock
  525. lcd.backlight();
  526. lcd.setCursor(0, 1);
  527. if (mcp.digitalRead(carlockPin) == LOW) {
  528. lcd.print("<Incuie masina>");
  529. if (mcp.digitalRead(buttonPin) == LOW) {
  530. lcd.setCursor(0, 1);
  531. toggle_sound();
  532. lcd.print("Incuiata!! ");
  533. lock_door();
  534. delay(500);
  535. }
  536. }
  537. else {
  538. lcd.print("<Descuie masina>");
  539. if (mcp.digitalRead(buttonPin) == LOW) {
  540. unlock_door();
  541. toggle_sound();
  542. lcd.setCursor(0, 1);
  543. lcd.print("Descuiata!! ");
  544. delay(500);
  545. }
  546. }
  547.  
  548. lcd.setCursor(0, 0);
  549. if (blcon = 0) {
  550. lcd.print("BL");
  551. lcd.write(8);
  552. }
  553. else {
  554. lcd.print("BL");
  555. lcd.write(7);
  556. }
  557. lcd.print(" ");
  558. if (mcp.digitalRead(contactPin) == LOW) {
  559. lcd.print("CAR");
  560. lcd.write(7);
  561. }
  562. else {
  563. lcd.print("CAR");
  564. lcd.write(8);
  565. }
  566. lcd.print(" ");
  567.  
  568. float sensor = analogRead(tempPin);
  569. float volt = (5.0 * sensor) / 1024.0;
  570. float milivolt = volt * 100;
  571. int c = milivolt - 273.15;
  572. // c = c - 16; //calibration
  573. lcd.print(c);
  574. lcd.print(char(223));
  575. lcd.print("C>");
  576. } //end menumode 0 ( homepage 1 )
  577.  
  578.  
  579. //Homepage 1 -- LED color
  580. if (menumode == -1) {
  581. lcd.backlight();
  582. lcd.setCursor(0, 0);
  583. lcd.print("<Culoarea Banda>");
  584. lcd.setCursor(0, 1);
  585. lcd.print("R:");
  586. lcd.print(redVal);
  587. lcd.print(" G:");
  588. lcd.print(grnVal);
  589. lcd.print(" B:");
  590. lcd.print(bluVal);
  591. if (mcp.digitalRead(buttonPin) == LOW) {
  592. menumode = -11;
  593. click_sound(); // --- buzzer sound
  594. delay(400);
  595. }
  596. }
  597. if (menumode == -11) {
  598. lcd.setCursor(0, 1);
  599. lcd.print("R:");
  600. lcd.print(redVal);
  601. lcd.print(" G:");
  602. lcd.print(grnVal);
  603. lcd.print(" B:");
  604. lcd.print(bluVal);
  605. // -----------------------------------------BEGIN COLOR MIXER
  606. //--------------------------------------------------------
  607. potVal = analogRead(potPin); // read the potentiometer value at the input pin
  608.  
  609. if (potVal < 341) // Lowest third of the potentiometer's range (0-340)
  610. {
  611. potVal = (potVal * 3) / 4; // Normalize to 0-255
  612.  
  613. redVal = 256 - potVal; // Red from full to off
  614. grnVal = potVal; // Green from off to full
  615. bluVal = 1; // Blue off
  616. }
  617. else if (potVal < 682) // Middle third of potentiometer's range (341-681)
  618. {
  619. potVal = ( (potVal - 341) * 3) / 4; // Normalize to 0-255
  620.  
  621. redVal = 1; // Red off
  622. grnVal = 256 - potVal; // Green from full to off
  623. bluVal = potVal; // Blue from off to full
  624. }
  625. else // Upper third of potentiometer"s range (682-1023)
  626. {
  627. potVal = ( (potVal - 683) * 3) / 4; // Normalize to 0-255
  628.  
  629. redVal = potVal; // Red from off to full
  630. grnVal = 1; // Green off
  631. bluVal = 256 - potVal; // Blue from full to off
  632. }
  633. analogWrite(ledRPin, (redVal - ((redVal * ledlight) / 100))); // Write values to LED pins
  634. analogWrite(ledGPin, (grnVal - ((grnVal * ledlight) / 100)));
  635. analogWrite(ledBPin, (bluVal - ((bluVal * ledlight) / 100)));
  636. delay(5);
  637. //--------------------------------------------------------
  638. //------------------------------------------ END COLOR MIXER
  639. if (mcp.digitalRead(buttonPin) == LOW) {
  640. EEPROM.write(0, redVal); //write to memory
  641. EEPROM.write(1, grnVal);
  642. EEPROM.write(2, bluVal);
  643. idlecount = 0;
  644. menumode = 0;
  645. delay(300);
  646. click_sound(); // --- buzzer sound
  647. }
  648. } //end homepage 1
  649.  
  650.  
  651. //Homepage 2 -- Notificari
  652. if (menumode == -2) {
  653. lcd.backlight();
  654. lcd.setCursor(0, 0);
  655. lcd.print("V baterie:");
  656. float vbat = analogRead(voltagePin);
  657. float temp2 = (vbat * (5.0 / 1023.0));
  658. lcd.print( temp2 / 0.3024 );
  659.  
  660. lcd.setCursor(0, 1);
  661. lcd.print(" V:");
  662. lcd.print(mcp.digitalRead(sensorVPin));
  663. lcd.print(" Lk:");
  664. lcd.print(mcp.digitalRead(carlockPin));
  665. lcd.print(" C:");
  666. lcd.print(mcp.digitalRead(contactPin));
  667.  
  668.  
  669.  
  670.  
  671. if (mcp.digitalRead(buttonPin) == LOW) {
  672. click_sound(); // --- buzzer sound
  673. delay(400);
  674. //reseteaza notificari
  675. }
  676. } // end homepage 2
  677.  
  678. //Homepage 3 -- Open truck
  679. if (menumode == -3) {
  680. lcd.backlight();
  681. lcd.setCursor(0, 0);
  682. lcd.print("<Porbagaj>");
  683. lcd.setCursor(0, 1);
  684. lcd.print("<deschide>");
  685. if (mcp.digitalRead(buttonPin) == LOW) {
  686. //OPEN TRUCK
  687. click_sound(); // --- buzzer sound
  688. menumode = 0;
  689. idlecount = 0;
  690. }
  691. } // end homepage 3
  692.  
  693. //Homepage 4 -- Open settings
  694. if (menumode == -4) {
  695. lcd.backlight();
  696. lcd.setCursor(0, 0);
  697. lcd.print("<Setari>");
  698. lcd.setCursor(0, 1);
  699. lcd.print("<deschide>");
  700. if (mcp.digitalRead(buttonPin) == LOW) {
  701. //OPEN settings
  702. click_sound(); // --- buzzer sound
  703. menumode = 1;
  704. idlecount = 0;
  705. delay(400);
  706. }
  707. } // end homepage 4
  708.  
  709. idlecount = idlecount + 1;
  710.  
  711. } //end if (active = 1)
  712.  
  713. //keep the page with the pot value
  714. if (menumode > -5) {
  715. if (menumode <= 0) {
  716. int pot = map(analogRead(potPin), 0, 1000, 0, -4); // number of settings submenus // to EDIT ~!~~~~~~~~~~~~~
  717. menusel = pot; //read the selection from the pot
  718. if (menumode != menusel) { //only if is not already set
  719. click_sound();
  720. menumode = menusel;
  721. lcd.clear();
  722. active = 1;
  723. idlecount = 0;
  724. delay(50);
  725. }
  726. }
  727. }
  728.  
  729. if (idlecount >= 400) {
  730. if (active == 1) {
  731. active = 0;
  732. lcd.clear();
  733. lcd.setCursor(0, 0);
  734. menumode = 0;
  735. lcd.print("Stand-by mode");
  736. lcd.noBacklight();
  737. toggle_sound();
  738. idlecount = 0;
  739. }
  740. }
  741.  
  742. } //END HOMEPAGES
  743.  
  744. } // end lcd clear
  745.  
  746. //LCD CLEAR FUNCTION
  747. // use lcdclear = x ms (def:1700)
  748. // 1 sec = 10 steps
  749. // 1,7 sec = 17 steps
  750.  
  751. if (lcdf == 1) {
  752. if (lcdclear < 17) {
  753. lcdclear = lcdclear + 1;
  754. }
  755. if (lcdclear >= 17) {
  756. lcdclear = 0;
  757. lcdf = 0;
  758. lcd.clear();
  759. delay(150); //default time for animations
  760. }
  761. }
  762.  
  763. // END LCD CLEAR FUNCTION
  764.  
  765.  
  766. if (menumode <= 0) {
  767.  
  768. // BEGIN usalights TOGGLER
  769. if (mcp.digitalRead(usaPin) == HIGH) { //if toggler is ON
  770. if (usaStatus == 0) {
  771. analogWrite(usaoutPin, map(usalight, 0, 100, 0, 255)); //set PWM of usa lights
  772. lcd.clear();
  773. lcd.backlight();
  774. toggle_sound();
  775. usaStatus = 1;
  776. lcd.setCursor(0, 0);
  777. lcd.print("USA Lights ON");
  778. lcd.setCursor(0, 1);
  779. lcd.print(usalight);
  780. lcd.print("%");
  781. lcdclear = 0;
  782. lcdf = 1;
  783. }
  784. }
  785. else //if toggler is off
  786. {
  787. if (usaStatus == 1) {
  788. analogWrite(usaoutPin, 0); //set PWM of usa lights
  789. lcd.clear();
  790. lcd.backlight();
  791. toggle_sound();
  792. usaStatus = 0;
  793. lcd.setCursor(0, 0);
  794. lcd.print("USA Lights OFF");
  795. lcdclear = 0;
  796. lcdf = 1;
  797. }
  798. }
  799. // END usalights TOGGLER
  800.  
  801.  
  802. // BEGIN LED STRIP toggler
  803. if (mcp.digitalRead(ledPin) == HIGH) { //if toggler is ON
  804. if (ledStatus == 0) {
  805. delay(50);
  806. lcd.clear();
  807. lcd.backlight();
  808. toggle_sound();
  809. ledStatus = 1;
  810. lcd.setCursor(0, 0);
  811. lcd.print("Banda LED: ");
  812. lcd.setCursor(0, 1);
  813. lcd.print(ledlight);
  814. lcd.print("%");
  815. lcdclear = 0;
  816. lcdf = 1;
  817. analogWrite(ledRPin, (redVal - ((redVal * ledlight) / 100))); // Write values to LED pins
  818. analogWrite(ledGPin, (grnVal - ((grnVal * ledlight) / 100)));
  819. analogWrite(ledBPin, (bluVal - ((bluVal * ledlight) / 100)));
  820. delay(10);
  821. }
  822. }
  823. else //if toggler is off
  824. {
  825. if (ledStatus == 1) {
  826. delay(50);
  827. lcd.clear();
  828. lcd.backlight();
  829. toggle_sound();
  830. ledStatus = 0;
  831. lcd.setCursor(0, 0);
  832. lcd.print("Banda LED OFF");
  833. lcdclear = 0;
  834. lcdf = 1;
  835. analogWrite(ledRPin, 0); // Write values to LED pins
  836. analogWrite(ledGPin, 0);
  837. analogWrite(ledBPin, 0);
  838. delay(10);
  839. }
  840. }
  841. // END LED STRIP toggler
  842.  
  843. // 2226,f8,949be1 -Geo phone
  844. // AT+RNAME?<PR> OK
  845. // AT+FSAD=<PR> OK
  846.  
  847. /*
  848. // BEGIN BLUETOOTH function
  849. blinterval = blinterval + 1;
  850. if (mcp.digitalRead(contactPin) == LOW) { //if the car is off
  851. if (mcp.digitalRead(carlockPin) == HIGH) { //if the car is locked
  852. if (blcon == 0) { //if bluetooth is disconnected
  853. if (blinterval == 10) { // and 1000 ms passes
  854. if (blstate == 0) {
  855. BTSerial.write("AT+RNAME?2226,f8,949be1"); //example command
  856. blstate = 1;
  857. }
  858. else {
  859. if (BTSerial.available()) {
  860. String temp = BTSerial.readString();
  861. if (temp == "OK") {
  862. blcon = 1;
  863. lcd.setCursor(0, 0);
  864. lcd.print("Merge");
  865. // unlock car
  866. }
  867. else { //if the answer is anything else
  868. lcd.setCursor(0, 0);
  869. lcd.print(temp);
  870. }
  871. }
  872. else {
  873. lcd.setCursor(0, 0);
  874. lcd.print("nu raspuns");
  875. }
  876. blstate = 0;
  877. }
  878. }
  879. }
  880. else { //if the bluetooth is connected
  881.  
  882. }
  883. }
  884. }
  885. // END BLUETOOTH function
  886. */
  887.  
  888.  
  889. // BEGIN RFID function
  890. /*
  891. if (RFIDVal == 1) { //RFID SETTINGS
  892. if (mcp.digitalRead(contactPin) == LOW) { //if the car is NOT ignited
  893. int detectedsts = 0; // check_RFID();
  894. if (detectedsts = 1) { //if you detect my RFID
  895. if (mcp.digitalRead(carlockPin) == HIGH) { //if the car is locked
  896. if (rfidStatus == 0) {
  897. unlock_door();
  898. rfidStatus = 1;
  899. lcd.setCursor(0, 0);
  900. lcd.print("Deschid masina!");
  901. lcdclear = 0;
  902. lcdf = 1;
  903. }
  904. }
  905. else
  906. { // if the car is unlocked
  907. if (rfidStatus == 1) {
  908. lock_door();
  909. rfidStatus = 0;
  910. lcd.setCursor(0, 0);
  911. lcd.print("Inchid masina!");
  912. lcdclear = 0;
  913. lcdf = 1;
  914. }
  915. }
  916. }
  917. }
  918. }
  919. */
  920.  
  921. // END RFID function toggler
  922.  
  923. // BEGIN ALARM TOGGLER watchdog
  924. if (mcp.digitalRead(alarmPin) == HIGH) { //if the alarm is enabled
  925. if (alarmStatus == 0) {
  926. lcd.clear();
  927. lcd.backlight();
  928. toggle_sound();
  929. alarmStatus = 1;
  930. lcd.setCursor(0, 0);
  931. lcd.print("Alarm is ON");
  932. lcdf = 1;
  933. lcdclear = 0;
  934. }
  935. }
  936. else { //if the button is LOW
  937. if (alarmStatus == 1) {
  938. lcd.clear();
  939. lcd.backlight();
  940. toggle_sound();
  941. alarmStatus = 0;
  942. lcd.setCursor(0, 0);
  943. lcd.print("Alarm is OFF !");
  944. lcdf = 1;
  945. lcdclear = 0;
  946. }
  947. }
  948. // END AlARM TOGGLER feedback
  949.  
  950.  
  951.  
  952. // BEGIN ALARM TRIGGER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  953. if (alarmStatus == 1) { //if the alarm is enabled
  954. if (alarmtrig == 0) {
  955. if (mcp.digitalRead(carlockPin) == HIGH) { //if the car is locked
  956. if (mcp.digitalRead(contactPin) == HIGH) { //if the car is locked
  957. if (mcp.digitalRead(sensorVPin) == LOW) { //if vibrations are detected
  958. alarmtrig = 1;
  959. mcp.digitalWrite(triggerPin, HIGH); //horn on
  960. error_sound(); //interior sound
  961. // lock_door(); //make sure the doors are locked
  962. // delay(alarmtime); //alarm time
  963. delay(3000); //alarm time
  964. alarmtrig = 0;
  965. mcp.digitalWrite(triggerPin, LOW); //horn off
  966. }
  967. }
  968. }
  969. }
  970. }
  971. // END ALARM TRIGGER ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  972.  
  973. //BEGIN truck-opener
  974. // if truckVal == 1 {
  975. //need microphone OR knock sensor
  976. // }
  977. //END truck-opener
  978.  
  979. }
  980.  
  981. //SETTINGS MODE ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SETTINGS
  982. if (menumode >= 1) {
  983. if (menumode == 1) { //welcome message
  984. lcd.clear();
  985. lcd.setCursor(5, 0);
  986. lcd.write("SETARI");
  987. delay(1000);
  988. lcd.setCursor(4, 1);
  989. lcd.write("avansate");
  990.  
  991. delay(1500);
  992. menumode = 2;
  993. menupage = 0;
  994. lcd.clear();
  995. click_sound(); // --- buzzer sound
  996.  
  997. count = 0;
  998. }
  999. else { //menu
  1000.  
  1001. //begin setting the selection
  1002. int pot = map(analogRead(potPin), 0, 1025, 1, 7); // 7 = number of settings submenus
  1003. menusel = pot; //read the selection from the pot
  1004.  
  1005. lcd.setCursor(0, 0);
  1006. lcd.print(" ");
  1007.  
  1008. if (menupage <= 2) { //set it only if your out of a submenu
  1009.  
  1010. lcd.setCursor(0, 1);
  1011. lcd.print(" ");
  1012. if ( (pot % 2) == 0) {
  1013. lcd.setCursor(0, 1);
  1014. lcd.print("~");
  1015. if (settingspos == 0) {
  1016. settingspos = 1;
  1017. click_sound(); // --- buzzer sound
  1018. }
  1019. }
  1020. else {
  1021. lcd.setCursor(0, 0);
  1022. lcd.print("~");
  1023. if (settingspos == 1) {
  1024. settingspos = 0;
  1025. click_sound(); // --- buzzer sound
  1026. }
  1027. }
  1028.  
  1029. //end of setting the selection
  1030.  
  1031. //page switcher
  1032. if (menusel >= 1 and menusel <= 2) {
  1033. menupage = 0;
  1034. }
  1035. if (menusel >= 3 and menusel <= 4) {
  1036. menupage = 1;
  1037. }
  1038. if (menusel >= 5 and menusel <= 6) {
  1039. menupage = 2;
  1040. }
  1041. }
  1042.  
  1043.  
  1044. if (menupage == 0) {
  1045. lcd.setCursor(1, 0);
  1046. lcd.print("1.LED interior ");
  1047. lcd.setCursor(1, 1);
  1048. lcd.print("2.US Lights ");
  1049. }
  1050. if (menupage == 1) {
  1051. lcd.setCursor(1, 0);
  1052. lcd.print("3.Alarma Hoti ");
  1053. lcd.setCursor(1, 1);
  1054. lcd.print("4.Porbagaj ");
  1055. }
  1056. if (menupage == 2) {
  1057. lcd.setCursor(1, 0);
  1058. lcd.print("5.Diagnostic ");
  1059. lcd.setCursor(1, 1);
  1060. lcd.print("6.Iesi ");
  1061. }
  1062. //LED STRIP MENU
  1063. if (menupage >= 3) {
  1064. if (menupage == 3) {
  1065. lcd.setCursor(0, 0);
  1066. lcd.print("Inainte: Dupa:");
  1067. lbg1.drawValue( ledlight, 100);
  1068. lbg.drawValue( analogRead(potPin), 1024);
  1069. menudone = 1;
  1070. }
  1071. //USA LIGHTS MENU
  1072. if (menupage == 4) {
  1073. lcd.setCursor(0, 0);
  1074. lcd.print("Inainte: Dupa:");
  1075. lbg1.drawValue( usalight, 100);
  1076. lbg.drawValue( analogRead(potPin), 1024);
  1077. menudone = 2;
  1078. }
  1079.  
  1080. //ALARM MENU
  1081. if (menupage == 5) {
  1082. lcd.setCursor(0, 0);
  1083. lcd.print("Inainte: Dupa:");
  1084. lcd.setCursor(0, 1); //mid line
  1085. lcd.print(alarmtime);
  1086. lcd.print("/30s");
  1087. lcd.setCursor(9, 1); //mid line
  1088. lcd.print(map(analogRead(potPin), 0, 1025, 1, 30));
  1089. lcd.print("/30s ");
  1090. menudone = 3;
  1091. }
  1092.  
  1093. //TRUCK OPENER MENU
  1094. if (menupage == 6) {
  1095. lcd.setCursor(0, 0);
  1096. lcd.print("Inainte: Dupa:");
  1097. lcd.setCursor(0, 1); //mid line
  1098. lcd.print(timeToKnock);
  1099. lcd.print("/30s");
  1100. lcd.setCursor(9, 1); //mid line
  1101. //sssssssssssssssssssss
  1102. lcd.print("/30s ");
  1103. menudone = 4;
  1104. }
  1105.  
  1106. //Diagnostics menu
  1107. if (menupage == 6) {
  1108. lcd.setCursor(0, 0);
  1109. lcd.print("pot:");
  1110. lcd.print(analogRead(potPin)); // 16 - 8 = 8 ch's left
  1111. lcd.print(" but:");
  1112. lcd.print(mcp.digitalRead(ledPin));
  1113. lcd.setCursor(0, 1); //mid line
  1114. lcd.print(timeToKnock);
  1115. lcd.print("/30s");
  1116. lcd.setCursor(9, 1); //mid line
  1117. lcd.print(map(analogRead(potPin), 0, 1025, 1, 30));
  1118. lcd.print("/30s ");
  1119. menudone = 5;
  1120. }
  1121. //exit
  1122. if (menupage == 7) {
  1123. menumode = 0;
  1124. }
  1125.  
  1126. lcd.setCursor(7, 1); //mid line
  1127. lcd.print("[]");
  1128. delay(100); //visualization fixer
  1129. }
  1130.  
  1131. if (mcp.digitalRead(buttonPin) == LOW) {
  1132. click_sound(); // --- buzzer sound
  1133.  
  1134. //LCD STRIP MENU
  1135. if (menupage >= 3) { //back to menu from led strip settings
  1136. delay(150);
  1137. lcd.clear();
  1138. lcd.setCursor(0, 0);
  1139. if (menudone == 1) {
  1140. ledlight = map(analogRead(potPin), 0, 1025, 0, 100); //set the new value
  1141. lcd.print("Led opacity:");
  1142. lcd.print(ledlight);
  1143. lcd.print("%");
  1144. EEPROM.write(9, ledlight);
  1145. }
  1146. if (menudone == 2) {
  1147. usalight = map(analogRead(potPin), 0, 1025, 0, 100); //set the new value
  1148. lcd.print("USA opacity:");
  1149. lcd.print(usalight);
  1150. lcd.print("%");
  1151. EEPROM.write(8, usalight);
  1152. }
  1153. if (menudone == 3) {
  1154. alarmtime = map(analogRead(potPin), 0, 1025, 1, 30); //set the new value
  1155. lcd.print("Alarm time:");
  1156. lcd.print(alarmtime);
  1157. lcd.print("s");
  1158. EEPROM.write(5, alarmtime);
  1159. }
  1160. if (menudone == 4) {
  1161. timeToKnock = map(analogRead(potPin), 0, 1025, 1, 30); //set the new value
  1162. lcd.print("Knock time:");
  1163. lcd.print(timeToKnock);
  1164. lcd.print("s");
  1165. EEPROM.write(6, timeToKnock);
  1166. }
  1167. filldown();
  1168. menupage = 1;
  1169. menumode = 3; // no click
  1170. click_sound(); // --- buzzer sound
  1171. delay(1500);
  1172. lcd.clear();
  1173. }
  1174.  
  1175. else if (menupage <= 2) { //ENTERING A MENU
  1176. if (menumode != 3) {
  1177. if (menusel == 6) {
  1178. menumode = 0;
  1179. click_sound(); // --- buzzer sound
  1180. }
  1181.  
  1182. if ( (menusel % 2) == 0) {
  1183. filldown();
  1184. }
  1185. else {
  1186. fillup();
  1187. }
  1188. delay(150);
  1189.  
  1190. menupage = (menusel + 2);
  1191. click_sound(); // --- buzzer sound
  1192. lcd.clear();
  1193. }
  1194. }
  1195. }
  1196.  
  1197. //end of timeout
  1198.  
  1199. }
  1200. }
  1201.  
  1202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement