Deleting code in sketch doesn't change outcome

`


// define the LED digit patterns, from 0 - 13
// 1 = LED on, 0 = LED off, in this order:
//74HC595 pin     Q0,Q1,Q2,Q3,Q4,Q5,Q6,Q7 
byte seven_seg_digits[14] = { 
  B01111010,  // = D
  B10011100,  // = C
  B00111110,  // = B
  B11101110,  // = A
  B11100110,  // = 9
  B11111110,  // = 8
  B11100000,  // = 7
  B10111110,  // = 6
  B10110110,  // = 5
  B01100110,  // = 4
  B11110010,  // = 3
  B11011010,  // = 2
  B01100000,  // = 1
  B11111100,  // = 0
                                                  
 };
 
// connect to the ST_CP of 74HC595 (pin 9,latch pin)
int latchPin = 9;
// connect to the SH_CP of 74HC595 (pin 10, clock pin)
int clockPin = 10;
// connect to the DS of 74HC595 (pin 8)
int dataPin = 8;
 
void setup() {
  // Set latchPin, clockPin, dataPin as output
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}
 
// display a number on the digital segment display
void sevenSegWrite(byte digit) {
  // set the latchPin to low potential, before sending data
  digitalWrite(latchPin, LOW);
     
  // the original data (bit pattern)
  shiftOut(dataPin, clockPin, LSBFIRST, seven_seg_digits[digit]);  
 
  // set the latchPin to high potential, after sending data
  digitalWrite(latchPin, HIGH);
}
 
void loop() {       
  // count from 14 to 0
  for (byte digit = 14; digit > 0; --digit) {
    delay(1000);
    sevenSegWrite(digit - 1); 
  }
   
  // suspend 4 seconds
  delay(5000);
}

The last part I deleted to keep the display illuminated. It extinguished about same time it did before deleting.

Did I copy this correctly? I went to sketch, "select all", then copied for forum.

Yes.

To erase a sketch from the Arduino, upload the minimal sketch:

void setup(){}
void loop(){}

I don't know what happened to my question. I thought that by deleting the following portion of code from the sketch that I'd keep the display illuminated. What did I do wrong?

... "// suspend 4 seconds
delay (5000);
}"

You removed a needed close-brace, so the sketch will not compile (therefore, not run).

1 Like

You can't delete the {

I didn't delete that. I deleted the delay sequence above it only, then tested it with --> it successfully compiled and then I loaded it with no change.

What change did you expect?

I was expecting that by deleting the delay that the display would remain lit. The code statement states suspend in 4 seconds. If the code that suspends is deleted then what change should I expect?

Do you mean that the code still delayed to 5 sec after counting digits to zero?

yes. it did just as before, no change. all segments lit brightly, then extinguished. I'm new... this is one of the early lessons. i'm trying to play around with the code to get a different outcome, mainly to stay illuminated. deleting the delay (descriptive text calls it suspend) seemed a good bet. This will help me rule out pwr issues earlier I was having and instructions that omitted some pins on the shift register, leaving them unpinned. I posted that elsewhere.

I don't know how much you really care, but your sketch seems to be written in a confusing way, and some of the comments do not seem to make sense. For example:

  // suspend 4 seconds
  delay(5000);

Which is your intent, a delay of four seconds, or one of five seconds?

I also notice that your seven_seg_digits array is backward. Is there any reason for this? It seems like it would be a lot less confusing to have each digit in its own array position (for example, digit 0 in array position 0, and then digit 1 in array position 1, and so forth), so I straightened it out to make it that way.

From the comments in your sketch, I assume that your intent is to have a 14-second countdown, is that right?
I have attempted to rewrite your sketch to behave as what I believe you wanted.

// define the LED digit patterns, from 0 - 14
// 1 = LED on, 0 = LED off, in this order:
//74HC595 pin     Q0,Q1,Q2,Q3,Q4,Q5,Q6,Q7 
byte seven_seg_digits[15] = {
  B11111100,  // = 0
  B01100000,  // = 1
  B11011010,  // = 2
  B11110010,  // = 3
  B01100110,  // = 4
  B10110110,  // = 5
  B10111110,  // = 6
  B11100000,  // = 7
  B11111110,  // = 8
  B11100110,  // = 9
  B11101110,  // = A
  B00111110,  // = B
  B10011100,  // = C
  B01111010,  // = D
  B10011110,  // = E                                       
};
 
// connect to the ST_CP of 74HC595 (pin 9,latch pin)
int latchPin = 9;
// connect to the SH_CP of 74HC595 (pin 10, clock pin)
int clockPin = 10;
// connect to the DS of 74HC595 (pin 8)
int dataPin = 8;
 
void setup() {
  // Set latchPin, clockPin, dataPin as output
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}
 
// display a number on the digital segment display
void sevenSegWrite(byte digit) {
  // set the latchPin to low potential, before sending data
  digitalWrite(latchPin, LOW);
     
  // the original data (bit pattern)
  shiftOut(dataPin, clockPin, LSBFIRST, seven_seg_digits[digit]);  
 
  // set the latchPin to high potential, after sending data
  digitalWrite(latchPin, HIGH);
}
 
void loop() {       
  // count from 14 to 0
  for (int digit = 14; digit >= 0; --digit) {
    sevenSegWrite((byte)digit);
    delay(1000);
  }
   
  // suspend 4 seconds
  delay(4000);
}
1 Like

It looks like your code in the board did not update.
Sorry for asking that, but did you upload the code after changing?
Please show an upload diagnostic message.

@odometer
Another thing I would change - replace the preincrement --digit to post- one in the for loop.

I originally posted this in my code

  // count from 14 to 0
  for (int digit = 14; digit >= 0; --digit) {
    sevenSegWrite(digit);
    delay(1000);
  }

but I wasn't sure if it would run properly, so I changed it to

  // count from 14 to 0
  for (int digit = 14; digit >= 0; --digit) {
    sevenSegWrite((byte)digit);
    delay(1000);
  }

Sorry if there's any confusion. I myself am not the best programmer, and I don't have hardware to test on.

I don't mind at all. I appreciate the help! I did update after checking the code first to see if the deletion broke the sequence. I then loaded and the board did what it does when successfully loading.

Because you asked, I loaded the sketch, deleted the portion that suspends, did the compile check, loaded, unplugged the usb and reinserted it and I got missing segments. tried again, some more missing segments. tried again and got the full 7 segments illuminated plus the dp. extinguishes in about 4 seconds. i've read around some old related posts and people talk about adding 1 capacitor and another says 2. Not what the lesson asks for. Lesson doesn't tell me expected result, but since it has the delay to "suspend" I suppose that's what it's supposed to do. I'm trying to eliminate the variables so I can play with the code a bit. I've got a project book that I wrote up to help me troubleshoot but I didn't get any feedback on the questions I highlighted in red. I can upload the latest if you want to see what I mean.

Does that actually make a difference? Or is that just style?
I know, for example, that it would be a mistake (undefined behavior) to write digit = --digit there,
but what does digit-- do that --digit doesn't, or vice versa?

Are you asking me? I've read about the =-- somewhere but I'm not versed in code enough to know the significance of it yet. Let me look. I'm going to upload my project book because it has some other code examples that might help. what I learned previously is that these tutorials from RexQualis and Elegoo, and maybe even Arduino don't necessarily get things completely right. I felt blindsided by this cause hey the kit is cookie cutter tried and tried again, right? This attachment has text highlighted and in red to draw attention to what was missing. I'm looking at the code you mentioned now to see what your asking.
1 Digit 7 Segment Display.pdf (1.9 MB)

My God.
So that code you showed us in post #1 was being presented as an example in a textbook for people to learn from?!
Good Lord! What has this world come to?!

Huh? Why, it's a beginner's electronics kit and I'm a beginner. I got stumped and have been trying to get some fun out of it but have mired myself in feet first in the mud! I thought it was me and I'm too old to learn.

Hey that RexQualis Tutorial uses terminology for the shift register pins that are different from the shift register I got. For instance, my shift register doesn't have Q0, it's QA. Doesnt seem offhand to make a difference except the code gets into hex equivalents... maybe I'm tired, but the hex equivalent for a 0 and an A are different numbers? Does this make sense? I'm just grasping, but does it matter whether I pulled a shift register 74HC595 from the Elegoo kit and use it for a RexQualis tutorial?