Blinking an LED a set number of times with respect to analog input......

Please help......

My code works great with my LCD and displays a voltage from 0.00-1.70 with respect to analog input.
I would however like to flash the led the number of times corresponding to the voltage value(the "PPO" Reading) read on analog input........for ex: at .40 volts flash led 4 times, at .50 volts flash led 5 times, at .60 volts flash led 6 times.......... I wrote the following code(highlighted in yellow) and it does flash, but I can not get the flashes to match the analog voltage input. Any help in modifying the code would be great!!!

Thank you!!

     // include the library code:
     #include <LiquidCrystal.h>
     #include <PID_v1.h>
     #define aref_voltage 1.72
     #define PPO inputReading1*aref_voltage/1024
     
     
    
      //Define Variables we'll be connecting to
      double Setpoint, Input, Output;
      //Specify the links and initial tuning parameters
      PID myPID(&Input, &Output, &Setpoint, 5,7,2, REVERSE);
      
      
       // initialize the lcd with the numbers of the interface pins
      LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
      


/****************************************************************
*initiate pins for each led
*****************************************************************/
     const int red1      =22;
     const int blue1     =0; 
     const int green1    =0;
     const int yellow1   =0;
/****************************************************************/      
       int nr;
       int wait;
      
/****************************************************************
*initiate pins for sensor inputs 1, 2, 3
*****************************************************************/             
     const int sensor1 = A0;  // the analog pin the 02 Sensor Vout pin is connected to
       int inputReading1; //declare a variable 
      
                         
     const int sensor2 = A1;  // the analog pin the 02 Sensor Vout pin is connected to  
       int inputReading2; // declare a variable   
               
      
     const int sensor3 = A2;  // the analog pin the 02 sensor Vout pin is connected to
       int inputReading3; // declare a variable 
               
      
/*****************************************************************/     
 
 
    void setup () 
    
    
   {

      Input = analogRead(PPO);
      Setpoint = 520;
      myPID.SetMode(AUTOMATIC);
  
       analogReference(EXTERNAL); // set external analog Ref for PPO output
         

      
 /*****************************************************************
*set output assignments for led pins 
******************************************************************/     
      pinMode( red1, OUTPUT);
      pinMode( green1, OUTPUT);
      pinMode( blue1, OUTPUT);
      
/****************************************************************/      
     
     
        
/***************************************************************
*set up the LCD's number of columns and rows:
****************************************************************/ 
    lcd.begin(20, 4);
  
    lcd.setCursor(0, 0);//where to start cursor
    lcd.print("PINA SYSYTEMS       ");//print message
    lcd.print("SOFTWARE VERSION 1.0");//print message
    delay(2000);
    lcd.clear();  
      
    lcd.setCursor(0, 0); // set the cursor to column 0, line 0
    lcd.print("PP1");  // Print a message to the LCD.
  
    lcd.setCursor(6, 0); // set the cursor to column 6, line 0
    lcd.print("PP2"); // Print a message to the LCD
  
    lcd.setCursor(12, 0); // set the cursor to column 12, line 0
    lcd.print("PP3");  // Print a message to the LCD.
    
  }
