Hi All
I meet the problem with the variable when I increase it to 1.
UNO board +1 button.
This is code :
#include <EEPROM.h>
#define START 6
float chuoi[]={};
float num=0;
int pos=0;
int bien=0;
int i=0;
void setup()
{
pinMode(START,INPUT);
digitalWrite(START,HIGH);
Serial.begin(115200);
}
void addtoado()
{
for(i;i<=5;i++)
{
num=i+0.5;
EEPROM.put(i*sizeof(num),num);
delay(5);
Serial.print("num=");
Serial.println(num);
}
}
void laytoado()
{
EEPROM.get(pos*sizeof(num),chuoi[pos]); // foat size 4 byte
Serial.print("chuoi[pos]=");
Serial.println(chuoi[pos]);
}
void loop()
{
bien=digitalRead(START);
if(bien==0)
{
delay(500);
addtoado();
Serial.print("posold=");
Serial.println(pos);
laytoado();
pos++;
Serial.print("posnew=");
Serial.println(pos);
if(pos>5)
{
pos=0;
}
}
}
First press : The monitor display posnew =1 // correct
Second press : posnew =16321 ( WHY ???? , increase 1 ???? )
Third press : return the first press ( ??? )
Please HELP !
float chuoi[]={};
This is not allowed in C++. If GCC compiler permitted this to slip through as a non-standard extension, then you ended up with an array of size 0
. You are not allowed to access elements of such array - they don't exist.
Yet you are happily doing it in your code. Your code has undefined behavior.
You may get into problems if you execute this statement more than once.
for(i;i<=5;i++)
Dear Montmorency
I'm done. It simple , but i don't attention. Thanks very much !
Dear david_2018
Thanks ! But I think i don't reset " i " .The loop "for" not working for the second.
Please at least post your solution so others can learn
Oh Sorry !
This code final , it working ok . thanks all !
#include <EEPROM.h>
#define START 6
float chuoi[5]; // ^_^
float num=0;
int pos=0;
int bien=0;
int i=0;
void setup()
{
pinMode(START,INPUT);
digitalWrite(START,HIGH);
Serial.begin(115200);
}
void addtoado()
{
for(i;i<=5;i++)
{
num=i+0.5;
EEPROM.put(i*sizeof(num),num);
delay(5);
Serial.print("num=");
Serial.println(num);
}
}
void laytoado()
{
EEPROM.get(pos*sizeof(num),chuoi[pos]); // foat size 4 byte
Serial.print("chuoi[pos]=");
Serial.println(chuoi[pos]);
}
void loop()
{
bien=digitalRead(START);
if(bien==0)
{
delay(500);
addtoado();
Serial.print("posold=");
Serial.println(pos);
laytoado();
pos++;
Serial.print("posnew=");
Serial.println(pos);
if(pos>5)
{
pos=0;
}
}
}
system
April 23, 2019, 3:40pm
7
for(i;i<=5;i++)
The first clause of a for loop is the initialization clause. i doesn't initialize anything. i = 0 does.