Some sort of counting issue when trying to count from 0-9 repetitively

Hey all.

This is my first post on the forum. I have been using Arduino for a few months and am at a fairly basic knowledge level. I am using the official starter kit for my Arduino, breadboard, and buttons. I am also using a 5161AH common cathode 7 segment display. Datasheet for 5161AH.

What I am trying to do is have the display show a number from 0-9, starting with 0. Once it reaches 9 and tries to add, it gets sent back to 0 and the same goes for subtracting from 0. I have 2 buttons, one increases the value, the other decreases it.

My issue is that at the end of each loop, the value automatically gets set to 9 no matter what input it gets.

/*
  This code is meant to display a number from 1 to 9.
  To change numbers, I have 2 pushbuttons. One increases the value, the other decreases it.
  The numbers will be displayed on a single digit 7 segment display 5161AH
*/
const int aPin = 2; //These are for the segments on the 7 segment display
const int bPin = 3;
const int cPin = 4;
const int dPin = 5;
const int ePin = 6;
const int fPin = 7;
const int gPin = 8;

const int upBtn = 12; //Sets pin for the up button
const int downBtn = 13; // Sets pin for the down button

int count = 0; //Count goes from 0 to 9. This is done later in the code
int upReading = 0; //the value read from upBtn
int downReading = 0; //the value read from downBtn

void setup() {
  Serial.begin(9600);

  pinMode(aPin, OUTPUT); //Set all the pins for the 7 segment display
  pinMode(bPin, OUTPUT);
  pinMode(cPin, OUTPUT);
  pinMode(dPin, OUTPUT);
  pinMode(ePin, OUTPUT);
  pinMode(fPin, OUTPUT);
  pinMode(gPin, OUTPUT);

  pinMode(upBtn, INPUT); //inputs for buttons
  pinMode(downBtn, INPUT);

}

