Arduino Program to Display Flow Rate in a 6 Digit Seven Segment Display (Part 2 of 3)

<<<< Read Previous Part (Part 1)

In the previous part, we saw the circuit to display the rate of flow of water through a flow meter in a six digit common cathode seven segment displays using an arduino mega. Here, we will see the program to be uploaded to the arduino board for the expected output (tested on arduino version 1.6.3). If uploading is successful, allow water to flow through the water flow sensor at different speeds. Rate of flow will be displayed in the common cathode seven segment displays.

int latchPin = 11;  //Pin connected to ST_CP of both 74595 ICs
int clockPin = 10;  //Pin connected to SH_CP of both 74595 ICs
int dataPin = 12;   //Pin connected to DS of 1st 74595

int display_array[6]={' ', ' ', ' ', ' ', ' ', ' '};


volatile float  flow_frequency;  // Measures flow meter pulses
double  l_hour;          // Calculated litres/hour                      
unsigned char flowmeter = 2;  // Flow Meter Pin number
unsigned long currentTime;
unsigned long cloopTime;

void flow ()                  // Interruot function
{ 
   flow_frequency++;
}

void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
    
  pinMode(flowmeter, INPUT);
  Serial.begin(9600); 
  attachInterrupt(0, flow, RISING); // Setup Interrupt 
                                    // see http://arduino.cc/en/Reference/attachInterrupt
  sei();                            // Enable interrupts  
  currentTime = millis();
  cloopTime = currentTime;
  
  Serial.begin(9600);
}

void turn_off_all_displays()
{
  
    // take the latchPin low so the LEDs don't change while you're sending in bits:     
    digitalWrite(latchPin, LOW);
    //Send 0 0 0 0 0 0 0 0 (0) to Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 2nd 74595
    shiftOut(dataPin, clockPin, MSBFIRST, 0);  
    //Send 0 0 0 0 0 0 0 0 (0) to Q7 Q6 Q5 Q4 Q3 Q2 Q1 Q0 of 1st 74595
    shiftOut(dataPin, clockPin, MSBFIRST, 0);
    // shift out the bits:    
    digitalWrite(latchPin, HIGH);
    
}

