Trouble With Common Anode 7-Segment Display

I am trying to create a project for my car that displays what gear I am in. I am simulating the final product with pushbuttons for now, but I am trying to get my code finished so that I can have a quick install.

The issue I am running in to is that I can't switch what is displayed on the 7-Segment display. Here is my code and attached is a pictorial diagram of my schematic.

const int gear1 = 0;
const int gear2 = 1;
const int gear3 = 2;
const int gear4 = 3;
const int gear5 = 4;
const int reverse = 5;

const int segment_a = 6;
const int segment_b = 7;
const int segment_c = 8;
const int segment_d = 9;
const int segment_e = 10;
const int segment_f = 11;
const int segment_g = 12;

void setup(){  
  pinMode(gear1, INPUT);
  pinMode(gear2, INPUT);
  pinMode(gear3, INPUT);
  pinMode(gear4, INPUT);
  pinMode(gear5, INPUT);
  pinMode(reverse, INPUT);

  pinMode(segment_a, OUTPUT);
  pinMode(segment_b, OUTPUT);
  pinMode(segment_c, OUTPUT);
  pinMode(segment_d, OUTPUT);
  pinMode(segment_e, OUTPUT);
  pinMode(segment_f, OUTPUT);
  pinMode(segment_g, OUTPUT);

  digitalWrite(segment_a, HIGH);
  digitalWrite(segment_b, HIGH);
  digitalWrite(segment_c, HIGH);
  digitalWrite(segment_d, HIGH);
  digitalWrite(segment_e, HIGH);
  digitalWrite(segment_f, HIGH);
  digitalWrite(segment_g, HIGH);
}

void loop(){
  //Displays n for neutral when no other gear is selected
  while(digitalRead(gear1 == LOW) && digitalRead(gear2 == LOW) && digitalRead(gear3 == LOW) && digitalRead(gear4 == LOW) && digitalRead(gear5 == LOW) && digitalRead(reverse == LOW)){
    digitalWrite(segment_a, HIGH);
    digitalWrite(segment_b, HIGH);
    digitalWrite(segment_c, LOW);
    digitalWrite(segment_d, HIGH);
    digitalWrite(segment_e, LOW);
    digitalWrite(segment_f, HIGH);
    digitalWrite(segment_g, LOW);
  }
  //Displays 1 when the lever is in 1st gear
  while(digitalRead(gear1 == HIGH)){
    digitalWrite(segment_a, HIGH);
    digitalWrite(segment_b, LOW);
    digitalWrite(segment_c, LOW);
    digitalWrite(segment_d, HIGH);
    digitalWrite(segment_e, HIGH);
    digitalWrite(segment_f, HIGH);
    digitalWrite(segment_g, HIGH);
  }
  //Displays 2 when the lever is in 2nd gear
  while(digitalRead(gear2 == HIGH)){
    digitalWrite(segment_a, LOW);
    digitalWrite(segment_b, LOW);
    digitalWrite(segment_c, HIGH);
    digitalWrite(segment_d, LOW);
    digitalWrite(segment_e, LOW);
    digitalWrite(segment_f, HIGH);
    digitalWrite(segment_g, LOW);
  }
  //Displays 3 when the lever is in 3rd gear
  while(digitalRead(gear3 == HIGH)){
    digitalWrite(segment_a, LOW);
    digitalWrite(segment_b, LOW);
    digitalWrite(segment_c, LOW);
    digitalWrite(segment_d, LOW);
    digitalWrite(segment_e, HIGH);
    digitalWrite(segment_f, HIGH);
    digitalWrite(segment_g, LOW);
  }
  //Displays 4 when the lever is in 4th gear
  while(digitalRead(gear4 == HIGH)){
    digitalWrite(segment_a, HIGH);
    digitalWrite(segment_b, LOW);
    digitalWrite(segment_c, LOW);
    digitalWrite(segment_d, HIGH);
    digitalWrite(segment_e, HIGH);
    digitalWrite(segment_f, LOW);
    digitalWrite(segment_g, LOW);
  }
  //Displays 5 when the lever is in 5th gear
  while(digitalRead(gear5 == HIGH)){
    digitalWrite(segment_a, LOW);
    digitalWrite(segment_b, HIGH);
    digitalWrite(segment_c, LOW);
    digitalWrite(segment_d, LOW);
    digitalWrite(segment_e, HIGH);
    digitalWrite(segment_f, LOW);
    digitalWrite(segment_g, LOW);
  }
  //Displays r for reverse when the lever is in reverse
  while(digitalRead(reverse == HIGH)){
    digitalWrite(segment_a, HIGH);
    digitalWrite(segment_b, HIGH);
    digitalWrite(segment_c, HIGH);
    digitalWrite(segment_d, HIGH);
    digitalWrite(segment_e, LOW);
    digitalWrite(segment_f, HIGH);
    digitalWrite(segment_g, LOW);
  }
}

I am not 100% sure if this is a hardware or coding issue, maybe both. From my understanding, the :while: command should only execute the code in its brackets if the arguments are true. This works when the car is in "neutral." n is displayed, but when I press a button to simulate moving to a different gear, nothing changes.

Your button switches have no pulldown resistors so the input pins to which they are connect are "floating" (of indeterminate state) when the switches are open (not pressed). The best fix is to connect one side of the switch to ground and the other to an input set to pinMode INPUT_PULLUP. The switches will read HIGH when not pressed and LOW when pressed. Adjust the logic in your code accordingly.

Don't use pins 0 and 1 for switches. Those pins are for the hardware serial. Save those pins for program upload and monitoring program flow and variable values. The serial port is your best debugging tool. Use it. The analog inputs are actually digital inputs with analog input as a special function. Use a couple of the analog inputs for switches instead of pins 0 and 1. When using analog pins as digital you can refer to them with the A notation (A0, A1, ... ) or by their numbers A0 = 14, A1 = 15, A2 = 16, ...

If the display is showing "n" that must mean all 5 button pins are reading LOW. Groundfungus is correct, your button pins are floating, assuming you have wired them as shown, so that must be happening by chance, but it is happening. But if you press any of the buttons, one of the pins should read HIGH, changing what is shown on the display. So I suspect your diagram is not in fact how your buttons are wired.

digitalRead(gear1 == LOW)

That will read Pin 1 because "gear1 == LOW" is true (gear1 == 0 and LOW == 0).

 digitalRead(gear2 == LOW)

This (and all the other pins) will read Pin 0 because "gear2 == LOW" is false (gear2 == 1 and LOW == 0).

What you want to do is:

digitalRead(gear1) == LOW

Note how the close-paren has moved. This will read Pin 0 and will be true if Pin 0 reads LOW.

Oh, good spot, @johnwasser, can't believe I didn't see that!

can't believe I didn't see that!

Me 3.

Thank you for the in-depth explanation, I think we'll be running in no time!

Thanks for catching that, definitely need to make some changes!

Thank you everyone for your help! I made the edits you all suggested and now the project works as intended! @groundFungus @johnwasser @PaulRB

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