7 Segment Digital Display Counting Problem *New Question Same Project

New Question is labeled below my first sketch.

Hi I literally just started working with Arduino in the last 48 hours and I love it so far, I decided to start a bit of my own work using a 7 segment digital display. I want it to count up from 1 to 9 but I can’t seem to get the counter to go up, it just throws all of my code together and ends up on 9 since I haven’t used one segment this early on. My code so far is:

int i = 0;
int wait = 1000;

void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT); // Top Left and Middle
pinMode(12, OUTPUT); // Bottom Left
pinMode(11, OUTPUT); // Bottom
pinMode(10, OUTPUT); // Bottom Right
pinMode(9, OUTPUT); // Top
pinMode(8, OUTPUT); // Top Right
}

void loop() {
i += 1; // Counter
if (i <= 1)
{
digitalWrite(10, HIGH); // 1
digitalWrite(8, HIGH);
}
else if (i <= 2)
{
digitalWrite(13, HIGH); // 2
digitalWrite(9,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11, HIGH);
}
else if (i <= 3)
{
digitalWrite(9,HIGH); // 3, need to find solution for Top Left and Middle being linked.
digitalWrite(8,HIGH);
digitalWrite(10,HIGH);
digitalWrite(11, HIGH);
}
}

All I want it to do is add 1 to i at every stage, I am just at a complete stand still as I am unable to figure it out. Any help would be greatly appreciated!

Thanks!

P.S. This is just the first 3 digits on the display, I labeled each segment at the top so turning 10 and 8 on looks like a 1 and 13, 11, 10 , and 9 looks like a 2 etc. I just haven’t finished the rest yet because I’d like to solve problems while its still somewhat simple to look at. :slight_smile:

NEW QUESTION:

Alright so I successfully built my 9 second timer using the 7 segment digital display, but there are some oddities that I can’t solve and I also want to add a button to reset the counter to 0.

Oddity:
Right now the value of “i” is increasing without the i += 1 line, I have looked through my sketch and have been unable to find a reason for the value of “i” rising at 1 second intervals and suddenly resetting once it hits 10. I’m assuming the end of the loop is the cause for “i” returning to 0, but why is “i” increasing in the first place?

RESET Button:
Okay so a counter wasn’t good enough, I have to add the button cause electronics aren’t fun if you can’t press something. My current dilemma is how to go about adding this button. I was attempting to add an if statement but that hasn’t worked so far. Here is my if idea

void loop(){
button = digitalRead(10);
if (button == HIGH)) {
i = 0;
} else {
if (i=1) {
ONE();
delay(1000);
}
if (i=2) {
TWO();
delay(1000);
}
if (i=3) {
THREE();
delay(1000);
}
if (i=4) {
FOUR();
delay(1000);
}
if (i=5) {
FIVE();
delay(1000);
}
if (i=6) {
SIX();
delay(1000);
}
if (i=7) {
SEVEN();
delay(1000);
}
if (i=8) {
EIGHT();
delay(1000);
}
if (i=9) {
NINE();
delay(1000);
}
}

This is all of my work combined:

int i = 0;

void setup() {
pinMode(2, OUTPUT); // Top
pinMode(3, OUTPUT); // Top Right
pinMode(4, OUTPUT); // Top Left
pinMode(5, OUTPUT); // Middle
pinMode(6, OUTPUT); // Bottom Right
pinMode(7, OUTPUT); // Bottom Left
pinMode(8, OUTPUT); // Bottom
pinMode(10, INPUT); // BUTTON
Serial.begin(9600);
}

void loop() {
if (i=1) {
ONE();
delay(1000);
}
if (i=2) {
TWO();
delay(1000);
}
if (i=3) {
THREE();
delay(1000);
}
if (i=4) {
FOUR();
delay(1000);
}
if (i=5) {
FIVE();
delay(1000);
}
if (i=6) {
SIX();
delay(1000);
}
if (i=7) {
SEVEN();
delay(1000);
}
if (i=8) {
EIGHT();
delay(1000);
}
if (i=9) {
NINE();
delay(1000);
}
}

void ONE(){
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
}

void TWO()
{
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
}

void THREE()
{
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
}

void FOUR()
{
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
}

void FIVE()
{
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
}

void SIX()
{
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
}

void SEVEN()
{
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
}

void EIGHT()
{
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
}

void NINE()
{
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
}

I want it to count up from 1 to 9 but I can’t seem to get the counter to go up, it just throws all of my code together and ends up on 9 since I haven’t used one segment this early on.

It is doing everything you want, in the proper order. It is just doing it so fast that you can’t see that it is doing so.

You could edit the boards.text file, and tell it that your Arduino was operating at 0.016MHz, instead. Then, you might be able to see what it was doing.

Even better, though, would be to add a small delay() at the end of loop(), so it displays the number for some time, before going on to the next one.

P.S. I don’t think you want <= in your if tests. That will display 1, then 1 and 2, then 1 and 2 and 3…

Also, you need to turn all the segments off before displaying the next number.

Aren't you missing out of some digits if you merge the middle and top-left segment (pin 13)?

I'm pretty new to this aswell, but why not do it this way:

int i = 0; int wait = 1000;

