[working] Using variables like memory

hi,

i have a max7219 correctly wired up to display on a 3x7 segment display, all is good there.

i wish to visually represent 4 [boxes] each with 4 levels of [mod], and to do this im using a single 7 segment display, with;

E Box1, F Box2, B Box3, C Box4 and D = mod1, G mod2 and A mod3

i wish to SET the 4 boxes with their own mod level, so for instance,

Box 1 will be level 1 Box 2, level 2 Box 3, level 4 and Box 4, level 3.

The below code, runs like which ever level you set for 1 box, it sets for all boxes.

i wish to have the 4 boxes at DIFFERENT levels, and when rotating through, the levels represented.

so if i set, 1=1, 2=1, 3=0 and 4=4, then pressing button 1 will cycle through those presets, and button 2 will increase those levels.

video added for either amusement, or clarity...

youtuuuube

(excuse the thumb...!)

oh, theres only 2 buttons to control this thing...

button 2 is cycle through the levels of the mod button 1 is the box number, and save.

the idea, is when starting, box 1 is activated... using button2, select desired level, then button 1 to save and progress to box 2, save, progress etc...

when no buttons have been pressed for say 10 seconds, then continue with program...

#include "LedControl.h"

//11 data, 13 clock, 10 load, 1 max7219
LedControl lc = LedControl(11, 13, 10, 1);

#define BUTTON1 5
#define BUTTON2 6

long Time = 0;
long Debounce = 125;

unsigned long delaytime = 250;

byte heatSinkLayout[4][4] = { { 4,  12,  13,  77},
  { 2,  10,  11,  75},
  {32,  40,  41, 105},
  {16, 152, 153,  89}
};

int modPositionX[4] = { 4, 2, 32, 16 };
int currentPositionX;

//int i = 0;
int a = 0;
int b = 0;

void setup() {
  Serial.begin(9600);
  lc.shutdown(0, false); // The MAX72XX is in power-saving mode on startup, we have to do a wakeup call
  lc.setIntensity(0, 8); // Set the brightness to a medium values
  lc.clearDisplay(0);  // and clear the display

  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
  digitalWrite(BUTTON1, HIGH);       // turn on pullup resistors
  digitalWrite(BUTTON2, HIGH);       // turn on pullup resistors
}

void loop() {

  updateDisplay();

  if (digitalRead(BUTTON1) == LOW && (millis() - Time > Debounce) ) {
    Time = millis();
    currentPositionX = heatSinkLayout[a][b];
    a++;
  }

  if (digitalRead(BUTTON2) == LOW && (millis() - Time > Debounce) ) {
    Time = millis();
    currentPositionX = heatSinkLayout[a][b];
    b++;
  }


  if (a > 3) a = 0;
  if (b > 3) b = 0;

}

void updateDisplay() {
  lc.setRow(0, 0, currentPositionX);
}

thanks!

i wish to visually represent 4 [boxes] each with 4 levels of [mod], and to do this im using a single 7 segment display, with;

You lost me. What is a [box] and what is a [mod] ?

UKHeliBob: You lost me. What is a [box] and what is a [mod] ?

yeah, whoops...

ok, so i have 4 boxes, with 4 possible states for each box. now to represent this on a 7 segment display im using E, F, B and C (in the video, this i me using button 1 to cycle through the 4 boxes, note the led changing around the 7 segment.

and for the 4 states, im using G, A and D. during the video, i click the second button, and G lights up, and this is apparent across all 4 boxes.

I too am lost even after your last post. What has any of this got to do with “How to use variables as a memory” ? Variables ARE memory !

kelvinmead: so if i set, 1=1, 2=1, 3=0 and 4=4, then pressing button 1 will cycle through those presets, and button 2 will increase those levels.

as @UKHeliBob your post is very confusing however as you already did in your code you should be able to achieve this using array

say you have start positions {0,1,2,3,4,5}

then create a 2d array something like this: byte start_pos[4][6] = {{ 1, 2, 3, 4, 5, 0}, { 1, 2, 3, 4, 5, 0}, {0, 1, 2, 3, 4, 5}, {4, 5, 0, 1, 2, 3,} };

4 being your number of presets and 6 your number of start positions (level).

you whenever you press button 1 it should cycles through you preset with you intended initial level selected (if you reset that count to 0!) and button 2 cycles through the preset

Typing this, I think this might be your issue; in your code you are not resetting 'b' whenever your are pressing button1 to increment 'a'

sherzaad: Typing this, I think this might be your issue; in your code you are not resetting 'b' whenever your are pressing button1 to increment 'a'

thats the bit there i need to work on, reset the b to 0... that totally makes sense.

thanks!

stowite: I too am lost even after your last post. What has any of this got to do with "How to use variables as a memory" ? Variables ARE memory !

yeah, i couldn't think of a post title that made sense. sorry

kelvinmead: yeah, i couldn't think of a post title that made sense. sorry

It's not too late to update the title - just edit your Original Post.

As I can't figure what the problem is I don't have any suggestions. It does make me wonder if your failure to find a title reflects your own lack of clarity about the problem ?

(And that is not meant as a snide criticism - rather as a suggestion for more thinking :) )

