Common anode 7 segment display

Sorry to be clear I was trying to find a way to hold the first digit at 1 until the second digit reaches f then back to 0 and increment the first digit to a 2 and so on. Basically counting in hex.

Also another question. In my for loop I used i+1 but if I use i++ this doesn't work for me. What is the difference between i+1 and i++?

There are so many tutorial sites online, all you need to do is Google them.

i++; increments i
i+1; doesn’t do anything

On the other hand:
i = i+1; increments i (same as i += 1;)
i = i++; has 'undefined behavior' so it may do ANYTHING

1 Like

Thank you for this explanation I really appreciate the help. Sometimes when your new to something, trying to Google, doesn't always help because your new to it and you don't know exactly what to google and the things you do google are not what your looking for.

Yes, but you can Google, "C++" and basic language references and tutorials will be among the first to appear.

Also, '++' and many other basic C operators are documented on this site, in the reference section:
https://www.arduino.cc/reference/en/

I actually told you that, back in reply #4.

I wanted to update this post with my most recent code for any other beginners who might read this in the future. This was the smallest I could make it. I also changed out the uno to a nano. The answer to my question of how I could increment the other digits was found with nested for loops. I never knew this was a thing. After realising this, I was able to study the uses of nested for loops. I tried using various methods including millis() before I found the simple solution. I hope this helps some beginner one day.

#define dataPin 3
#define latchPin 4
#define clockPin 5
#define delayTime 500
byte digits[10]{129,243,73,97,51,37,5,241,1,33};
void setup() {
 pinMode(latchPin,OUTPUT);
 pinMode(dataPin,OUTPUT);
 pinMode(clockPin,OUTPUT);
}
void loop() {
 for (int l=0; l<=9; l++){
 for (int k=0; k<=9; k++){
 for (int j=0; j<=9; j++){ 
 for (int i=0; i<=9; i++){
digitalWrite(latchPin,LOW);
shiftOut(dataPin,clockPin,MSBFIRST,digits[i]);
shiftOut(dataPin,clockPin,MSBFIRST,digits[j]);
shiftOut(dataPin,clockPin,MSBFIRST,digits[k]);
shiftOut(dataPin,clockPin,MSBFIRST,digits[l]);
digitalWrite(latchPin,HIGH);
delay(delayTime);
 }
 }
 }
 }
}

Simple, until you really need to do something while the display is running (which is pretty common!). The implementation of nested logic in round robin multitasking is classically difficult. But it can be done. Here are some ideas:

Here is how you implement a for loop using a state machine:

for(i=3; i<6; i++) {<somecode>}

decomposes as the sequence

i=3; while (i<6) { <somecode> i++; }

For the state machine you would do something like

if (state == INITIALIZE) { i=3; state = RUN; } else if (state == RUN and i<6) { <somecode> i++; } else { state = IDLE; }

To activate it, you just assert:

state = INITIALIZE;

Or, you can use a switch case statement:

switch (state) {

case:INITIALIZE
   i=3;
   state = RUN;
   break;

case:RUN
 if (i<6) {
   <somecode>
   i++;
    }
  else
    {
    state = IDLE;
    }
   break;

case: IDLE
  break;
}

Nice wiring job, by the way!

1 Like

Funny, I was going to say that wiring is way too neat. :expressionless:

a7

1 Like

Almost like a fritzy..OOPPPs... :astonished:
Very well done on the protoboard wiring, makes hardware debug really easy if needed.

Tom... :grinning: :+1: :coffee: :australia:

1 Like

Thank you for the information. Also thanks for the wiring compliment. 21 years doing automotive electrics has made my wiring neat. I'm good with circuits. Just want learn code now.

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