Stap 6: Nu we gebruiken EEPROM te sparen laatste status van schakelaars
Met behulp van de EEPROM
Ik ben het toevoegen van interne EEPROM om op te slaan de laatste status
Als het apparaat uit te gaan van de macht en opnieuw aangesloten
het laden van de laatste status van schakelaars
die schakelaars die op voordat de macht af nu zijn ze nog steeds op
#include <SoftwareSerial.h> // SoftwareSerial mySerial(1, 0); // 1-RX, 0-TX - #include <LiquidCrystal.h>#include <EEPROM.h>int ch1 = A0; int ch2 = A1; int ch3 = A2; int ch4 = A3; LiquidCrystal lcd(12, 11, 5, 4, 3, 2);;//(rs, enable, d4, d5, d6, d7) int switchPin[] = {6,7,8,9}; void setup() { mySerial.begin(9600); pinMode(ch1, OUTPUT); pinMode(ch2, OUTPUT); pinMode(ch3, OUTPUT); pinMode(ch4, OUTPUT);lcd.begin(16, 2);pinMode(switchPin[0], INPUT); digitalWrite(switchPin[0], HIGH); pinMode(switchPin[1], INPUT); digitalWrite(switchPin[1], HIGH); pinMode(switchPin[2], INPUT); digitalWrite(switchPin[2], HIGH); pinMode(switchPin[3], INPUT); digitalWrite(switchPin[3], HIGH); pinMode(switchPin[4], INPUT); digitalWrite(switchPin[4], HIGH); if (EEPROM.read(0) == 1) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch1, HIGH) ; lcd.setCursor(0, 0); lcd.print("r1 ON"); // toggle running variable // indicate via LED } if (EEPROM.read(0) == 0) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch1, LOW) ; lcd.setCursor(0, 0); lcd.print("r1 OFF"); // toggle running variable // indicate via LED } if (EEPROM.read(1) == 1) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch2, HIGH) ; lcd.setCursor(8, 0); lcd.print("r2 ON"); // toggle running variable // indicate via LED } if (EEPROM.read(1) == 0) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch2, LOW) ; lcd.setCursor(8, 0); lcd.print("r2 OFF"); // toggle running variable // indicate via LED } if (EEPROM.read(2) == 1) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch3, HIGH) ; lcd.setCursor(0, 1); lcd.print("r3 ON"); // toggle running variable // indicate via LED } if (EEPROM.read(2) == 0) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch3, LOW) ; lcd.setCursor(0, 1); lcd.print("r3 OFF"); // toggle running variable // indicate via LED } if (EEPROM.read(3) == 1) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch4, HIGH) ; lcd.setCursor(8, 1); lcd.print("r4 ON"); // toggle running variable // indicate via LED } if (EEPROM.read(3) == 0) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch4, LOW) ; lcd.setCursor(8, 1); lcd.print("r3 OFF"); // toggle running variable // indicate via LED } } void loop() { char caracter = mySerial.read(); //--------------------------------ch1--------------------- if(caracter == 'A') { digitalWrite(ch1,HIGH); lcd.setCursor(0, 0); lcd.print("r1 ON"); EEPROM.write(0, 1); }if(caracter == 'a') { digitalWrite(ch1,LOW); lcd.setCursor(0, 0); lcd.print("r1 OFF"); EEPROM.write(0, 0); } //---------------------------------ch1---------------------- //---------------------------------ch2--------------------- if(caracter == 'B') { digitalWrite(ch2,HIGH); lcd.setCursor(8, 0); lcd.print("r2 ON"); EEPROM.write(1, 1); }if(caracter == 'b') { digitalWrite(ch2,LOW); lcd.setCursor(8, 0); lcd.print("r2 OFF"); EEPROM.write(1, 0); } //---------------------------------ch2---------------------- //---------------------------------ch3--------------------- if(caracter == 'C') { digitalWrite(ch3,HIGH); lcd.setCursor(0, 1); lcd.print("r3 ON"); EEPROM.write(2, 1); }if(caracter == 'c') { digitalWrite(ch3,LOW); lcd.setCursor(0, 1); lcd.print("r3 OFF"); EEPROM.write(2, 0); } //---------------------------------ch3---------------------- //---------------------------------ch4--------------------- if(caracter == 'D') { digitalWrite(ch4,HIGH); lcd.setCursor(8, 1); lcd.print("r4 ON"); EEPROM.write(3, 1); }if(caracter == 'd') { digitalWrite(ch4,LOW); lcd.setCursor(8, 1); lcd.print("r4 OFF"); EEPROM.write(3, 0); } //---------------------------------ch4---------------------- if (digitalRead(switchPin[0]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch1, HIGH) ; lcd.setCursor(0, 0); lcd.print("r1 ON"); EEPROM.write(0, 1); } if (digitalRead(switchPin[0]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch1, LOW) ; lcd.setCursor(0, 0); lcd.print("r1 OFF"); EEPROM.write(0, 0); } if (digitalRead(switchPin[1]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch2, HIGH) ; lcd.setCursor(8, 0); lcd.print("r2 ON"); EEPROM.write(1, 1); } if (digitalRead(switchPin[1]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch2, LOW) ; lcd.setCursor(8, 0); lcd.print("r2 OFF"); EEPROM.write(1, 0); } if (digitalRead(switchPin[2]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch3, HIGH) ; lcd.setCursor(0, 1); lcd.print("r3 ON"); EEPROM.write(2, 1); } if (digitalRead(switchPin[2]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch3, LOW) ; lcd.setCursor(0, 1); lcd.print("r3 OFF"); EEPROM.write(2, 0); } if (digitalRead(switchPin[3]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch4, HIGH) ; lcd.setCursor(8, 1); lcd.print("r4 ON");// toggle running variable EEPROM.write(3, 1); } if (digitalRead(switchPin[3]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch4, LOW) ; lcd.setCursor(8, 1); lcd.print("r4 OFF"); EEPROM.write(3, 0); } delay(0); }