我需要能够找到5个连续的EEPROM存储器位,理想情况下最接近2(因为我有与其他数据相关的位#1和2,并希望保持组织有序)。我开发了这个有效的代码,但for循环在找到一组好的数字后继续。
Serial.println("got to assignment number finder");
for (int AssignCheck = 2; AssignCheck < 250; AssignCheck++){
Serial.println("Finding a good assignment number " + String(AssignCheck));
if (EEPROM.read(AssignCheck) == 255){ //Looks for a blank space which can be used to store the new card
if (EEPROM.read(AssignCheck + 1) == 255){
if (EEPROM.read(AssignCheck + 2) == 255){
if (EEPROM.read(AssignCheck + 3) == 255){
if (EEPROM.read(AssignCheck + 4) == 255){
Serial.println("Found assignment numbers " + String(AssignCheck) + " through to " + String(int(AssignCheck) + 4) + ". Scanner value = " + String(Scanner));
int StoreValue = AssignCheck;
}
}
}
}
}
}
然后我想我可以在它周围放一个while循环,并且一旦变量设置为0而不是1就让while循环停止,所以我写了这个:(注意while循环的引入,变量和中间的Scanner = 0
行。
Serial.println("got to assignment number finder");
int Scanner = 1;
while (Scanner == 1){
for (int AssignCheck = 2; AssignCheck < 250; AssignCheck++){
Serial.println("Finding a good assignment number " + String(AssignCheck) + ". Scanner value = " + String(Scanner));
if (EEPROM.read(AssignCheck) == 255){ //Looks for a blank space which can be used to store the new card
if (EEPROM.read(AssignCheck + 1) == 255){
if (EEPROM.read(AssignCheck + 2) == 255){
if (EEPROM.read(AssignCheck + 3) == 255){
if (EEPROM.read(AssignCheck + 4) == 255){
Scanner = 0;
Serial.println("Found assignment numbers " + String(AssignCheck) + " through to " + String(int(AssignCheck) + 4) + ". Scanner value = " + String(Scanner));
int StoreValue = AssignCheck;
}
}
}
}
}
}
}
它在我想要时正确识别并将变量设置为0,但是while循环似乎没有影响,循环继续产生可以工作的数字集。
作为一名新手编码员,我不确定接下来会尝试什么。
非常感谢任何建议。
答案 0 :(得分:0)
在这种情况下,您可以使用说明break
:
Serial.println("got to assignment number finder");
for (int AssignCheck = 2; AssignCheck < 250; AssignCheck++){
Serial.println("Finding a good assignment number " + String(AssignCheck));
if (EEPROM.read(AssignCheck) == 255){ //Looks for a blank space which can be used to store the new card
if (EEPROM.read(AssignCheck + 1) == 255){
if (EEPROM.read(AssignCheck + 2) == 255){
if (EEPROM.read(AssignCheck + 3) == 255){
if (EEPROM.read(AssignCheck + 4) == 255){
Serial.println("Found assignment numbers " + String(AssignCheck) + " through to " + String(int(AssignCheck) + 4) + ". Scanner value = " + String(Scanner));
int StoreValue = AssignCheck;
break;
}
}
}
}
}
}
正如您可以阅读here,它完全符合您的要求。
请注意,您的第二次尝试不起作用,因为for loop
在一段时间内:它在整个for loop
之前完成,之后,它会比较“扫描仪“while loop
你可以这样纠正:
Serial.println("got to assignment number finder");
int Scanner = 1;
for (int AssignCheck = 2; (AssignCheck < 250) && (Scanner == 1); AssignCheck++){
Serial.println("Finding a good assignment number " + String(AssignCheck) + ". Scanner value = " + String(Scanner));
if (EEPROM.read(AssignCheck) == 255){ //Looks for a blank space which can be used to store the new card
if (EEPROM.read(AssignCheck + 1) == 255){
if (EEPROM.read(AssignCheck + 2) == 255){
if (EEPROM.read(AssignCheck + 3) == 255){
if (EEPROM.read(AssignCheck + 4) == 255){
Scanner = 0;
Serial.println("Found assignment numbers " + String(AssignCheck) + " through to " + String(int(AssignCheck) + 4) + ". Scanner value = " + String(Scanner));
int StoreValue = AssignCheck;
}
}
}
}
}
}
答案 1 :(得分:-1)
读取EEPROM很贵,请考虑一下:
Serial.println("got to assignment number finder");
int Scanner = 1;
char rollingBuffer[5];
rollingBuffer[0] = EEPROM.read(2 + 0);
rollingBuffer[1] = EEPROM.read(2 + 1);
rollingBuffer[2] = EEPROM.read(2 + 2);
rollingBuffer[3] = EEPROM.read(2 + 3);
rollingBuffer[4] = EEPROM.read(2 + 4);
pointRoll = 0;
for (int AssignCheck = 7; (AssignCheck < 255); AssignCheck++)
{
if ((rollingBuffer[0] == 255) && (rollingBuffer[1] == 255) && (rollingBuffer[2] == 255) && (rollingBuffer[3] == 255) && (rollingBuffer[4] == 255))
{
Serial.println("Found assignment numbers " + String(AssignCheck-5) + " through to " + String(int(AssignCheck-1)) + ". Scanner value = " + String(Scanner));
int StoreValue = AssignCheck;
break;
}
rollingBuffer[pointRoll] = EEPROM.read(AssignCheck);
pointRoll++;
if (pointRoll > 4) pointRoll = 0;
}