void seven_segment_display(int number, int index)
{
    
  int display_cathode_number, dot_value = 0;
  
  if(index==0)
  {
    display_cathode_number = 254;
  }

  else if(index==1)
  {
    display_cathode_number = 253;
  }

  else if(index==2)
  {
    display_cathode_number = 251;
  }
  
  else if(index==3)
  {
    display_cathode_number = 247;
    dot_value = 128;
  }

  else if(index==4)
  {
    display_cathode_number = 239;
  }
  
  else if(index==5)
  {
    display_cathode_number = 223;
  }


  if(number == 45)
  {
    // take the latchPin low so the LEDs don't change while you're sending in bits:     
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 64  + dot_value);  
    shiftOut(dataPin, clockPin, MSBFIRST, display_cathode_number);
    // shift out the bits:    
    digitalWrite(latchPin, HIGH);
       
    delay(1);
  }  
  

  if(number == 32)
  {
    // take the latchPin low so the LEDs don't change while you're sending in bits:     
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 0  + dot_value);
    shiftOut(dataPin, clockPin, MSBFIRST, display_cathode_number);
    // shift out the bits:    
    digitalWrite(latchPin, HIGH);
       
    delay(1);
  }
  
  else if(number == 0)
  {
    // take the latchPin low so the LEDs don't change while you're sending in bits:     
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 63  + dot_value);
    shiftOut(dataPin, clockPin, MSBFIRST, display_cathode_number);
    // shift out the bits:    
    digitalWrite(latchPin, HIGH);       
       
    delay(1);
  }
  
  else if(number == 1)
  {
    // take the latchPin low so the LEDs don't change while you're sending in bits:     
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 6 + dot_value);
    shiftOut(dataPin, clockPin, MSBFIRST, display_cathode_number);
    // shift out the bits:    
    digitalWrite(latchPin, HIGH);       
       
    delay(1);
  }
  
  else if(number == 2)
  {
    // take the latchPin low so the LEDs don't change while you're sending in bits:     
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 91 + dot_value);
    shiftOut(dataPin, clockPin, MSBFIRST, display_cathode_number);
    // shift out the bits:    
    digitalWrite(latchPin, HIGH);       
       
    delay(1);
  }
  
  
  else if(number == 3)
  {
    // take the latchPin low so the LEDs don't change while you're sending in bits:     
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 79 + dot_value);
    shiftOut(dataPin, clockPin, MSBFIRST, display_cathode_number);
    // shift out the bits:    
    digitalWrite(latchPin, HIGH);       
       
    delay(1);
  }
  
  
  else if(number == 4)
  {
    // take the latchPin low so the LEDs don't change while you're sending in bits:     
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 102 + dot_value);
    shiftOut(dataPin, clockPin, MSBFIRST, display_cathode_number);
    // shift out the bits:    
    digitalWrite(latchPin, HIGH);       
       
    delay(1);
  }
  
  
  else if(number == 5)
  {
    // take the latchPin low so the LEDs don't change while you're sending in bits:     
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 109 + dot_value);
    shiftOut(dataPin, clockPin, MSBFIRST, display_cathode_number);
    // shift out the bits:    
    digitalWrite(latchPin, HIGH);       
       
    delay(1);
  }
  
  
  else if(number == 6)
  {
    // take the latchPin low so the LEDs don't change while you're sending in bits:     
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 125 + dot_value);  
    shiftOut(dataPin, clockPin, MSBFIRST, display_cathode_number);
    // shift out the bits:    
    digitalWrite(latchPin, HIGH);       
       
    delay(1);
  }
  
  else if(number == 7)
  {
    // take the latchPin low so the LEDs don't change while you're sending in bits:     
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 7 + dot_value);  
    shiftOut(dataPin, clockPin, MSBFIRST, display_cathode_number);
    // shift out the bits:    
    digitalWrite(latchPin, HIGH);       
       
    delay(1);
  }
  
  
  else if(number == 8)
  {
    // take the latchPin low so the LEDs don't change while you're sending in bits:     
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 127 + dot_value);  
    shiftOut(dataPin, clockPin, MSBFIRST, display_cathode_number);
    // shift out the bits:    
    digitalWrite(latchPin, HIGH);       
       
    delay(1);
  }
  
  else if(number == 9)
  {
    // take the latchPin low so the LEDs don't change while you're sending in bits:     
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 111 + dot_value);  
    shiftOut(dataPin, clockPin, MSBFIRST, display_cathode_number);
    // shift out the bits:    
    digitalWrite(latchPin, HIGH);       
       
    delay(1);
  }  
}

void decimal_conversion(double num)
{
    int remainder = 0;
    int modulus = 0;
    int count = 3, i=0, j=0;
    int intpart = (int)num;
    double decpart = num - intpart;
    
    double decpart_mul_1000 = decpart * 1000;
    int int_part_decpart_mul_1000 = int(decpart_mul_1000);
    int third_digit = int_part_decpart_mul_1000 %10;
    int first_two_digits = int_part_decpart_mul_1000 / 10;
    
    if(third_digit >= 5)
    {
      first_two_digits++;
    }
//    Serial.print(intpart);
//    Serial.print(" ");
//    Serial.print(decpart);
    
    while(intpart!=0)
    {
      remainder = intpart / 10; 
      modulus = intpart % 10;
      intpart = remainder;
      display_array[count]= modulus;
      count--;
    }
    
    count = 5;
    
    while(first_two_digits!=0)
    {
      remainder = first_two_digits / 10; 
      modulus = first_two_digits % 10;
      first_two_digits = remainder;
      display_array[count]= modulus;
      count--;
    }    
 
    for(i=0;i<6;i++)
    {
      for(j=0;j<2;j++)
      {
        seven_segment_display(display_array[i], i);
      }
      turn_off_all_displays();
     Serial.println(display_array[i]);
    }

}

void loop() {
    
   int i=0;
  
   currentTime = millis();
   // Every second, calculate and print litres/hour
   if(currentTime >= (cloopTime + 1000))
   {     
      cloopTime = currentTime;              // Updates cloopTime
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min. (Results in +/- 3% range)
      l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flow rate in L/hour 
      flow_frequency = 0;                   // Reset Counter
           
      Serial.print(l_hour);            // Print litres/hour
//      Serial.println(" L/hour");
   }
   
    decimal_conversion(l_hour);

    for(i=0;i<6;i++)
    {
       display_array[i]=0;
    } 
}

Continued in Next Part (Part 3) >>>>

(Visited 1,341 times, 1 visits today)
paulpp644
A deep electronic enthusiast who spent most of my time for electronic experiments. I am also interested in publishing my experiments in my blog for helping other electronics enthusiasts.