non riesco a risolvere un problemino che sembrerebbe banale.
Con lo sketch che segue provo ad attivare un timer su pressione di un pulsante (UNO-ON).
Alla successiva pressione di un secondo pulsante (DUE-OFF) inserisce il dato dei secondi trascorsi nella EEPROM
Il problema è che ho necessità di avere una somma di tali secondi, invece alla successiva pressione del pulsante ON (Avvio Timer) scopro che non tiene conto del dato presente nella variabile Secondi_Read.
Grazie dell'aiuto
Vi abbraccio
//Scrivo e leggo su EEPROM i secondi di un timer
#include <EEPROM.h>
#define UNO 7
#define DUE 8
int c1 = 0;
unsigned long Timer;
unsigned long Secondi;
unsigned long Secondi_Read;
bool onetime1 = true; // Eseguo solo una volta ONTIMER
bool onetime2 = true; // Eseguo solo una volta OFFTIMER
void setup() {
Serial.begin(9600);
pinMode(UNO, INPUT_PULLUP);
pinMode(DUE, INPUT_PULLUP);
}
void loop() {
if (digitalRead(UNO) == LOW){
delay(40);
if (digitalRead(UNO) == LOW){Serial.println("---- UNO---- "); ONTIMER();}
}
if (digitalRead(DUE) == LOW){
delay(40);
if (digitalRead(DUE) == LOW){Serial.println("---- DUE---- "); OFFTIMER();}
}
} //Chiude Loop
void ONTIMER()
// Parte Timer
{
if (onetime1) {
Timer = millis();
onetime2 = true;
onetime1 = false;
//Leggo valore Secondi_Read in EEPROM
long aa=EEPROM.read(c1);
long Secondi_Read=aa;
Serial.println("---- ON---- ");
Serial.print("Secondi_Read: ");
Serial.println(Secondi_Read);
Serial.println("----------------------------------- ");
}
}
void OFFTIMER()
// Ferma Timer
{
if (onetime2) {
Secondi=Secondi_Read+((millis()-Timer)/1000UL);
onetime1 = true;
onetime2 = false;
//SCRIVE SU EEPROM QUANDO IL TIMER VA OFF
long a = Secondi;
EEPROM.write(c1, a);
Serial.println("---- OFF---- ");
Serial.print("Secondi: ");
Serial.println(Secondi);
Serial.print("Secondi_Read: ");
Serial.println(Secondi_Read);
Serial.println("----------------------------------- ");
}
}
/*
* EEPROM Write
*
* Stores values read from analog input 0 into the EEPROM.
* These values will stay in the EEPROM when the board is
* turned off and may be retrieved later by another sketch.
*/
#include <EEPROM.h>
/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
int addr = 0;
void setup() {
/** Empty setup. **/
}
void loop() {
/***
Need to divide by 4 because analog inputs range from
0 to 1023 and each byte of the EEPROM can only hold a
value from 0 to 255.
***/
int val = analogRead(0) / 4;
/***
Write the value to the appropriate byte of the EEPROM.
these values will remain there when the board is
turned off.
***/
EEPROM.write(addr, val);
/***
Advance to the next address, when at the end restart at the beginning.
Larger AVR processors have larger EEPROM sizes, E.g:
- Arduno Duemilanove: 512b EEPROM storage.
- Arduino Uno: 1kb EEPROM storage.
- Arduino Mega: 4kb EEPROM storage.
Rather than hard-coding the length, you should use the pre-provided length function.
This will make your code portable to all AVR processors.
***/
addr = addr + 1;
if (addr == EEPROM.length()) {
addr = 0;
}
/***
As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
EEPROM address is also doable by a bitwise and of the length - 1.
++addr &= EEPROM.length() - 1;
***/
delay(100);
}
Zamundo:
Cazziate a parte, ho sempre scritto int su EEPROM
Non diciamo sciocchezze ... EEPROM.read() ed EEPROM.write() leggono/scrivono solo BYTE ed è chiaramente documentato nel reference.
Quel tuo pezzo di codice ti funziona perché ... analogRead() da un valore massimo a 10 bit che, guarda caso, diviso per 4 (ovvero >> 2) da un valore a 8 bit ... e c'è pure scritto nei commenti ... se tu li leggessi !!! >:(
Zamundo:
PS Guglielmo perchè reagisci sempre in modo "nervoso" ? Dai che con l'estate urge vacanza
... perché ti conosco personalmente, so che lavori su Arduino da anni, che hai parecchia esperienza e ... NON ammetto che una persona come te, che ha sviluppato tutto quello che ha sviluppato, faccia ancora degli errori simili, anzi ... mi fa veramente incxxxare >:( >:( >:(
... oltre a non leggere i reference, continui a fare SEMPRE gli stessi sbagli ... variabili "locali" con lo stesso nome di variabili "globali" ... e si che ti era già stato spiegato il problema ...