Blink LED while reading Analog input voltage

Hello community !

I need a little help here.
Being a beginner at programming, I must say learning to program, I'm actually struggling with my sketch.

I need to blink a LED while I'm reading through Analog port 0, a voltage between 0 - 5 volts.

All I can achieve so far, is light up a red LED for the time the voltage is read, when the voltage has reached
the preset value, the green LED turns on, and the red LED turns off.

Examples on the net aren't very clear to my understanding....

Is there a simple way to blink that red LED while reading the Analog voltage ?

Any help would be greatly appreciated !

Here is my sketch for you too look at;

int vin = 0;   // Define Analog IN here
int start = 5; // Start switch
int stop = 4;  // Stop switch
int led = 3;   // A green LED is used here, but a relay could also be used
int led2 = 2;  // Activity LED, red
float value;


void setup()
{
  pinMode(vin, INPUT);             // Define I/O ports
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(start, INPUT_PULLUP);
  pinMode(stop, INPUT_PULLUP);
  digitalWrite(led, LOW);          // Define I/O state for LED
}

void led_on()                      // Turn ON relay/LED 
{
  digitalWrite (led, HIGH);
}


void led_off()                     // Turn OFF relay/LED  
{
  digitalWrite (led, LOW); 
}


void tuning_led_on()               // Turn ON Tuning LED
{
  digitalWrite(led2, HIGH);
}
 

void tuning_led_off()              // Turn OFF Tuning LED
{
  digitalWrite (led2, LOW);
}


void loop()
  
{
 value=analogRead(vin);               
 float vin = value * (5.0 / 1023); // Calculate analog voltage in
  
 if (vin < 0.20)                   // Flush anything below 0.02 volts
  {
    vin = 0.0;                     
  }  
  delay(100);

// ---------------- START TUNING WHEN YELLOW BUTTON IS PRESSED ----------------------
  

  if (digitalRead(start) == LOW)   // YELLOW START BUTTON PRESSED

  {
   led_on();                       // Turn ON relay/LED
   tuning_led_on();                // Turn ON Tuning LED
  }


// ---------------- STOP TUNING WHEN RED BUTTON IS PRESSED ----------------------
  

  if (digitalRead(stop) == LOW)    // RED STOP BUTTON PRESSED

  {
   led_off();                      // Turn OFF relay/LED
   tuning_led_off();               // Turn OFF Tuning LED
  }

// -------------------- STOP TUNING IF ANALOG IN IS LOW -------------------------


  if (value < 0.20)                // LOW SENSOR VALUE REACHED

  {
   delay(2000);                    // Delay to let the ADC to stabilize
   led_off();                      // Turn OFF relay/LED
   tuning_led_off();               // Turn OFF Tuning LED
  }
  

}

Why are you using the same variable name "vin" for the analog pin and also the result of reading from that pin? That's an ideal way to give yourself scope problems and also to confuse anyone trying to read your program.

Anyway to do several things at once you need to get rid of the delays. Have a look at the Blink without delay example in the IDE and read the "Useful links..." post at the top of this forum.

Steve

The analogRead() function is a blocking function. It only takes 104 microseconds. You wouldn't see an LED blink even if you could blink it while analogRead() was executing.

You could use the blink without delay philosophy and a boolean variable and blink the LED when the reading is below some threshold, but that is not the same thing.

Hi guys.

Thanks for the quick reply and suggestions.

Maybe my question wasn't clear enough.

What I meant was, I want to flash a LED, after hitting the start button, that LED will stop flashing when the preset Analog value has been reached.

Clear as mud ?

And, I took a look at the IDE examples, but I still have no clue how and where to do it...

Thanks again.

Gilles

You start by defining a boolean variable, needToBlink, initialized to false.

When the start switch BECOMES pressed, set needToBlink to true.

When the analog value exceeds some threshold, set needToBlink to false.

Then, at the end of loop(), add:

if(needToBlink)
{
   blink();
}

The blink() function will be called thousands of times per second. In that function, you decide whether or not to toggle the state of the LED as illustrated in the blink without delay example.

If you want the LED to be off when there is not need to be blinking, as opposed to just not blinking, add an else clause to that if statement, and turn the LED pin off.

PaulS, and all others

Thanks for putting me on the right track, all is working as I want now !

I can now go ahead with the rest of this project.

Thanks again !

Case closed !