Arduino Forum

Using Arduino => Microcontrollers => Topic started by: fauzian on Jul 01, 2020, 10:56 am

Title: How to make arduino code permanently
Post by: fauzian on Jul 01, 2020, 10:56 am
how to make arduino code permanently?
the program :

Code: [Select]

#include<EEPROM.h>
#define address_DeviceID 1
#define address_InterValSending 5

/************ubah parameter disini************/
String  strDeviceID = "HGT07";      //maksimal 5 karakter
uint8_t interval      = 5;          //dalam satuan menit
/*********************************************/

String  strDummy      = "";
char    charBuf[10];
char    kar;
int     value         =0;


void setup() {
   Serial.begin(9600);
   
   //tulis device id ke eeprom
   for(uint8_t i=0;i<=strDeviceID.length()-1;i++){
        strDummy=strDeviceID.substring(i,i+1);
        strDummy  .toCharArray(charBuf,10);
        kar=charBuf[0];
        value=(int)kar;
        EEPROM.write(1+i,value);
    }
   strDummy="";
   
   //richek
   for(uint8_t i=1;i<=5;i++){
      strDummy+=(String)(char)(EEPROM.read(i));     
   }

   if(strDummy==strDeviceID){
    Serial.println("Sukses Tulis Data");
    Serial.println(strDummy);
   }else{
    Serial.println("GAGAL Tulis Data");
    Serial.println(strDummy);
   }

   //tulis interval sending
   EEPROM.write(10,interval);
   if(EEPROM.read(10)==interval){
    Serial.println("Tulis Interval Sukses");
    Serial.print(EEPROM.read(10));Serial.print(" Menit");
    Serial.println("");
   }else{
    Serial.println("Tulis Interval Gagal!");
    Serial.print(EEPROM.read(10));Serial.print(" Menit");
    Serial.println("");
   }
}

void loop() {
  // put your main code here, to run repeatedly:

}



i think the arduino have the permanently code, because when i upload this sketch and change this value, the program not removed and the value has change.

Code: [Select]

/************ubah parameter disini************/
String  strDeviceID = "HGT07";      //maksimal 5 karakter
uint8_t interval      = 5;          //dalam satuan menit
/*********************************************/


can you explain? and tell me how to make the permanently code?
Title: Re: How to make arduino code permanently
Post by: sterretje on Jul 03, 2020, 05:18 pm
There is no need to cast a character to an integer before storing it in EEPROM. Further an integer is two bytes and EEPROM.write only writes one byte.

The preferred approach probably is something like
Code: [Select]
char  strDeviceID[10] = "HGT07"; // minimum length should be 6
char charBuf[10];
...
...
void setup()
{
  EEPROM.put(0, strDeviceID);

  EEPROM.get(0, charBuf);

  if(strcmp(strDeviceID, charBuf) != 0)
  {
    // not a match
  }
  else
  {
    // match
  }
}