Hello everybody, I am building an electronic eye dropper that releases one drop of eye medication once every three seconds when the input of a touch sensor reads HIGH. The number of drops dispensed by my device is represented by the variable dispenseCount with the maximum number of drops that can be dispensed being dispenseCount=82. Hence, full device is when dispenseCount = 0 and empty device is when dispenseCount = 82.
Normally when the battery to my device is turned off, the power to the ATtiny84 ceases to and as a result when the battery is turned back on, the code resets with the dispenseCount value always being 0 regardless it's value before the battery was turned off. This is problematic as after turning the battery off and on, the device always thinks it's full even when it's not.
I seek to utilize the ATtiny84's internal EEPROM to store the integer value of dispenseCount such that when the battery is turned off and on again, the device will continue to function at the dispenseCount value it had before the battery was turned off instead of always being 0.
My code is as follows (not full code, heavily simplified for the purpose of making it easily readable for this forum):
#include <EEPROM.h>
int addr = 0;
int dispenseCount = 0;
int maxDispenseCount = 82;
void setup() {
// touch sensor input
pinMode(2, INPUT);
}
void loop() {
// my attempt at accessing the dispenseCount value from before the battery turning off
if (dispenseCount != 0) {
dispenseCount = EEPROM.read(addr);
}
// when touch sensor is activated
while (digitalRead(2) == HIGH) {
dispenseCount += 1;
delay(3000);
EEPROM.update(addr, dispenseCount);
while (dispenseCount >= maxDispenseCount) {
dispenseCount -= maxDispenseCount;
EEPROM.update(addr, dispenseCount);
}
}
}
I would greatly appreciate any help on making this work! I am also programming the ATtiny84 using an arduino uno as ISP incase that helps or means I need to do extra to make it work.
Kind regards,
Anthony.