Hello, I'm having a hard time to get my project work. It's a simple input selector. As far as storing EEPROM value is working fine i have problem to recall it after reboot because I'm forced to push the button to recall settings stored. I would appreciate any help.
#include <EEPROM.h>
const int btn = 5;
const int Out1 = 6;
const int Out2 = 7;
const int Out3 = 13;
volatile byte choice = 0;
boolean isPressed = false;
int EEPROM_Store = 24 ;
void setup() {
Serial.begin(9600);
pinMode(btn, INPUT_PULLUP);
pinMode(Out1, OUTPUT);
pinMode(Out2, OUTPUT);
pinMode(Out3, OUTPUT);
choice = EEPROM.read(EEPROM_Store);
Serial.println("Case number stored in EEPROM: ");
Serial.print(choice);
//
}
void loop ()
{
if (digitalRead(btn) == LOW && isPressed == false )
{
isPressed = true;
doSwitchStatement();
choice++;
if (choice > 2) {
choice = 0;
}
} else if (digitalRead(btn) == HIGH)
{
isPressed = false;
}
}
void doSwitchStatement() {
switch(choice) {
case 0:
digitalWrite(Out1, HIGH);
digitalWrite(Out2, LOW);
digitalWrite(Out3, LOW);
EEPROM.write(EEPROM_Store, choice) ;
Serial.println("case..0");
Serial.println(choice);
break;
case 1:
digitalWrite(Out1, LOW);
digitalWrite(Out2, HIGH);
digitalWrite(Out3, LOW);
EEPROM.write(EEPROM_Store, choice) ;
Serial.println("case..1");
Serial.println(choice);
break;
case 2:
digitalWrite(Out1, LOW);
digitalWrite(Out2, LOW);
digitalWrite(Out3, HIGH);
EEPROM.write(EEPROM_Store, choice) ;
Serial.println("case..2");
Serial.println(choice);
break;
}
}
Not clear why the btn push is needed. The code you posted has no btn input test.
I'm not sure what does it mean that "button have no input test". I'm guessing there is no "check" on program begin, but sorry I'm a newbie, and I've spent like 8 hours making it to this point. Can You bring some examples?
i have problem to recall it after reboot because I'm forced to push the button to recall settings stored.
What do you mean "forced".
Are you saying that you want to push a button press in setup to have this code executed only after the button is pressed
choice = EEPROM.read(EEPROM_Store);
Serial.println("Case number stored in EEPROM: ");
Serial.print(choice);
Hi
I tested your code with an arduino, and what I saw is that due to the bouncing of the button, the "choice" values vary very quickly.
I programmed a debouncing with a delay of 50mSec, and it worked as expected.
See your modified code to avoid bouncing.
#include <EEPROM.h>
const int btn = 5;
const int Out1 = 6;
const int Out2 = 7;
const int Out3 = 13;
volatile byte choice = 0;
boolean isPressed = false;
int EEPROM_Store = 24 ;
//-------------------------------------------------------
void setup() {
Serial.begin(9600);
pinMode(btn, INPUT_PULLUP);
pinMode(Out1, OUTPUT);
pinMode(Out2, OUTPUT);
pinMode(Out3, OUTPUT);
choice = EEPROM.read(EEPROM_Store);
Serial.println("Case number stored in EEPROM: ");
Serial.print(choice);
}
//-------------------------------------------------------
void loop ()
{
if (digitalRead(btn) == LOW && isPressed == false )
{
delay(50);
if (digitalRead(btn) == LOW && isPressed == false )
{
isPressed = true;
doSwitchStatement();
choice++;
if (choice > 2) {
choice = 0;
}
}
}
else if (digitalRead(btn) == HIGH)
{
isPressed = false;
}
}
//-------------------------------------------------------
void doSwitchStatement() {
switch (choice) {
case 0:
digitalWrite(Out1, HIGH);
digitalWrite(Out2, LOW);
digitalWrite(Out3, LOW);
EEPROM.write(EEPROM_Store, choice) ;
Serial.println("case..0");
Serial.println(choice);
break;
case 1:
digitalWrite(Out1, LOW);
digitalWrite(Out2, HIGH);
digitalWrite(Out3, LOW);
EEPROM.write(EEPROM_Store, choice) ;
Serial.println("case..1");
Serial.println(choice);
break;
case 2:
digitalWrite(Out1, LOW);
digitalWrite(Out2, LOW);
digitalWrite(Out3, HIGH);
EEPROM.write(EEPROM_Store, choice) ;
Serial.println("case..2");
Serial.println(choice);
break;
}
}
I mean I need to take an action (in this case I have to press the button) to turn the LED on. Arduino recalling correct case but button press is needed to flip state. After rebooting all output pins are low, and there is no high state at any outputs until I press the button.
Thank You! Work much better, but still have an issue with restoring "HIGH" state on saved value after reboot. All the time after power down and reboot i have to press the button to make any action - in this case - restoring "HIGH" value on stored "case".
Hi
Then see if this is what you want to happen with your code.
#include <EEPROM.h>
const int btn = 5;
const int Out1 = 6;
const int Out2 = 7;
const int Out3 = 13;
volatile byte choice = 0;
boolean isPressed = false;
int EEPROM_Store = 24 ;
//-------------------------------------------------------
void setup() {
Serial.begin(9600);
pinMode(btn, INPUT_PULLUP);
pinMode(Out1, OUTPUT);
pinMode(Out2, OUTPUT);
pinMode(Out3, OUTPUT);
choice = EEPROM.read(EEPROM_Store);
Serial.println("Case number stored in EEPROM: ");
Serial.print(choice);
doSwitchStatement();
}
//-------------------------------------------------------
void loop ()
{
if (digitalRead(btn) == LOW && isPressed == false )
{
delay(50);
if (digitalRead(btn) == LOW && isPressed == false )
{
isPressed = true;
doSwitchStatement();
choice++;
if (choice > 2) {
choice = 0;
}
}
}
else if (digitalRead(btn) == HIGH)
{
isPressed = false;
}
}
//-------------------------------------------------------
void doSwitchStatement() {
switch (choice) {
case 0:
digitalWrite(Out1, HIGH);
digitalWrite(Out2, LOW);
digitalWrite(Out3, LOW);
EEPROM.write(EEPROM_Store, choice) ;
Serial.println("case..0");
Serial.println(choice);
break;
case 1:
digitalWrite(Out1, LOW);
digitalWrite(Out2, HIGH);
digitalWrite(Out3, LOW);
EEPROM.write(EEPROM_Store, choice) ;
Serial.println("case..1");
Serial.println(choice);
break;
case 2:
digitalWrite(Out1, LOW);
digitalWrite(Out2, LOW);
digitalWrite(Out3, HIGH);
EEPROM.write(EEPROM_Store, choice) ;
Serial.println("case..2");
Serial.println(choice);
break;
}
}
Wow! Thank You! It's working great! <3 I owe You 
Do you understand the change made and why it worked and why your original program did not do what you wanted?
I think I get it. Adding “doSwitchStatement();” in program setup internally press a switch which engaging “choice” counter. Correct me if I’m wrong.
I'm not sure if i should write here, but is it possible to modify this program to add a "long press" which will engage a 4-th case? For example write all outputs LOW? (it's suppose to be audio signal selector so "mute" option can be useful) If so, where should I start? I've found this threat but in this case it's not working.
Adding button "long press" to switch case - Using Arduino / Programming Questions - Arduino Forum