...R

Robin2: It's not too late to update the title - just edit your Original Post.

As I can't figure what the problem is I don't have any suggestions. It does make me wonder if your failure to find a title reflects your own lack of clarity about the problem ?

(And that is not meant as a snide criticism - rather as a suggestion for more thinking :) )

...R

no offences taken, you guys are trying to help my jumbled mind!

and your correct about the clarity... i don't really know how to describe whats happening, or what i want it to do... so getting help is never going to be simple... the video to me makes perfect sense, but maybe i need to annotate it to make it clearer.

i have imovie, so...

annotated video....

I have looked at the video.

One button cycles around the 4 vertical bars of a 7-segment display and the other button cycles over the three horizontal bars and for a 4th state none of the horizontal bars is lit.

Now, what's the problem?

...R

Robin2: I have looked at the video.

One button cycles around the 4 vertical bars of a 7-segment display and the other button cycles over the three horizontal bars and for a 4th state none of the horizontal bars is lit.

Now, what's the problem?

...R

so whatever state is generated, it is copied across all the vertical bars.

each vertical bar, should have its own state... configured by the second button.

kelvinmead: so whatever state is generated, it is copied across all the vertical bars.

each vertical bar, should have its own state... configured by the second button.

Sorry, but I don't understand.

In the video the vertical bars are controlled by the first button. Are you saying that you want the whole thing to go through all the states using a single button. Something like 01 02 03 04 11 12 13 14 21 22 etc

...R

so i let this fizzle around my head for a couple of days, and took it back to the beginning, and rewrote it. pow, done in 10 seconds!

thanks all

// setup
int savea;
int saveb[4];

// main loop

  lc.setRow(0,0, heatSinkLayout[savea][saveb[a]]); // update display

  if (digitalRead(BUTTON1) == LOW && (millis() - Time > Debounce) ) { 
Time = millis(); 
savea=a; 
a++; 
b=0; }

  if (digitalRead(BUTTON2) == LOW && (millis() - Time > Debounce) ) { 
Time = millis(); 
saveb[a]=b; 
b++; }


  
if (a>3) a=0;
if (b>3) b=0;

video working!

So now you need to change variable names such as 'a' and 'b' to names that describe their roles in the program. Also, you need to as far as possible get rid of the magic numbers such as the 3 in

if (a>3) a=0;
if (b>3) b=0;

stowite: Also, you need to as far as possible get rid of the magic numbers such as the 3 in

if (a>3) a=0;
if (b>3) b=0;

magic numbers?

i googled before hitting the post button.

i see, so magic numbers are the numbers that when you come back to the program in 2 years, you'll have no idea why they are there, so use variables to define them, even if they have no intention of changing...

so;

state = 3;

if (mod>state) mod = 0;

or even

state=3;
wrapAround = 0;

if (mod>state) mod = wrapAround;

kelvinmead: i see, so magic numbers are the numbers that when you come back to the program in 2 years, you'll have no idea why they are there, so use variables to define them, even if they have no intention of changing...

The other (and more immediate) disadvantage of magic numbers is the problem of finding them all when you need to change a value.

If you use a named variable or constant you just need to make a change in one place. Even for trivial programs I find that important.

And in my case substitute "2 weeks" for "2 years" :)

...R