Hi,
Who am I
I am a hobbyist and new to arduino. With some logical learning and browsing, i was able to understand some programming and was able to make this Menu program, which will control various settings of my project.
Why this post
I have read that we have limited eeprom write cycles,
Whats my request from community
Can somebody look at the code, as i dont want any mistake to cost me bad eeprom . (schematic also attached at end)
Big Idea
Here is the big pic. Mode button (far right) will select menu item, the knob will set new value (range 0-100) and mid button when pressed will write eeprom with new setting.
#include <EEPROM.h> // include eeprom memory for storing settings
#include <SPI.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(9);
// ********EEProm memory*************
// thermostat value in memory
int EEPthermostat;
int EEPvaluethermo;
// night light dark setting
int EEPNLdark;
int EEPvalueNLD;
// ********Caliberation button*************
int ButtonModeInput = A2; //enter exit calib mode
void ButtonModeFunc();
int ButtonMode;
int ButtonModestate;
int ButtonModecount;
// ******** set button*************
int potButtonInput = A1; //enter exit calib mode
void potButtons();
int potButton;
int potButtonstate;
int potButtoncount;
// ********Caliberation Pot *************
int SettingPotinput = A0;
void settingpotfunc();
int settingpotread;
int potvalue;
void setup() {
Serial.begin(9600);
pinMode(SettingPotinput, INPUT);
pinMode(potButtonInput, INPUT);
pinMode (ButtonModeInput, INPUT);
lcd.begin(16, 2);
lcd.setCursor(0, 0); // set the cursor to colum 0 row 0
}
void loop() {
ButtonModeFunc();
}
//************************ Functions *****************************************
void ButtonModeFunc() {
ButtonMode = digitalRead(ButtonModeInput);
if (ButtonMode != ButtonModestate) {
if (ButtonMode == HIGH) {
ButtonModecount++;
if (ButtonModecount >= 3) {
ButtonModecount = 0;
}
Serial.print("Mode: ");
Serial.println(ButtonModecount);
}
else { // do nothing
}
}
ButtonModestate = ButtonMode; // save the current state as the last state for next time through the loop
//************************ Menu Start ************************
switch (ButtonModecount) {
case 0:
EEPROM.read(EEPthermostat);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Thermostat: ");
lcd.print(EEPvaluethermo);
settingpotfunc(); // Function reads the setting pot value
lcdnewvalue(); // clears bottom row and displays pot value
potButtons();
if (potButtoncount == 1) {
EEPvaluethermo = potvalue;
EEPROM.write(EEPthermostat, EEPvaluethermo);
newvalueset(); // clears lcd and displays new value set
potButtoncount = 0;
}
break;
case 1:
//address = EEPNLdark;
//addressvalue = EEPvalueNLD;
EEPROM.read(EEPNLdark);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("LDR Dark: ");
lcd.print(EEPvalueNLD);
settingpotfunc(); // Function reads the setting pot value
lcdnewvalue(); // clears bottom row and displays pot value
potButtons();
if (potButtoncount == 1) {
EEPvalueNLD = potvalue;
EEPROM.write(EEPNLdark, EEPvalueNLD);
newvalueset(); // clears lcd and displays new value set
potButtoncount = 0;
}
break;
} // switch case end
} // void end
//******************** small assisting functions ********************//
void potButtons() {
int potButton = digitalRead(potButtonInput);
if (potButton != potButtonstate) {
if (potButton == HIGH) {
potButtoncount++;
if (potButtoncount >= 3) {
potButtoncount = 0;
}
Serial.print("Mode: ");
Serial.println(potButtoncount);
}
else { // do nothing
}
delay(50); // to avoid button bounces
}
potButtonstate = potButton;
}
void settingpotfunc() {
settingpotread = analogRead(SettingPotinput);
potvalue = map(settingpotread, 0, 850, 0, 100);
} // void end
void lcdnewvalue() {
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("New? : ");
lcd.print(potvalue);
delay (50);
}// void end
void newvalueset() {
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print("New Value SET! ");
delay (500);
lcd.setCursor(0, 1);
lcd.print(" ");
}