void loop() {

  Serial.print("Initial Count: "); //This segment prints the starting value
  delay(500);
  Serial.print(count);
  Serial.print("\n");
  delay(1000);

  upReading = digitalRead(upBtn); //assign a value to upReading

  Serial.print("The up button reading is: "); //This segment prints the value that upReading has been assigned
  delay(500);
  Serial.print(upReading);
  Serial.print("\n");
  delay(1000);

  if (upReading == HIGH && count != 9) { //just adds one if upBtn is pressed
    count == count++;
    delay(1);

    Serial.print("Count has increased. Current count is: "); //This segment prints the count value once it has increased
    delay(500);
    Serial.print(count);
    Serial.print("\n");
    delay(1000);

  } else if (upReading == HIGH && count == 9) { //To make count 0
    count = 0;
    delay(1);

    Serial.print("The counter has reached max value.\nIt has been set to: "); //This segment tells you that the count has been set back to 0
    delay(500);
    Serial.print(count);
    Serial.print("\n");
    delay(1000);

  } else if (upReading == LOW) {

    Serial.print("Counter did not go up\nCount is still: ");
    delay(500);
    Serial.print(count);
    Serial.print("\n");
    delay(1000);
  }

  downReading = digitalRead(downBtn);

  Serial.print("The down button reading is: ");
  delay(500);
  Serial.print(downReading);
  Serial.print("\n");
  delay(1000);

  if (downReading == HIGH && count != 0) { //decreases count when downBtn is pressed
    count == count--;
    delay(1);

    Serial.print("Count has decreased.\nCurrent count is: "); //This segment shows the count value once it decreases
    delay(500);
    Serial.print(count);
    Serial.print("\n");
    delay(1000);

  } else if (downReading == HIGH && count == 0) { //once it gets to 0 it gets set to 9 when the downBtn is pressed
    count = 9;
    delay(1);

    Serial.print("Counter has reached minimum value.\nCount has been set to: ");
    delay(500);
    Serial.print(count);
    Serial.print("\n");
    delay(1000);

  } else if (downReading == LOW) {
    Serial.print("Counter did not go down\nCount is still: ");
    delay(500);
    Serial.print(count);
    Serial.print("\n");
    delay(1000);
  }

  //From here on displays the numbers on the 7 segment display

  if (count = 0) { //prints 0
    digitalWrite(aPin, LOW);
    digitalWrite(bPin, LOW);
    digitalWrite(cPin, LOW);
    digitalWrite(dPin, LOW);
    digitalWrite(ePin, LOW);
    digitalWrite(fPin, LOW);
    digitalWrite(gPin, LOW);
    delay(1);
    digitalWrite(aPin, HIGH);
    digitalWrite(bPin, HIGH);
    digitalWrite(cPin, HIGH);
    digitalWrite(dPin, HIGH);
    digitalWrite(ePin, HIGH);
    digitalWrite(fPin, HIGH);
    delay(1);
  }

  if (count = 1) { //prints 1
    digitalWrite(aPin, LOW);
    digitalWrite(bPin, LOW);
    digitalWrite(cPin, LOW);
    digitalWrite(dPin, LOW);
    digitalWrite(ePin, LOW);
    digitalWrite(fPin, LOW);
    digitalWrite(gPin, LOW);
    delay(1);
    digitalWrite(bPin, HIGH);
    digitalWrite(cPin, HIGH);
    delay(1);
  }

  if (count = 2) { //prints 2
    digitalWrite(aPin, LOW);
    digitalWrite(bPin, LOW);
    digitalWrite(cPin, LOW);
    digitalWrite(dPin, LOW);
    digitalWrite(ePin, LOW);
    digitalWrite(fPin, LOW);
    digitalWrite(gPin, LOW);
    delay(1);
    digitalWrite(aPin, HIGH);
    digitalWrite(bPin, HIGH);
    digitalWrite(dPin, HIGH);
    digitalWrite(ePin, HIGH);
    digitalWrite(gPin, HIGH);
    delay(1);
  }

  if (count = 3) { //prints 3
    digitalWrite(aPin, LOW);
    digitalWrite(bPin, LOW);
    digitalWrite(cPin, LOW);
    digitalWrite(dPin, LOW);
    digitalWrite(ePin, LOW);
    digitalWrite(fPin, LOW);
    digitalWrite(gPin, LOW);
    delay(1);
    digitalWrite(aPin, HIGH);
    digitalWrite(bPin, HIGH);
    digitalWrite(cPin, HIGH);
    digitalWrite(dPin, HIGH);
    digitalWrite(gPin, HIGH);
    delay(1);
  }

  if (count = 4) { //prints 4
    digitalWrite(aPin, LOW);
    digitalWrite(bPin, LOW);
    digitalWrite(cPin, LOW);
    digitalWrite(dPin, LOW);
    digitalWrite(ePin, LOW);
    digitalWrite(fPin, LOW);
    digitalWrite(gPin, LOW);
    delay(1);
    digitalWrite(bPin, HIGH);
    digitalWrite(cPin, HIGH);
    digitalWrite(fPin, HIGH);
    digitalWrite(gPin, HIGH);
    delay(1);
  }

  if (count = 5) { //prints 5
    digitalWrite(aPin, LOW);
    digitalWrite(bPin, LOW);
    digitalWrite(cPin, LOW);
    digitalWrite(dPin, LOW);
    digitalWrite(ePin, LOW);
    digitalWrite(fPin, LOW);
    digitalWrite(gPin, LOW);
    delay(1);
    digitalWrite(aPin, HIGH);
    digitalWrite(cPin, HIGH);
    digitalWrite(dPin, HIGH);
    digitalWrite(fPin, HIGH);
    digitalWrite(gPin, HIGH);
    delay(1);
  }

  if (count = 6) { //prints 6
    digitalWrite(aPin, LOW);
    digitalWrite(bPin, LOW);
    digitalWrite(cPin, LOW);
    digitalWrite(dPin, LOW);
    digitalWrite(ePin, LOW);
    digitalWrite(fPin, LOW);
    digitalWrite(gPin, LOW);
    delay(1);
    digitalWrite(aPin, HIGH);
    digitalWrite(cPin, HIGH);
    digitalWrite(dPin, HIGH);
    digitalWrite(ePin, HIGH);
    digitalWrite(fPin, HIGH);
    digitalWrite(gPin, HIGH);
    delay(1);
  }

  if (count = 7) { //prints 7
    digitalWrite(aPin, LOW);
    digitalWrite(bPin, LOW);
    digitalWrite(cPin, LOW);
    digitalWrite(dPin, LOW);
    digitalWrite(ePin, LOW);
    digitalWrite(fPin, LOW);
    digitalWrite(gPin, LOW);
    delay(1);
    digitalWrite(aPin, HIGH);
    digitalWrite(bPin, HIGH);
    digitalWrite(cPin, HIGH);
    delay(1);
  }

  if (count = 8) { //prints 8
    digitalWrite(aPin, LOW);
    digitalWrite(bPin, LOW);
    digitalWrite(cPin, LOW);
    digitalWrite(dPin, LOW);
    digitalWrite(ePin, LOW);
    digitalWrite(fPin, LOW);
    digitalWrite(gPin, LOW);
    delay(1);
    digitalWrite(aPin, HIGH);
    digitalWrite(bPin, HIGH);
    digitalWrite(cPin, HIGH);
    digitalWrite(dPin, HIGH);
    digitalWrite(ePin, HIGH);
    digitalWrite(fPin, HIGH);
    digitalWrite(gPin, HIGH);
    delay(1);
  }

  if (count = 9) { //prints 9
    digitalWrite(aPin, LOW);
    digitalWrite(bPin, LOW);
    digitalWrite(cPin, LOW);
    digitalWrite(dPin, LOW);
    digitalWrite(ePin, LOW);
    digitalWrite(fPin, LOW);
    digitalWrite(gPin, LOW);
    delay(1);
    digitalWrite(aPin, HIGH);
    digitalWrite(bPin, HIGH);
    digitalWrite(cPin, HIGH);
    digitalWrite(fPin, HIGH);
    digitalWrite(gPin, HIGH);
    delay(1);
  }
}

I have added all of the code and a screenshot from TinkerCAD for reference, I am doing this on a real Arduino though. Again, the issue that I am getting is after every cycle of the void loop, the count gets set to 9.

Also, if anyone has any suggestions as to how I could make my code shorter or more "professional" I am open to advice!

If I posted this to the wrong board, please let me know which board I should post it to.

if (count = 9) { //prints 9

Oops - a single = assignes the right value to the left variable, you need to use ==, this means equal to.

if (count == 9) { //prints 9

Sort the others also.

You've got some great serial debug statements there. Please share the serial output with us.

@missdrew

Thank you for clarifying the = signs for me. I made the change that you suggested and everything works the way I want it to.

@aarg

Thank you for trying to help :slight_smile: missdrew's message solved the issue.

If you change the compilation setting for warnings to "all", the compiler would report it when you do something like that.

Thank you for the advice! It is much appreciated.

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