// Name:Karl Edward Sarmiento
// Purpose:Learn how the EEPROM works. Also, to automatically adjust the delay of by giving an equation that will less the frequency of the led.
// Commentary: In this lab, I learned the purpose of EEPROM which it keeps the memory of the the codes when the arduino is turned off. The EEPROM will still continue to read the last code it was been before it was turned off, then proceed again.
#include <EEPROM.h>
int redLed=8;
int address=EEPROM.read(0);
int bytes;
double Hertz=EEPROM.read(1);
int Turn_ON=EEPROM.read(2);
int blink_rate;
void setup() {
// put your setup code here, to run Turn_ONce:
pinMode(redLed, OUTPUT); // set pin 8 as an output
Serial. begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (address == 255){
Hertz = 0.5;
Turn_ON = 0;
}
while(1){
bytes = address;
blink_rate = 1/Hertz * 1000; // equation to decrease the frequency of the led
EEPROM.write(0, bytes);
EEPROM.write(1, Hertz);
EEPROM.write(2, Turn_ON);
digitalWrite(redLed, HIGH);
delay(blink_rate);
digitalWrite(redLed, LOW);
delay(blink_rate);
Serial.println(bytes);
Serial.println(blink_rate);
Serial.println(Hertz);
if (Hertz == 10 and Turn_ON == 0){ // when Hertz reached 10 the, it will start the cycle from 10 to 1
Turn_ON = 1;
digitalWrite(redLed, HIGH);
delay(1000);
digitalWrite(redLed, LOW);
delay(1000);
}
if (Turn_ON == 1){
Hertz = Hertz - 0.5; // the hertz is subtracting 0.5 until it reached 1
} else { // while it does not reached 10 the frequency is increasing 1/hertz * 1000
Hertz = Hertz + 0.5; // the hertz is adding 0.5 until it reached 10
}
if (Hertz == 1 and Turn_ON == 1){
blink_rate = 1/Hertz * 1000;
digitalWrite(redLed, HIGH);
delay(1000);
digitalWrite(redLed, LOW);
delay(1000);
Serial.println(blink_rate);
Serial.println(Hertz);
Turn_ON = 0;
Hertz = 0.5;
}
address = address - 1;
}
}