Hardware components | ||||||
![]() |
| × | 1 | |||
![]() |
| × | 4 | |||
![]() |
| × | 3 |
In this tutorial I will show you how to use Arduino built in eeprom to preserve data so it is not wiped out when Arduino is restarted.We will be writing single byte values, sequence of byte values but also writing more complex data structures to that memory.This video also provides information about all memory pools at your disposal when programing your arduino.
Here is the link to the full tutorial
#include <EEPROM.h>
byte led_to_lit=0;
byte led_lit=0;
void setup() {
Serial.begin(9600);
pinMode(A1,INPUT_PULLUP);
pinMode(A2,INPUT_PULLUP);
pinMode(A3,INPUT_PULLUP);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
led_to_lit=EEPROM.read(0);
digitalWrite(led_to_lit,HIGH);
}
void loop() {
if(digitalRead(A1)==0) led_to_lit=7;
if(digitalRead(A2)==0)led_to_lit=6;
if(digitalRead(A3)==0)led_to_lit=5;
if(led_to_lit!=0 and led_to_lit!=led_lit){
if (led_to_lit==7){digitalWrite(7,HIGH);digitalWrite(6,LOW);digitalWrite(5,LOW);EEPROM.write(0, 7);}
if (led_to_lit==6){digitalWrite(7,LOW);digitalWrite(6,HIGH);digitalWrite(5,LOW);EEPROM.write(0, 6); }
if (led_to_lit==5){digitalWrite(7,LOW);digitalWrite(6,LOW);digitalWrite(5,HIGH);EEPROM.write(0, 5);}
EEPROM.write(0,led_to_lit );
Serial.println(led_to_lit);
}
led_lit=led_to_lit;
}
#include <EEPROM.h>
boolean record=false;
boolean button_pressed=false;
int G=HIGH;
int R=HIGH;
int W=HIGH;
int End=HIGH;
int eeprom_address=0;
unsigned long Time=0;
void replay_the_sequence(){
for (int index = 0 ; index < EEPROM.length(); index++) {
digitalWrite(EEPROM.read(index),HIGH);
delay(1000);
digitalWrite(EEPROM.read(index),LOW);
delay(1000);
}
}
void setup() {
Serial.begin(9600);
pinMode(A1,INPUT_PULLUP);
pinMode(A2,INPUT_PULLUP);
pinMode(A3,INPUT_PULLUP);
pinMode(A0,INPUT_PULLUP);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
}
void loop() {
G=digitalRead(A1);
R=digitalRead(A2);
W=digitalRead(A3);
End=digitalRead(A0);
digitalWrite(7,!G);
digitalWrite(6,!R);
digitalWrite(5,!W);
if ((G==0 or R==0 or W==0)and record==false) {
for (int i = 0 ; i < 30 ; i++) EEPROM.write(i, 0);
record=true;
eeprom_address=0;
}
if (record==true and button_pressed==false){
if (G==0 or R==0 or W==0){
button_pressed=true;
if (G==0) EEPROM.write(eeprom_address,7);
if (R==0) EEPROM.write(eeprom_address,6);
if (W==0) EEPROM.write(eeprom_address,5);
eeprom_address++;
Time=millis();
}
}
if (G==1 and R==1 and W==1 and millis()-Time>300) button_pressed=false;
if (End==0 and record==true) record=false;
if (End==0 and record==false)replay_the_sequence();
}
#include <EEPROM.h>
struct LED_PLUS_TIME {
int PIN;
unsigned long Interval;
};
boolean record=false;
boolean button_pressed=false;
int G=HIGH;
int R=HIGH;
int W=HIGH;
int End=HIGH;
int index=0;
int eeprom_address=0;
unsigned long Time=0;
LED_PLUS_TIME LED;
LED_PLUS_TIME L_AND_T;
void replay_the_sequence(){
eeprom_address=0;
for (int i = 0 ; i < 10; i++) {
EEPROM.get(eeprom_address,L_AND_T);
digitalWrite(L_AND_T.PIN,HIGH);
delay(L_AND_T.Interval);
digitalWrite(L_AND_T.PIN,LOW);
delay(1000);
eeprom_address+=sizeof(L_AND_T);
}
}
void setup() {
Serial.begin(9600);
pinMode(A1,INPUT_PULLUP);
pinMode(A2,INPUT_PULLUP);
pinMode(A3,INPUT_PULLUP);
pinMode(A0,INPUT_PULLUP);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
}
void loop() {
G=digitalRead(A1);
R=digitalRead(A2);
W=digitalRead(A3);
End=digitalRead(A0);
digitalWrite(7,!G);
digitalWrite(6,!R);
digitalWrite(5,!W);
if ((G==0 or R==0 or W==0)and record==false) {
for (int i = 0 ; i < 100; i++) EEPROM.update(i, 0);
record=true;
eeprom_address=0;
index=0;
}
if (record==true and button_pressed==false){
if (G==0 or R==0 or W==0){
button_pressed=true;
if (G==0) LED.PIN=7;
if (R==0) LED.PIN=6;
if (W==0) LED.PIN=5;
Time=millis();
}
}
if (G==1 and R==1 and W==1 and millis()-Time>300 and button_pressed==true ) {
LED.Interval= millis()-Time;
EEPROM.put(eeprom_address,LED);
eeprom_address+=sizeof(LED);
button_pressed=false;
index++;
}
if (End==0 and record==true) record=false;
if (End==0 and record==false)replay_the_sequence();
}
Comments
Please log in or sign up to comment.