Using the EEPROM on ATTINY13

Hey everyone,

I’ve made an audio input selector with 4 inputs and 1 output with 2 CD4066BE IC’s and a MAX232CPE IC to generate a negative voltage to power the first two chips. The CD4066’s are basically 4 “relays” in one package that get switched in a certain order to connect one input with the output. To select a output, the signal wire of the desired input has to be set to HIGH and the other have to be set to LOW. I’m using a push button and a ATTINY13 to set the signal wires HIGH or LOW in a specific order to switch the inputs. Pushing that button will switch between the inputs.

All of the above is working perfectly. The only problem i’m having is, when the ATTINY power’s off or gets reset, the selected input won’t be remembered. I’ve tried using the following methods in combination with integer and byte datatypes with the variabel INPUTselection: EEPROM.write() and EEPROM.read(). I’ve also tried using the methods EEPROM.put() and EEPROM.get() also without any success. Does anyone see what I’m doing wrong or what I could try to get it working?

Thanks,
Pieter-Jan

My code:

#include <Bounce2.h>
#include <EEPROM.h>

#define input1_PIN 0
#define input2_PIN 1
#define input3_PIN 2
#define input4_PIN 3
#define button1_PIN 4

Bounce debouncer1 = Bounce();

byte INPUTselection = 0;
int previousValue = HIGH;

void setup() {
  pinMode(input1_PIN, OUTPUT);
  pinMode(input2_PIN, OUTPUT);
  pinMode(input3_PIN, OUTPUT);
  pinMode(input4_PIN, OUTPUT);
  pinMode(button1_PIN, INPUT);
  digitalWrite(input1_PIN, LOW);
  digitalWrite(input2_PIN, LOW);
  digitalWrite(input3_PIN, LOW);
  digitalWrite(input4_PIN, LOW);
  
  debouncer1.attach(button1_PIN);
  debouncer1.interval(5); 
  INPUTselection = EEPROM.read(20);
//  Serial.begin(9600);
}

void loop() {
  debouncer1.update();

  int value = debouncer1.read();
  
  if (value != previousValue) {
    if (value == LOW) {
    
      INPUTselection++;
      if (INPUTselection > 4){
        INPUTselection = 1;
      }
  
      switch(INPUTselection){
        case 1: digitalWrite(input1_PIN, HIGH);
                digitalWrite(input2_PIN, LOW);
                digitalWrite(input3_PIN, LOW);
                digitalWrite(input4_PIN, LOW);
//                Serial.println("INPUT 1 is nu geselecteerd!");
                break;
        case 2: digitalWrite(input1_PIN, LOW);
                digitalWrite(input2_PIN, HIGH);
                digitalWrite(input3_PIN, LOW);
                digitalWrite(input4_PIN, LOW);
//                Serial.println("INPUT 2 is nu geselecteerd!");
                break;
        case 3: digitalWrite(input1_PIN, LOW);
                digitalWrite(input2_PIN, LOW);
                digitalWrite(input3_PIN, HIGH);
                digitalWrite(input4_PIN, LOW);
//                Serial.println("INPUT 3 is nu geselecteerd!");
                break;
        case 4: digitalWrite(input1_PIN, LOW);
                digitalWrite(input2_PIN, LOW);
                digitalWrite(input3_PIN, LOW);
                digitalWrite(input4_PIN, HIGH);
//                Serial.println("INPUT 4 is nu geselecteerd!");   
                break; 
      }
    }
  }
  
  if (previousValue != value){
    EEPROM.write(20, INPUTselection);
  } 
  previousValue = value;
}

I would move the EEPROM.write(20, INPUTselection) to right before the switch(INPUTselection). In other words, write to EEPROM directly after changing the value. That might fix your Problem and I think you are currently writing to EEPROM twice. Once on button press, once on button release. Remember that writes to EEPROM are limited so no sense wasting writes.

Thanks for your help!

I found the problem. The arduino only switches the input pins of the CD4066 when the button is pushed. When void setup() is run, it just changes the variable INPUTselection and the Switch-case won’t be triggered. I’ve now put the switch-case in a seperate method changeINPUT(). I also changed the EEPROM.write() to the other If statement like you suggested. I don’t know why I had put it there in the first place :o

Here’s my new code:

#include <Bounce2.h>
#include <EEPROM.h>

#define input1_PIN A0
#define input2_PIN A1
#define input3_PIN A2
#define input4_PIN A3
#define button1_PIN A4
#define EEPROMadress 0

Bounce debouncer1 = Bounce();

int INPUTselection;
int previousValue = HIGH;

void setup() {
  Serial.begin(9600);
  
  pinMode(input1_PIN, OUTPUT);
  pinMode(input2_PIN, OUTPUT);
  pinMode(input3_PIN, OUTPUT);
  pinMode(input4_PIN, OUTPUT);
  pinMode(button1_PIN, INPUT);
  digitalWrite(input1_PIN, LOW);
  digitalWrite(input2_PIN, LOW);
  digitalWrite(input3_PIN, LOW);
  digitalWrite(input4_PIN, LOW);
  
  debouncer1.attach(button1_PIN);
  debouncer1.interval(5); 
  INPUTselection = 1;
  INPUTselection = byte(EEPROM.read(EEPROMadress));
  changeINPUT();
}

void loop() {
  debouncer1.update();

  int value = debouncer1.read();
  
  if (value != previousValue) {
    if (value == LOW) {
    
      INPUTselection++;
      if (INPUTselection > 4){
        INPUTselection = 1;
      }
      EEPROM.write(EEPROMadress, int(INPUTselection));
      changeINPUT();
    }
  }
  previousValue = value;
}

void changeINPUT(){
  switch(INPUTselection){
        case 1: digitalWrite(input1_PIN, HIGH);
                digitalWrite(input2_PIN, LOW);
                digitalWrite(input3_PIN, LOW);
                digitalWrite(input4_PIN, LOW);
                Serial.println("INPUT 1 is nu geselecteerd!");
                break;
        case 2: digitalWrite(input1_PIN, LOW);
                digitalWrite(input2_PIN, HIGH);
                digitalWrite(input3_PIN, LOW);
                digitalWrite(input4_PIN, LOW);
                Serial.println("INPUT 2 is nu geselecteerd!");
                break;
        case 3: digitalWrite(input1_PIN, LOW);
                digitalWrite(input2_PIN, LOW);
                digitalWrite(input3_PIN, HIGH);
                digitalWrite(input4_PIN, LOW);
                Serial.println("INPUT 3 is nu geselecteerd!");
                break;
        case 4: digitalWrite(input1_PIN, LOW);
                digitalWrite(input2_PIN, LOW);
                digitalWrite(input3_PIN, LOW);
                digitalWrite(input4_PIN, HIGH);
                Serial.println("INPUT 4 is nu geselecteerd!");   
                break; 
      }
}