Can we configure Pin functions after reading pin number from EEPROM?
Instead of hard-coding pin numbers, Can we save Pin numbers for different functions in EEPROM and read it just after powering ON Arduino Board to configure it.
I connected 7 LEDs from Pin number 0 to 7. Now I want to read pin number from EEPROM and then configure it for Output Pin mode and then blink it continuously. I found that LED is blinking but with very less brightness.
#include <EEPROM.h>
int led_pin = 0;
int led_pin_addr = 0;
void setup() {
led_pin = EEPROM.read(led_pin_addr );//Note: I already written number 0 at 0x00 location of EEPROM.
int i;
for(i=0;i<8;i++)
{
pinMode(led_pin, OUTPUT);
}
led_pin = led_pin + 1;
if(led_pin >=7)
{
led_pin = 0;
}
EEPROM.write(led_pin_addr , led_pin);
}
void loop() {
digitalWrite(led_pin, HIGH);
delay(500);
digitalWrite(led_pin, LOW);
delay(500);
}
If I am writing pin number in code then LED is blinking with good brightness.
So wait, what you want it to do is blink a led on pin 0 when it freshly uploaded. Then when you reset it it will blink led on pin 1, reset again and pin 2?
That looks like it should work, except this part makes no sense:
for(i=0;i<8;i++)
{
pinMode(led_pin, OUTPUT);
}
You only need to call pinMode(led_pin,OUTPUT) once. You're not even using i.
The problem you're encountering is that you're incrementing led_pin AFTER you set it output. Thus led_pin has a different value when loop starts running than it had when you set pinMode(), so the pin isn't set output.
Also, you should avoid using pins 0 and 1 for that, because putting LEDs on those pins may interfere with sketches being uploaded.
#include <EEPROM.h>
int led_pin = 0;
int led_pin_addr = 0;
void setup() {
led_pin = EEPROM.read(led_pin_addr );//Note: I already written number 0 at 0x00 location of EEPROM.
led_pin++; //same as led_pin=led_pin+1 (or led_pin+=1)
if(led_pin >=7)
{
led_pin = 0;
}
EEPROM.write(led_pin_addr , led_pin);
pinMode(led_pin, OUTPUT);
}
void loop() {
digitalWrite(led_pin, HIGH);
delay(500);
digitalWrite(led_pin, LOW);
delay(500);
}