Changing different variables with the serial interface.

Greetings from south Africa I need some advice. first thing first please excuse me if i make a spelling mistake as English is not my first language. I have some basic programming experience, but not a lot in any way.

I am busy writing code for a battery monitor for large scale energy storage. The code is working well at least the core part that reads the ads1115 and changes the battery state of charge. At the moment to change any of the config i use the serial interface almost like a basic command line and it is working but i would like to know what would be the correct way to use the serial interface to select an setting and then change that setting value from 5 to 10 as an example. I will post my code as soon as I get home. I am just curios as to what would be the correct flow for a piece of code that does this function.

Any tips will help

Let's wait for your code to see what you're doing; you might already have the perfect solution.

Here is the code that handles the changing of the config. messy but it works

#include <EEPROM.h>

//  batt data 
float capacityAH = 105;     // addres 0
float hours = 20 ;          // addres 5                                    
float efficency = 0.92 ;    // addres 10
float Pe = 1.19 ;           // addres 15 

// user settings 
float batsyncT =  300;     // addres 20
float batsyncV =  13.8;    // addres 25
float batsyncper = 0.01 ;  // addres 30
float tailcurrent = 0.2 ;  // addres 35
float desireddod =  0.5;   // addres 45

// vairbles

//core 
String serialstat = "D"  ;    // status of serial monitor 
String serialin = ""  ;     // new serial input 
int configstep = 0 ;    // step of the config the program is in 
int configselect = 0 ;       // config to change 
float confignewval = 0 ;          // new value of config
float lcdtime   = 0 ;      // lcd timing 



void setup() 
{
  // load settings ;
EEPROM.get(0,capacityAH);
EEPROM.get(5,hours);
EEPROM.get(10,efficency);
EEPROM.get(15,Pe);
EEPROM.get(20,batsyncT);
EEPROM.get(25,batsyncV);
EEPROM.get(30,batsyncper);
EEPROM.get(35,tailcurrent);
EEPROM.get(40,desireddod);

// normal settup 

  Serial.begin(9600);              // starting the serial output


}

void loop() 
{
// display code 





if ( millis() - lcdtime  > 2000    )
{ 
lcdtime = millis(); // reset of lcd timing 
  
// serila data 
if (serialstat == "D")
{


Serial.println ("To change settings type C ") ; 
Serial.println("data will be shown while program runs ");
}



} // end off display code 



// Serial change settings
if (Serial.available() > 0)
{
serialin = Serial.readString() ; 

if (serialin == "D")
{
serialstat = "D";
configstep = 0 ;
Serial.println("Display");
}

if (serialin == "C" and (configstep == 0) )
{
Serial.println("config");
serialstat = "C"; 
Serial.println("1: AH capacity ") ;
Serial.println("2 Time rating ") ;
Serial.println("3 charching eficency ") ;
Serial.println("4 peukert eksponent ") ;
Serial.println("5 battery sync time ") ;
Serial.println("6 battery sync voltage  ") ;
Serial.println("7 Battery sync percentage ") ;
Serial.println("8 Tailcurrent ") ;
Serial.println("9 depth of discharge ");
Serial.println("type the number of config to change ");
configstep = 1 ;
}
else
{
if (( serialstat == "C") and (configstep == 1))
{
configselect = serialin.toInt() ;
configstep = 2 ;
Serial.println("enter the new value");
}
else 
{
if (( serialstat == "C") and (configstep == 2))
{
confignewval = serialin.toFloat();
configstep = 3;  
}

if (( serialstat == "C") and (configstep == 3))
{
if (configselect == 1)
{
capacityAH = confignewval;
}
if (configselect == 2)
{
hours = confignewval;
}
if (configselect == 3)
{
efficency = confignewval;
}
if (configselect == 4)
{
Pe = confignewval;
}
if (configselect == 5)
{
 batsyncT = confignewval;
}
if (configselect == 6)
{
batsyncV = confignewval;
}
if (configselect == 7)
{
batsyncper = confignewval;
}
if (configselect == 8)
{
tailcurrent = confignewval;
}
if (configselect == 9)
{
desireddod = confignewval;
}
EEPROM.put(0,capacityAH);
EEPROM.put(5,hours);
EEPROM.put(10,efficency);
EEPROM.put(15,Pe);
EEPROM.put(20,batsyncT);
EEPROM.put(25,batsyncV);
EEPROM.put(30,batsyncper);
EEPROM.put(35,tailcurrent);
EEPROM.put(40,desireddod);
Serial.print("eeprom was updated at config ");
Serial.print(configselect);
Serial.print(" to ");
Serial.println(confignewval);
configstep = 0 ;  ;
}

}
}
}// end of serial change settings code 

} // end of program

Read the updated Serial Input Basics to get ideas.

Geniet die aand.

Bad idea to use Strings -- they can, and often do, cause the Arduino to crash by introducing memory problems.

Just use character variables and arrays (C-strings), and the simple, very reliable string handling functions.

Thank you very much for your help and advice. I will have to rewrite the code as i have also seen some parts that can be improved....a lot. I will definitely have a look at that Serial input basic and try to remove those Strings.

Thank you for your time and advice

Dit maak my altyd bly om n ander Suid-Afrikaner op n forum raak te loop.

En ook iemand wat jou taal praat. :slight_smile:

Nico-Merwe:
Dit maak my altyd bly om n ander Suid-Afrikaner op n forum raak te loop.

En ook iemand wat jou taal praat. :slight_smile:

Ek is nie een Suid-Afrikaner nie; ek is een Nederlander (sorry to burst the bubble :slight_smile: ). Ek woon al 18 jaar in Suid-Afrika (Jo'burg) en verstaan Afrikaans en praat dat een bietjie.

Ek moet mijn vrou vra om myn afrikaans te korrigeer :wink: