Displaying data from EEPROM

Hello, my code functions almost perfectly. However, I'm trying to get the TM1637 to display the value saved for (EEPROM.get(address1, Saves[0]); // bank A) on boot up. So far, it displays a default of ''A 00''....
Should correcet this in the loop function with if statements?

#include "TM1637.h"  // include TM1637 library
#include <EEPROM.h>
#include <PinButton.h>

#define CLK 2  // define TM1637 clock pin
#define DIO 3  // define TM1637 data pin

// initialize the TM1637 library
TM1637 tm1637(CLK, DIO);

//myButtonDN single click (de-increment preset)
//myButtonUP single click (increment preset)
//myButtonDN double click (increment bank)
//myButtonUP & myButtonDN pressed together (save preset)
PinButton myButtonUP(4);
PinButton myButtonDN(5);

int num = 0, prev_num = 1;
int let = 0, prev_let = 1;
int i = 0, prev_i = 1;

const unsigned int numReadings = 3;
int storedValues[numReadings];
int Saves[numReadings];

int address1 = 0; // bank A // adresses need two spaces for ints! One for bytes
int address2 = 2; // bank B
int address3 = 4; // bank C 

void setup() {

  Serial.begin(9600);
  // initialize the TM1637 display
  tm1637.init();
  // set display brightness (from 0 to 7)
  tm1637.set(3);

  EEPROM.get(address1, Saves[0]); // bank A
  EEPROM.get(address2, Saves[1]); // bank B
  EEPROM.get(address3, Saves[2]); // bank C  

  Serial.println(F("save the presets to EEPROM each time the button is pressed."));
}

void loop() {

  myButtonUP.update();
  myButtonDN.update();
  int channel = let;  // A B or C, midi chan, 1, 2 or 3! (pedal 1 2 or 3!)

  if (num != prev_num) {
    // if the displayed (current) number was changed
    prev_num = num;  // save current value of 'num'
    int program = num;
    // print all data
    Serial.println(Saves[let]);
    usbMIDI.sendProgramChange(program, (channel + 1));  // +1 means A = 1 etc!

    tm1637.display(2, num / 10 % 10);  // print tens digit
    tm1637.display(3, num % 10);       // print ones digit
    //delay(200);                        // wait 200 milliseconds
  }

  if (let != prev_let) {
    // if the displayed (current) number was changed
    prev_let = let;  // save current value of 'let'
    // print all data
    tm1637.display(0, (let + 10));  //this gives me 10, 11 & 12 (A,b & C)
    //delay(200);  // wait 200 milliseconds
  }

  if (i != prev_i) {
    // if the displayed (current) number was changed
    prev_i = i;  // save current value of 'let'
    //delay(200);  // wait 200 milliseconds
  }

  if (myButtonUP.isSingleClick()) {
    // if the UP button is presses
    num++;         // increment 'num'
    if (num > 99)  //99
      num = 0;
  }

  if (myButtonDN.isSingleClick()) {
    // if the DN button is presses
    num--;  // decrement 'num'
    if (num < 0)
      num = 99;  //99
  }
    int program = num;

  if (myButtonDN.isDoubleClick()) {
    Serial.println("double");
    let++;        // increment 'let'
    if (let > 2)  // >= 3
      let = 0;
    num = Saves[let];
    usbMIDI.sendProgramChange(program, (channel - 1)); 
    Serial.println(Saves[let]);
    tm1637.display(2, Saves[let] / 10 % 10);  // print tens digit
    tm1637.display(3, Saves[let] % 10);       // print ones digit
  }

  delay(50);
  if (myButtonUP.isLongClick() && myButtonDN.isLongClick()) {
    Serial.println("long");
    Saves[let] = num;
    i++;
    if (let > 2)  // >= 3
      let = 0;
    Serial.println(F("Saving presets to EEPROM"));
    //******** add serial print show what will be saved
    for (byte i = 0; i < 3; i++) {
      Serial.println(Saves[i]);
    }
    EEPROM.put(address3, Saves[2]);  // bank C
    EEPROM.put(address2, Saves[1]);  // bank B
    EEPROM.put(address1, Saves[0]);  // bank A
  }
}

What sort of Arduino do you have?

1 Like

Damn! I always forget that!
I'm using a Teensy 2

separate the processing, reading and display.

if you're not sure about the values read from the EEPROM, display them first on the serial monitor.

when displaying things on the 7 seg display, i would capture the values to display on each digit in a 4 byte buffer and write a routine to display the buffer

not obvious if you want to support only displaying digits or segments

1 Like

I will minimalise the code to make it easier. There's no trouble reading from the eeprom and displaying on the tm1637. My issue is simply how to adjust the code to allow a saved value to show when arduino is first switched on but change accordingly when altering the values via the buttons. I'm guessing the solution would be around one of the "if" statements but I'm uncertain as to writing that.

Which "if"?
Display a default value on startup.
Update the display whenever there is AA button press. No harm if it's unnecessary

1 Like

If I put this

tm1637.display(2, Saves[let] / 10 % 10);  // print tens digit
    tm1637.display(3, Saves[let] % 10);       // print ones digit

at the beginning of the loop then it will give me what I need on startup. Of course, any selection I make with the UP & DN buttons (single click, displaying 0-99) won't display as the code is looping. Unless I press save, then the display will of course display the current saved value in the eeprom
So I'm thinking that this

tm1637.display(2, Saves[let] / 10 % 10);  // print tens digit
    tm1637.display(3, Saves[let] % 10);       // print ones digit

needs to be part of one of the button press IF statements. Does that make sense?

I'll give this a shot!

So I've tried a couple of things with the code and this is what I have so far.