/*************************************************************/
[color=yellow]

         void blinkRed(int nr, int wait) // number of blinks follwed by a delay 
     
     {
        
        {
           digitalWrite(red1,LOW); 
           delay(250); 
           digitalWrite(red1, HIGH);
           delay(250); 
        }
        
     }


        void loop () 
    
    {  
     
     
     
    if (PPO<.30) blinkRed(3, 0);  // flash three red
    if (PPO<.40) blinkRed(4, 500);  // flash four red
    if (PPO<.50) blinkRed(5, 1000); // flash five red
    if (PPO<.60) blinkRed(6, 1500); // flash six red
    if (PPO<.70) blinkRed(7, 2000);  // flash seven red
    if (PPO<.80) blinkRed(8, 2500);  // flash eight red
    if (PPO<.90) blinkRed(9, 3000) ; // flash nine red 
    if (PPO<1.00) blinkRed(10, 3500) ; // flash ten red  
    
[/color]
/*************************************************************
* turn the output pin on/off based on pid output
**************************************************************/
      Input = analogRead(PPO);
      myPID.Compute();
      analogWrite(7,Output);
      
/*************************************************************/
  
   
    
    lcd.setCursor(0, 1); //sets the cursor to column 0, line 1
    //getting the voltage reading from the 02 sensor
    inputReading1 = analogRead(sensor1);
    // converting that reading to voltage
    float voltage1 = inputReading1 * aref_voltage; 
    voltage1 /= 1024.0;
    //print actual voltage to lcd
    lcd.print(voltage1, 2);    
    delay(200);
    

    lcd.setCursor(6, 1); //sets the cursor to column 6, line 1 
    //getting the voltage reading from the temperature sensor
    inputReading2 = analogRead(sensor2);
    // converting that reading to voltage
    float voltage2 = inputReading2 * aref_voltage;
    voltage2 /= 1024.0;
    //print actual voltage to lcd
    lcd.print(voltage2, 2);
    delay(200);     
  
  
    lcd.setCursor(12, 1); //sets the cursor to column 12, line 2 
    //getting the voltage reading from the temperature sensor
    inputReading3 = analogRead(sensor3);
    // converting that reading to voltage  
    float voltage3 = inputReading3 * aref_voltage;
    voltage3 /= 1024.0;
    //print actual voltage to lcd
    lcd.print(voltage3, 2);
    delay(200);
 
  }
void blinkRed(int nr) // number of blinks 
{
  for (int i=0; i< nr; i++)
  {
    digitalWrite(red1, LOW); 
    delay(250); 
    digitalWrite(red1, HIGH);
    delay(250); 
  }

}