void setup() { // initialize the digital pin as an output. // Pin 13 has an LED connected on most Arduino boards: pinMode(13, OUTPUT); // Top Left pinMode(12, OUTPUT); // Bottom Left pinMode(11, OUTPUT); // Bottom pinMode(10, OUTPUT); // Bottom Right pinMode(9, OUTPUT); // Top pinMode(8, OUTPUT); // Top Right pinMode(7, OUTPUT); //Middle }

void loop() { if (i=1) { digitalWrite(8, HIGH); digitalWrite(9, LOW); digitalWrite(10, HIGH); digitalWrite(11, LOW); digitalWrite(12, LOW); digitalWrite(13, LOW); } if (i=2) { digitalWrite(8, HIGH); digitalWrite(9, HIGH); digitalWrite(10, LOW); digitalWrite(11, HIGH); digitalWrite(12, LOW); digitalWrite(13, HIGH); } if (i=3) { digitalWrite(8, HIGH); digitalWrite(9, HIGH); digitalWrite(10, HIGH); digitalWrite(11, HIGH); digitalWrite(12, LOW); digitalWrite(13, LOW); } delay(1000); i++; }

if (i=1) {

This, of course, will always be true, as will if (i=2) {

"i" will keep rising so two statements can't be true at the same time.

The if statements if(i==1) and if(i==2) can not both be true at the same time. The if statements if(i=1) and if(i=2) are both true. The = is an assignment operator. i is assigned the value 1 or 2. The result of an assignment is the assigned value. Since non-zero is true, both assignments are true.

The ==, on the other hand, is the equality operator.

The if statements if(i==1) and if(i==2) can not both be true at the same time. The if statements if(i=1) and if(i=2) are both true. The = is an assignment operator. i is assigned the value 1 or 2. The result of an assignment is the assigned value. Since non-zero is true, both assignments are true.

The ==, on the other hand, is the equality operator.

Yeah that was a stupid glitch from my part, don’t know how I missed it :stuck_out_tongue:

When I read a code with a lots of if() statments, my brain is like : ?????? - comfuse...

@Plecto and Cowboyfromhel2

Did you guys look into switch() - case statement... A switch() - case is less comfusing...realy..

My 2 cents

example :

switch(i)
{
   case 1:   displayone();
                break;
   case 2:   displaytwo();
                break;
   case 3:   displaythree();
                break;
   case 4 :  displayfour();
                break;
   // the rest of the numbers to be display
}

A real cool way would be to make it a function like

7seg(1); delay(500); 7seg(2); delay(500); etc

Then at bottom

Void 7seg(int number){

If(number == 1){ // then code for turning on segement } if(number ==2){ //code }

}//end of 7seg

This approach would make you loop look simple and make it easy to integrate into something else

I'd like to thank everyone for their input, the amount of people who are willing to help on this forum is incredible and it was a great first experience.

@Plecto I used your idea and it works perfectly, I hadn't thought about setting the power for each pin for every number which is probably why I always ended up with 8 on the display.

@ Techone and winner10920, I'm still learning how to code and there are plenty of commands I don't understand just quite yet. I will be looking into both of your suggested ways so that in future projects I will have that knowledge at my disposal.

Thanks everyone!

I think I did something similar to what Techone was saying with this sketch, its using Plecto's high low idea for each number and it also defines the numbers so I don't have to type each segment out for every number I use, I can just use ONE() to represent all the digitalWrites for the number 1.

int i = 0; int wait = 1000;

void setup() { pinMode(2, OUTPUT); // Top pinMode(3, OUTPUT); // Top Right pinMode(4, OUTPUT); // Top Left pinMode(5, OUTPUT); // Middle pinMode(6, OUTPUT); // Bottom Right pinMode(7, OUTPUT); // Bottom Left pinMode(8, OUTPUT); // Bottom }

void loop() { i = i + 1; if (i=1) { ONE(); delay(1000); } if (i=2) { TWO(); delay(1000); } if (i=3) { THREE(); delay(1000); } }

void ONE(){ digitalWrite(2, LOW); digitalWrite(3, HIGH); digitalWrite(4, LOW); digitalWrite(5, LOW); digitalWrite(6, HIGH); digitalWrite(7, LOW); digitalWrite(8, LOW); }

void TWO() { digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, LOW); digitalWrite(5, HIGH); digitalWrite(6, LOW); digitalWrite(7, HIGH); digitalWrite(8, HIGH); }

void THREE() { digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, LOW); digitalWrite(5, HIGH); digitalWrite(6, HIGH); digitalWrite(7, LOW); digitalWrite(8, HIGH); }

That code is not efficient, all those if's have be evaluated to find the active one.

Smarter is this:

switch(i){

case 1:
  ONE();
    delay(1000);
break;

  case 2:
    TWO();
    delay(1000);
break;

  case 3:
    THREE();
    delay(1000);
break;
  } // end switch

Then there is the equivalent of 1 comparison and a jump directly to the active code.

Does the # following case determine the value of "i" for which it will run the bracketed code?

When i == 1, case 1 runs. break then jumps it out to the end }
Same for i ==2, i==3, i==however many you have.

can be other stuff too:

switch (character){ case '0':

case 'a':

case 'f':

You need to type "if(i==x)", not "if(i=x)" (the same mistake I did). i=x will, as AWOL said allways be true.

To make "i" rise one value at the time you could just type "i++", that equals "i+1".

To make the reset button work you could type something like this:

int reset = digitalRead(button); if (reset == HIGH) { i=0; }

I did not say i=x will always be true.
If x is zero, it will not be true.