-
Notifications
You must be signed in to change notification settings - Fork 0
/
EEPROMledToggle.ino
83 lines (66 loc) · 2.03 KB
/
EEPROMledToggle.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*EEPROM elephant in the room
*
* solid state non volatile memory
* small amount of data stored
* rated life expetency
* 100000 ryt cycle
* constantly updating the code : stops working
well leveling
store eprom
*
*
*
* typical size 512kb smalldata
* specifying addres and action read write or erase
* 1023 kb of eeprom to use
* rated life expectancy
* only write to each adrres limited time
* constantly updating thng eeprom may fail
*
* change to fresh adrss if it reaches a limit
*/
#include <EEPROM.h>
//
//pin definitions
int ledPin = 13;
int buttonPin = 2;
//global varibles
int lastButtonState = 1;
long unsigned int lastPress;
int debounceTime = 20;
int counter;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
//initialise Serial port
Serial.begin(9600);
// toggle button function if statement
//takes one arg address to read from -- 0
//assign counter the value of address 0
counter = EEPROM.read(0);
//write a 0 to address 0.This allows for the consecuti
//2 arg adress to write to , value to rite to
EEPROM.write(0,0);
}
void loop(){
int buttonState = digitalRead(buttonPin);//read the state of the buttonPin and store it as 0 or 1
if((millis() - lastPress > debounceTime)) //if the time between the last buttonChange is greater than the debounceTime
{
lastPress = millis(); //update last press
}
if(buttonState == 0 && lastButtonState == 1); //if the time between the last buttonChange is greater than the debounceTime
{
counter++;//increment by one
EEPROM.write(0, counter);//write counter to address 0
digitalWrite(ledPin, HIGH);//momentary LED
lastButtonState = 0; //record the lastButtonState
//print the results
Serial.print("Counter: ");
Serial.print(counter);
}
if (buttonState == 1 && lastButtonState ==0)// if buttom is not pressed, and was pressed last change
{
lastButtonState = 1; // record the lastButtonState
digitalWrite(ledPin, LOW); //momentary LED
}
}