void loop () 
{  
  blinkRed((int)(PPO*10);  // flash  red

WOW....your a genius!!!! thank you so much!!!!

If was to do another color i can just repeat the same code correct?

I only have on problem, when I use the "delay" function with "digitalWrite" it delays the response time of the LCD output, is there a way to make your genius solution work without using the "delay" function.....?

Thank you Again!!!!

John,

Thank you so much!!!!!
I messed around with it for quite a while and was able to get three colors on the RGB working.

Its a beautiful thing....!!!!!

johnwasser:

void blinkRed(int nr) // number of blinks 

{
  for (int i=0; i< nr; i++)
  {
    digitalWrite(red1, LOW);
    delay(250);
    digitalWrite(red1, HIGH);
    delay(250);
  }

}

void loop ()

  blinkRed((int)(PPO*10);  // flash  red
}

Hi john,
Thank you so much for your input it was great!!
I only have one question, If I was to add another color, could I just repeat the code? I want the two RGB leds to flash together.
I tried adding another led, but they dont flash together, one must finish flashing before the second one cycles on. Is there a way to allow them to flash in sync?

thank you!!

If you are using delay() for the blink timing the only way to have two LED's (on separate pins) blink in unison is to set both pins in the same blink cycle:[quote

void blinkRed(int nr) // number of blinks 
{
  for (int i=0; i< nr; i++)
  {
    digitalWrite(red1, LOW); 
    digitalWrite(red2, LOW); 
     delay(250); 
    digitalWrite(red1, HIGH);
    digitalWrite(red2, HIGH); 
    delay(250); 
  }

}

johnwasser:
If you are using delay() for the blink timing the only way to have two LED's (on separate pins) blink in unison is to set both pins in the same blink cycle:[quote

void blinkRed(int nr) // number of blinks 

{
  for (int i=0; i< nr; i++)
  {
    digitalWrite(red1, LOW);
    digitalWrite(red2, LOW);
     delay(250);
    digitalWrite(red1, HIGH);
    digitalWrite(red2, HIGH);
    delay(250);
  }

}

This does work, theres only one prob. I have three independent analog outputs driving three independent leds, so each led needs to blink with respect to the current analog value associated with its output.

For example: sensor1 will cause led1 to flash 5 times with output1=.50, at the same time sensor2 may have an output = to .70 in which case led2 will flash 7 times, while sensor3 will cause led3 to flash 7 times with output=.70 also.......

The only problem is all three leds need to flash in sync with each other through one complete cycle so if one sensor should fail or its output not equal to the other(as in the example above) this will cause one of the leds to blink short, which will indicate an issue with the associated sensor connect to that output. I have included a copy of my current code. I hope this makes sense.

Thank you

Dave

  // include the library code:
     #include <LiquidCrystal.h>
     
     #define aref_voltage1 1.72
     
     #define PPO1 inputReading1*aref_voltage/1024
     #define PPO2 inputReading2*aref_voltage/1024
     #define PPO3 inputReading3*aref_voltage/1024
            
      LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the lcd with the numbers of the interface pins
      

      const int red1      =22;
      const int blue1     =28; 
      const int green1    =26;
            int nr1; // declare numbers of flashes

      const int red2      =30;
      const int blue2     =36; 
      const int green2    =34;
            int nr2;

      const int red3      =38;
      const int blue3     =44; 
      const int green3    =42;
            int nr3;



            
      const int sensor1 = A0;  // the analog pin the 02 Sensor Vout pin is connected to
            int inputReading1; //declare a variable 
      
                         
      const int sensor2 = A1;  // the analog pin the 02 Sensor Vout pin is connected to  
            int inputReading2; // declare a variable   
               
      
      const int sensor3 = A2;  // the analog pin the 02 sensor Vout pin is connected to
            int inputReading3; // declare a variable      
    
 
    void setup () 
        
    {
  
       analogReference(EXTERNAL); // set external analog Ref for PPO output
           
  
       pinMode( red1, OUTPUT);
       pinMode( green1, OUTPUT);
       pinMode( blue1, OUTPUT);     

    
       pinMode( red2, OUTPUT);
       pinMode( green2, OUTPUT);
       pinMode( blue2, OUTPUT);     

  
       pinMode( red3, OUTPUT);
       pinMode( green3, OUTPUT);
       pinMode( blue3, OUTPUT);     
     
            

      lcd.begin(20, 4);
  
      lcd.setCursor(0, 0);//where to start cursor
      lcd.print("PINA SYSYTEMS       ");//print message
      lcd.print("SOFTWARE VERSION 1.0");//print message
      delay(2000);
      lcd.clear();  
      
      lcd.setCursor(0, 0); // set the cursor to column 0, line 0
      lcd.print("PPO1");  // Print a message to the LCD.
  
      lcd.setCursor(6, 0); // set the cursor to column 6, line 0
      lcd.print("PPO2"); // Print a message to the LCD
  
      lcd.setCursor(12, 0); // set the cursor to column 12, line 0
      lcd.print("PPO3");  // Print a message to the LCD.
    }



 
      void blinkGreen1(int nr1) // number of blinks follwed by a delay 
       {
         if( PPO1 <= .50)
            for(int i=0; i< nr1; i++)
          
          {
            digitalWrite(green1,LOW); 
            delay(250); 
            digitalWrite(green1, HIGH);
            delay(250); 
          }
            else digitalWrite(green1, HIGH);        
       }
        
      void blinkBlue1(int nr1) // number of blinks follwed by a delay 
       {
         if( PPO1 > .50 && PPO1 < 1.00) 
            for(int i=0; i< nr1; i++)
         
          {
            digitalWrite(blue1,LOW); 
            delay(250); 
            digitalWrite(blue1, HIGH);
            delay(250); 
          }
            else digitalWrite(blue1, HIGH);        
       }
        
      void blinkRed1(int nr1) // number of blinks follwed by a delay 
       {
         if(PPO1 >1.00)
            for(int i=0; i< nr1; i++)
            
          {
            digitalWrite(red1,LOW); 
             delay(250); 
            digitalWrite(red1, HIGH);
            delay(250); 
          }
            else digitalWrite(red1, HIGH);           
       }
      

 
      void blinkGreen2(int nr2) // number of blinks follwed by a delay 
       {
         if( PPO2 <= .50) 
            for(int i=0; i< nr2; i++)
         
          {
            digitalWrite(green2,LOW); 
            delay(250); 
            digitalWrite(green2, HIGH);
            delay(250); 
          }
            else digitalWrite(green2, HIGH);        
       }
        
      void blinkBlue2(int nr2) // number of blinks follwed by a delay 
       {
         if( PPO2 > .50 && PPO2 < 1.00)
            for(int i=0; i< nr2; i++)
         
          {
            digitalWrite(blue2,LOW); 
            delay(250); 
            digitalWrite(blue2, HIGH);
            delay(250); 
          }
            else digitalWrite(blue2, HIGH);        
       }
        
      void blinkRed2(int nr2) // number of blinks follwed by a delay 
       {
         if(PPO2 >1.00)
            for(int i=0; i< nr2; i++)
            
          {
            digitalWrite(red2,LOW); 
             delay(250); 
            digitalWrite(red2, HIGH);
            delay(250); 
          }
            else digitalWrite(red2, HIGH);           
       }


 
      void blinkGreen3(int nr3) // number of blinks follwed by a delay 
       {
         if( PPO3 <= .50)
            for(int i=0; i< nr3; i++)
          
          {
            digitalWrite(green3,LOW); 
            delay(250); 
            digitalWrite(green3, HIGH);
            delay(250); 
          }
            else digitalWrite(green3, HIGH);        
       }
        
      void blinkBlue3(int nr3) // number of blinks follwed by a delay 
       {
         if( PPO3 > .50 && PPO3 < 1.00)
            for(int i=0; i< nr3; i++)
         
          {
            digitalWrite(blue3,LOW); 
            delay(250); 
            digitalWrite(blue3, HIGH);
            delay(250); 
          }
            else digitalWrite(blue3, HIGH);        
       }
        
      void blinkRed3(int nr3) // number of blinks follwed by a delay 
       {
         if(PPO3 >1.00)
            for(int i=0; i< nr3; i++)
            
          {
            digitalWrite(red3,LOW); 
             delay(250); 
            digitalWrite(red3, HIGH);
            delay(250); 
          }
            else digitalWrite(red3, HIGH);           
       }

       
    
     void loop () 
    
       {
          blinkGreen1((int)(PPO1*10)); // flash green1  
          blinkBlue1((int)(PPO1*10)); // flash blue1
          blinkRed1((int)(PPO1*10)); // flash red1
          
          blinkGreen2((int)(PPO2*10)); // flash green2  
          blinkBlue2((int)(PPO2*10)); // flash blue2
          blinkRed2((int)(PPO2*10)); // flash red2
          
          blinkGreen3((int)(PPO3*10)); // flash green3  
          blinkBlue3((int)(PPO3*10)); // flash blue3
          blinkRed3((int)(PPO3*10)); // flash red3
                          


        lcd.setCursor(0, 1); //sets the cursor to column 0, line 1    
        inputReading1 = analogRead(sensor1); //getting the voltage reading from the 02 sensor    
        float voltage1 = inputReading1 * aref_voltage1; // converting that reading to voltage 
        voltage1 /= 1024.0;    
        lcd.print(voltage1, 2);//print actual voltage to lcd    
        delay(200);
    

        lcd.setCursor(6, 1); //sets the cursor to column 6, line 1     
        inputReading2 = analogRead(sensor2); //getting the voltage reading from the O2 sensor    
        float voltage2 = inputReading2 * aref_voltage2; // converting that reading to voltage
        voltage2 /= 1024.0; 
        lcd.print(voltage2, 2);  //print actual voltage to lcd
        delay(200);     
  
  
        lcd.setCursor(12, 1); //sets the cursor to column 12, line 2    
        inputReading3 = analogRead(sensor3); //getting the voltage reading from the O2 sensor    
        float voltage3 = inputReading3 * aref_voltage3; // converting that reading to voltage 
        voltage3 /= 1024.0; 
        lcd.print(voltage3, 2);  //print actual voltage to lcd
        delay(200);
 
      }
void loop () 
    {
    int blinks1 = PPO1*10;
    int blinks2 = PPO2*10;
    int blinks3 = PPO3*10;
    int maxBlinks = max(max(blinks1, blinks2), blinks3);

    for (int i=0; i<maxBlinks; i++)
         {
         digitalWrite(red1, blinks1 < i);
         digitalWrite(green1, blinks1 < i);
         digitalWrite(blue1, blinks1 < i);
         digitalWrite(red2, blinks2 < i);
         digitalWrite(green2, blinks2 < i);
         digitalWrite(blue2, blinks2 < i);
         digitalWrite(red3, blinks3 < i);
         digitalWrite(green3, blinks3 < i);
         digitalWrite(blue3, blinks3 < i);

        delay(250);

         digitalWrite(red1,    LOW);
         digitalWrite(green1, LOW);
         digitalWrite(blue1,  LOW);
         digitalWrite(red2,    LOW);
         digitalWrite(green2, LOW);
         digitalWrite(blue2,   LOW);
         digitalWrite(red3,    LOW);
         digitalWrite(green3, LOW);
         digitalWrite(blue3,   LOW);

        delay(250);
        }

        lcd.setCursor(0, 1); //sets the cursor to column 0, line 1    
        inputReading1 = analogRead(sensor1); //getting the voltage reading from the 02 sensor    
        float voltage1 = inputReading1 * aref_voltage1; // converting that reading to voltage 
        voltage1 /= 1024.0;    
        lcd.print(voltage1, 2);//print actual voltage to lcd    
        delay(200);
    

        lcd.setCursor(6, 1); //sets the cursor to column 6, line 1     
        inputReading2 = analogRead(sensor2); //getting the voltage reading from the O2 sensor    
        float voltage2 = inputReading2 * aref_voltage2; // converting that reading to voltage
        voltage2 /= 1024.0; 
        lcd.print(voltage2, 2);  //print actual voltage to lcd
        delay(200);     
  
  
        lcd.setCursor(12, 1); //sets the cursor to column 12, line 2    
        inputReading3 = analogRead(sensor3); //getting the voltage reading from the O2 sensor    
        float voltage3 = inputReading3 * aref_voltage3; // converting that reading to voltage 
        voltage3 /= 1024.0; 
        lcd.print(voltage3, 2);  //print actual voltage to lcd
        delay(200);
 
      }

johnwasser:

void loop () 

{
    int blinks1 = PPO110;
    int blinks2 = PPO2
10;
    int blinks3 = PPO3*10;
    int maxBlinks = max(max(blinks1, blinks2), blinks3);

for (int i=0; i<maxBlinks; i++)
         {
         digitalWrite(red1, blinks1 < i);
         digitalWrite(green1, blinks1 < i);
         digitalWrite(blue1, blinks1 < i);
         digitalWrite(red2, blinks2 < i);
         digitalWrite(green2, blinks2 < i);
         digitalWrite(blue2, blinks2 < i);
         digitalWrite(red3, blinks3 < i);
         digitalWrite(green3, blinks3 < i);
         digitalWrite(blue3, blinks3 < i);

delay(250);

digitalWrite(red1,    LOW);
         digitalWrite(green1, LOW);
         digitalWrite(blue1,  LOW);
         digitalWrite(red2,    LOW);
         digitalWrite(green2, LOW);
         digitalWrite(blue2,   LOW);
         digitalWrite(red3,    LOW);
         digitalWrite(green3, LOW);
         digitalWrite(blue3,   LOW);

delay(250);
        }

lcd.setCursor(0, 1); //sets the cursor to column 0, line 1   
        inputReading1 = analogRead(sensor1); //getting the voltage reading from the 02 sensor   
        float voltage1 = inputReading1 * aref_voltage1; // converting that reading to voltage
        voltage1 /= 1024.0;   
        lcd.print(voltage1, 2);//print actual voltage to lcd   
        delay(200);

lcd.setCursor(6, 1); //sets the cursor to column 6, line 1     
        inputReading2 = analogRead(sensor2); //getting the voltage reading from the O2 sensor   
        float voltage2 = inputReading2 * aref_voltage2; // converting that reading to voltage
        voltage2 /= 1024.0;
        lcd.print(voltage2, 2);  //print actual voltage to lcd
        delay(200);     
 
 
        lcd.setCursor(12, 1); //sets the cursor to column 12, line 2   
        inputReading3 = analogRead(sensor3); //getting the voltage reading from the O2 sensor   
        float voltage3 = inputReading3 * aref_voltage3; // converting that reading to voltage
        voltage3 /= 1024.0;
        lcd.print(voltage3, 2);  //print actual voltage to lcd
        delay(200);

}

John this works great!!! I know I've been a thorn in your side and you have been so helpful. I have only one last request. Is there any way to separate the three colors of the RGB led so that I can use green for 0-.50 volts, blue fof .50-1.00 volts and red for 1.00-1.60 volts for each of the three leds?

thanx again.

    for (int i=0; i<maxBlinks; i++)
         {
         digitalWrite(green1, blinks1 <= 5 && blinks1 < i);
         digitalWrite(blue1 , blinks1 > 5 && blinks1 < =10 && blinks1 < i);
         digitalWrite(red1   , blinks1 >10 && blinks1< i);

         digitalWrite(green2, blinks2 <= 5 && blinks2 < i);
         digitalWrite(blue2 , blinks2 > 5 && blinks2 < =10 && blinks2 < i);
         digitalWrite(red2   , blinks2 >10 && blinks2< i);

        digitalWrite(green3, blinks3 <= 5 && blinks3 < i);
         digitalWrite(blue3 , blinks3 > 5 && blinks3 < =10 && blinks3 < i);
         digitalWrite(red3   , blinks3 >10 && blinks3< i);

        delay(250);

         digitalWrite(red1,    LOW);
         digitalWrite(green1, LOW);
         digitalWrite(blue1,  LOW);
         digitalWrite(red2,    LOW);
         digitalWrite(green2, LOW);
         digitalWrite(blue2,   LOW);
         digitalWrite(red3,    LOW);
         digitalWrite(green3, LOW);
         digitalWrite(blue3,   LOW);

        delay(250);
        }

        lcd.setCursor(0, 1); //sets the cursor to column 0, line 1    
        inputReading1 = analogRead(sensor1); //getting the voltage reading from the 02 sensor    
        float voltage1 = inputReading1 * aref_voltage1; // converting that reading to voltage 
        voltage1 /= 1024.0;    
        lcd.print(voltage1, 2);//print actual voltage to lcd    
        delay(200);
    

        lcd.setCursor(6, 1); //sets the cursor to column 6, line 1     
        inputReading2 = analogRead(sensor2); //getting the voltage reading from the O2 sensor    
        float voltage2 = inputReading2 * aref_voltage2; // converting that reading to voltage
        voltage2 /= 1024.0; 
        lcd.print(voltage2, 2);  //print actual voltage to lcd
        delay(200);     
  
  
        lcd.setCursor(12, 1); //sets the cursor to column 12, line 2    
        inputReading3 = analogRead(sensor3); //getting the voltage reading from the O2 sensor    
        float voltage3 = inputReading3 * aref_voltage3; // converting that reading to voltage 
        voltage3 /= 1024.0; 
        lcd.print(voltage3, 2);  //print actual voltage to lcd
        delay(200);
 
      }