Thanks for that that's working ok now, But I've now tried to correct my other code where I have a little menu system and once I get to the last menu 4 it should write the new values to the EEProm but it does not seem to.
I've done a demo version where I've cut the code down and added a little counter to show that something is happening, I press the menu button, This then jumps into the menu I scroll though adjust to values and then restart the system and and the new values I've just changed have not been stored.
Here's my test code in case I've down something wrong or missed something
#include <Arduino.h>
#include "EEPROM.h"
#include <U8g2lib.h>
#include <SPI.h>
#include <TimedAction.h>
#include <Wire.h> // SDA=pin4, SCL=pin5
#include <MCP342x.h>
float Volts_Cal ;
float Volt_scale; //Change this if the incoming reading with nothing connected on the input terminals
int Eaddress = 0;
int menubuttonPushCounter = 0;
int menubuttonState = 0;
int lastmenuButtonState = 0;
boolean menubuttonbool = false;
int upbuttonState;
int lastupbuttonState;
int upbutton;
int downbuttonState;
int lastdownbuttonState;
int downbutton;
int menucounter = 0;
long counting = 0 ;
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 1000;
boolean send_data = true;
#define menubuttonPin 36//pin to which the button is conected to
#define downbuttonpin 39//Pin to which downbutton is conected to.
#define upbuttonpin 34//Pin to which upbutton is conected to.
//############## LCD Pins ####################################
U8G2_ST7920_128X64_2_HW_SPI u8g2(U8G2_R2, /* CS=*/ 17); // Feather
//####################################################
//# Initializes TimedAction - Timer Interrupt #
//####################################################
void TimerService01();// Timer service routinte
TimedAction Timedact01 = TimedAction(80, TimerService01);// Timer1 for ADC'S, called every 80 Ms seconds
void setup() {
Serial.begin(115200);
u8g2.begin();
pinMode(menubuttonPin, INPUT);
digitalWrite(menubuttonPin, HIGH);
pinMode(downbuttonpin, INPUT);
digitalWrite(downbuttonpin, HIGH);
pinMode(upbuttonpin, INPUT);
digitalWrite(upbuttonpin, HIGH);
u8g2.setColorIndex(1); // pixel on
u8g2.setFontRefHeightExtendedText();
u8g2.clear();
//read the variables back
EEPROM.begin(1024);
EEPROM.get(Eaddress, Volt_scale);
Eaddress += sizeof(float);
EEPROM.get(Eaddress, Volts_Cal);
Eaddress += sizeof(float);
}
void loop() {
if (send_data == true) {
Serial.print("VOLTS SCALE");
Serial.println(Volt_scale, 3);
Serial.print("VOLTS CAL: ");
Serial.println(Volts_Cal, 2);
send_data = false;
}
unsigned long currentMillis = millis();
// just so I know it working
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
counting ++;
}
menubuttonState = digitalRead(menubuttonPin);
if (menubuttonState != lastmenuButtonState)
{
if (menubuttonState == LOW)
{
Serial.println("MENU");
delay(100);
menubuttonPushCounter = 4;
menubuttonbool = true;
}
if (menubuttonState == HIGH)
{
menubuttonbool = false;
}
}
if (menubuttonbool == true && menubuttonPushCounter == 4)
{
menucounter = 0;
menu();
}
lastmenuButtonState = menubuttonState;
Timedact01.check();
}
///############ Displays the Menu ##########################################
int menu()
{
Timedact01.check();
delay(1000);
while (menucounter < 4) // Runs in a loop while menucounter is less than 4
{
menubuttonState = digitalRead(menubuttonPin);
// scrollbuttonState = digitalRead(scrollbuttonpin);
upbutton = digitalRead(upbuttonpin);
downbutton = digitalRead(downbuttonpin);
if (menubuttonState != lastmenuButtonState)
{
// u8g2.firstPage(); // Update LCD 1
// do {
// u8g2.clear();
// } while ( u8g2.nextPage() );
if (menubuttonState == LOW)
{
delay(200);
menucounter++;
}
}
lastmenuButtonState = menubuttonState;
switch (menucounter) {
case 0:
u8g2.firstPage(); // Update LCD 1
do {
u8g2.setFont(u8g_font_6x10);
u8g2.drawStr(7, 5, "CALIBRATE VREF CH1");
u8g2.setFont(u8g2_font_fub11_tf);
u8g2.setCursor(40, 35);
u8g2.print(Volt_scale, 5); // 3 dp's
if (upbutton == LOW) {
Volt_scale = Volt_scale + 0.00001; //(Volt_scale, 5); //Volts_disp_Ch1
delay(200);
}
if (downbutton == LOW)
{
Volt_scale = Volt_scale - 0.00001;;
delay(200);
}
} while ( u8g2.nextPage() );
break;
//####### voltage calibration screen #########################
case 1:
u8g2.firstPage(); // Update LCD 1
do {
u8g2.setFont(u8g_font_6x10);
u8g2.drawStr(7, 5, "CALIBRATE VOLTS CH1");
u8g2.setFont(u8g2_font_fub11_tf);
u8g2.setCursor(40, 35);
u8g2.print(Volts_Cal, 1); // 3 dp's
if (upbutton == LOW) {
Volts_Cal = Volts_Cal + 0.1;
delay(200);
}
if (downbutton == LOW)
{
Volts_Cal = Volts_Cal - 0.1;;
delay(10);
}
} while ( u8g2.nextPage() );
break;
//####### current calibration screen #########################
case 2:
u8g2.firstPage(); // Update LCD 1
do {
u8g2.setFont(u8g2_font_fub17_tn);
u8g2.drawStr(22, 33, "MENU 3");
} while ( u8g2.nextPage() );
break;
//####### current calibration screen #########################
case 3:
u8g2.firstPage(); // Update LCD 1
do {
u8g2.setFont(u8g_font_6x10);
u8g2.drawStr(22, 33, "MENU 4");
} while ( u8g2.nextPage() );
break;
//########## Stores the set values and reads the new values #######################
case 4:
u8g2.firstPage(); // Update LCD 1
do {
u8g2.setFont(u8g_font_6x10);
u8g2.drawStr(7, 5, "STORE VALUES");
menucounter = 4;
menubuttonPushCounter = 0;
} while ( u8g2.nextPage() );
EEPROM.begin(1024);
EEPROM.writeFloat(Eaddress, Volt_scale);//store the vatibale
Eaddress += sizeof(float);
EEPROM.writeFloat(Eaddress, Volts_Cal);//store the vatibale
Eaddress += sizeof(float);
EEPROM.commit();
delay(1000);
break;
}
}
}
void LCD1updatedraw() {
// Write large text to LCD (col, row, text), Sizes = 11,14,17,20,25,30,35,42,49
u8g2.setFont(u8g2_font_fub11_tn);
u8g2.setCursor(10, 2);
u8g2.setFontPosTop();
u8g2.print(counting); // 3 dp's
}
void TimerService01() {
// Service LCDs
u8g2.firstPage(); // Update LCD 1
do {
LCD1updatedraw();
} while ( u8g2.nextPage() );
}