void loop() {

  myButtonUP.update();
  myButtonDN.update();
  int channel = let;  // A B or C, midi chan, 1, 2 or 3! (pedal 1 2 or 3!)

   if (num == 0) {
    tm1637.display(2, Saves[let] / 10 % 10);  // print tens digit
    tm1637.display(3, Saves[let] % 10);       // print ones digit
   }
   
  else if (num != prev_num) {
    // if the displayed (current) number was changed
    prev_num = num;  // save current value of 'num'
    //int program = num;
    // print all data
    //Serial.println(Saves[let]);
    //usbMIDI.sendProgramChange(program, (channel + 1));  // +1 means A = 1 etc!

    tm1637.display(2, num / 10 % 10);  // print tens digit
    tm1637.display(3, num % 10);       // print ones digit
    //delay(200);                        // wait 200 milliseconds
  }

On startup I get the preset that was saved in the eeprom which is perfect.
However, if I press the up button counter goes to 2, down press it goes to 1, and a down press again displays the same preset as startup. It appears each of the presets replace the "00" position. Any ideas?

post your entire code

1 Like
#include "TM1637.h"  // include TM1637 library
#include <EEPROM.h>
#include <PinButton.h>

#define CLK 2  // define TM1637 clock pin
#define DIO 3  // define TM1637 data pin

// initialize the TM1637 library
TM1637 tm1637(CLK, DIO);

//myButtonDN single click (de-increment preset)
//myButtonUP single click (increment preset)
//myButtonDN double click (increment bank)
//myButtonUP & myButtonDN pressed together (save preset)
PinButton myButtonUP(4);
PinButton myButtonDN(5);

int num = 0, prev_num = 1;
int let = 0, prev_let = 1;
int i = 0, prev_i = 1;

const unsigned int numReadings = 3;
int storedValues[numReadings];
int Saves[numReadings];

int address1 = 0; // bank A // adresses need two spaces for ints! One for bytes
int address2 = 2; // bank B
int address3 = 4; // bank C

int channel = let;
int program = num;

void setup() {

  Serial.begin(9600);
  // initialize the TM1637 display
  tm1637.init();
  // set display brightness (from 0 to 7)
  tm1637.set(3);

  EEPROM.get(address1, Saves[0]); // bank A
  EEPROM.get(address2, Saves[1]); // bank B
  EEPROM.get(address3, Saves[2]); // bank C

  //usbMIDI.sendProgramChange(channel + 0,Saves[0] ); //
  //usbMIDI.sendProgramChange(channel + 1,Saves[1] ); //
  //usbMIDI.sendProgramChange(channel + 2,Saves[2] ); //

  Serial.println(Saves[0]);
  Serial.println(Saves[1]);
  Serial.println(Saves[2]);

  //Serial.println(F("save the presets to EEPROM each time the button is pressed."));
}

void loop() {

  myButtonUP.update();
  myButtonDN.update();
  int channel = let;  // A B or C, midi chan, 1, 2 or 3! (pedal 1 2 or 3!)

   if (num == 0) {
    tm1637.display(2, Saves[0] / 10 % 10);  // print tens digit
    tm1637.display(3, Saves[0] % 10);       // print ones digit
   }
   
  else if (num != prev_num) {
    // if the displayed (current) number was changed
    prev_num = num;  // save current value of 'num'
    //int program = num;
    // print all data
    //Serial.println(Saves[let]);
    //usbMIDI.sendProgramChange(program, (channel + 1));  // +1 means A = 1 etc!

    tm1637.display(2, num / 10 % 10);  // print tens digit
    tm1637.display(3, num % 10);       // print ones digit
    //delay(200);                        // wait 200 milliseconds
  }
  

  if (let != prev_let) {
    // if the displayed (current) number was changed
    prev_let = let;  // save current value of 'let'
    // print all data
    tm1637.display(0, (let + 10));  //this gives me 10, 11 & 12 (A,b & C)
    //delay(200);  // wait 200 milliseconds
    
  }

  if (i != prev_i) {
    // if the displayed (current) number was changed
    prev_i = i;  // save current value of 'let'
    //delay(200);  // wait 200 milliseconds
  }

  if (myButtonUP.isSingleClick()) {
    // if the UP button is presses
    num++;         // increment 'num'
    if (num > 99)  //99
      num = 0;
      Serial.println(num);
  }

  if (myButtonDN.isSingleClick()) {
    // if the DN button is presses
    num--;  // decrement 'num'
    if (num < 0)
      num = 99;  //99
      Serial.println(num);
  }
  //int program = num;

  if (myButtonDN.isDoubleClick()) {
    Serial.println("double");
    let++;        // increment 'let'
    if (let > 2)  // >= 3
      let = 0;
    num = Saves[let];
    //usbMIDI.sendProgramChange(program, (channel - 1));
    Serial.println(Saves[let]);
    //tm1637.display(2, Saves[let] / 10 % 10);  // print tens digit
    //tm1637.display(3, Saves[let] % 10);       // print ones digit
  }

  delay(50);
  if (myButtonUP.isLongClick() && myButtonDN.isLongClick()) {
    Serial.println("long");
    Saves[let] = num;
    i++;
    if (let > 2)  // >= 3
      let = 0;
    Serial.println(F("Saving presets to EEPROM"));
    //******** add serial print show what will be saved
    for (byte i = 0; i < 3; i++) {
      Serial.println(Saves[i]);
    }
    EEPROM.put(address3, Saves[2]);  // bank C
    EEPROM.put(address2, Saves[1]);  // bank B
    EEPROM.put(address1, Saves[0]);  // bank A
  }
}

i would write a small program that just exercises your button library to inc/decrement num to verify that you understand those functions correctly using the serial monitor

1 Like

@gcjr yes I think your right. Best to take the slow road on this. Thanks for your help.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.