Collecting the number of analog value breaks

I want to count the number of times 3 photocell's go below a certain value then save that number to CumulativeTotal then ouput that to a seven segment display. my code compiles but it only displays a 5 on the seven segment regardless of the analog values. any help would be great

int CumulativeTotal = 0; //initial value of CumulativeTotal = 0



void setup() {
  pinMode(A0, INPUT);
  pinMode(A1, INPUT); 
  pinMode(A2, INPUT);
  pinMode(2, OUTPUT);  
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  Serial.begin (9600);
}

  

void loop() {
  delay(5);
if  (analogRead(0) < 40){(++CumulativeTotal);}  // if statement true add 1 to 'The cumulative total' which will be displayed to the seven segment display.
delay (50);
if  (analogRead(1) < 40){(++CumulativeTotal);}  // if statement true add 1 to 'The cumulative total' which will be displayed to the seven segment display.
delay (50);
if  (analogRead(2) < 40){(++CumulativeTotal);}// if statement true add 1 to 'The cumulative total' which will be displayed to the seven segment display.
delay (50);
if (CumulativeTotal == 0);
{
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 digitalWrite(7, 1);
 digitalWrite(8, 0);// write '0'
}

if (CumulativeTotal == 1);
{
 digitalWrite(2, 0);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 digitalWrite(7, 0);
 digitalWrite(8, 0);// write '1'
}
 
   
if (CumulativeTotal == 2);
{
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 0);
 digitalWrite(5, 1);
 digitalWrite(6, 1);
 digitalWrite(7, 0);
 digitalWrite(8, 1);}// write '2'

 
   
 if (CumulativeTotal == 3);
 {
 digitalWrite(2, 1);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 0);
 digitalWrite(7, 0);
 digitalWrite(8, 1);}// write '3'
    
   if (CumulativeTotal == 4);;
  {
  digitalWrite(2, 0);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 digitalWrite(7, 1);
 digitalWrite(8, 1);}// write '4'
 
  if (CumulativeTotal == 5);
 {
 digitalWrite(2, 1);
 digitalWrite(3, 0);
 digitalWrite(4, 1);
 digitalWrite(5, 1);
 digitalWrite(6, 0);
 digitalWrite(7, 1);
 digitalWrite(8, 1);}// write '5'
if (CumulativeTotal == 0);

The semicolon forms the body of your if statement. That hardly seems like what you want the body to be.

A switch statement seems more appropriate that all those if statements, when only one condition at a time can apply.

As PaulS pointed out, the semicolons in your if statements are the problem. To elaborate, the presence of the semicolon ends the if statement and the following block of code is executed no mater what. It looks like your code is actually writing each digit, 0 - 5, to the 7-segment display. You only see the 5 because of the delay at the top of the loop.

Change your code from:

if (CumulativeTotal == 1);
{
 digitalWrite(2, 0);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 digitalWrite(7, 0);
 digitalWrite(8, 0);// write '1'
}

to

if (CumulativeTotal == 1)
{
 digitalWrite(2, 0);
 digitalWrite(3, 1);
 digitalWrite(4, 1);
 digitalWrite(5, 0);
 digitalWrite(6, 0);
 digitalWrite(7, 0);
 digitalWrite(8, 0);// write '1'
}

It would be more efficient if you used "else if" for digits 1 - 5 so that once you get a match you stop comparing CumulativeTotal to the other values.

Or use a 6 x 7 byte array and a single loop.

int CumulativeTotal = 0; //initial value of CumulativeTotal = 0

#define PIN_A 2

byte ledVals [6][7] = {      // you fill in the values
//   a b c d e f g 
    {0,0,0,0,0,0,0},        // 0
    {0,0,0,0,0,0,0},        // 1
    {0,0,0,0,0,0,0},        // 2
    {0,0,0,0,0,0,0},        // 3
    {0,0,0,0,0,0,0},        // 4
    {0,0,0,1,0,0,1}         // 5

};

void setup() {
  pinMode(A0, INPUT);
  pinMode(A1, INPUT); 
  pinMode(A2, INPUT);
  
  for (int i = 0; i < 7; i++) {
    pinMode(i + PIN_A, OUTPUT);
  }
  Serial.begin (9600);
}


void loop() {
    delay(5);
    if  (analogRead(0) < 40){(++CumulativeTotal);}  // if statement true add 1 to 'The cumulative total' which will be displayed to the seven segment display.
    delay (50);
    if  (analogRead(1) < 40){(++CumulativeTotal);}  // if statement true add 1 to 'The cumulative total' which will be displayed to the seven segment display.
    delay (50);
    if  (analogRead(2) < 40){(++CumulativeTotal);}  // if statement true add 1 to 'The cumulative total' which will be displayed to the seven segment display.
    delay (50);

    for (int i = 0; i < 7; i++) {
        pinMode(i + PIN_A, ledVals[CumulativeTotal][i]);
    }
}

BTW CumulativeTotal never gets reset, what happens when it's > 5?


Rob

I can't express how big of a help that was, code is up and working perfectly and rob I have the code to count up to 9 on the digit display now. eventualy i want to have a 6" tall 2 digit display or LED matrix counting to 99 but im not very good with shift registers (may need you all's help again for that part)

just one more question, Blue Eyes what should the else if say to stop comparing the values to CumulativeTotal?