Help with Arduino window comparator?

Hi, everybody; I have my circuit on a breadboard and it works but when the voltages approximate the limits, the LEDs flicker. Seems as if hysteresis is needed but I don´t believe it can be implemented.
Could it be my layout? Something missing or wrong with my circuit?
Attaching my code because I don´t have internet at home and we are not allowed to install software at cyber.Thanks, Alberto

circuit.ino (727 Bytes)

What do you mean "the voltages approximate the limits"?

PS Circuit is missing a 100nF ceramic cap over the chip. Every chip likes that :slight_smile:

alah:
Hi, everybody; I have my circuit on a breadboard and it works but when the voltages approximate the limits, the LEDs flicker. Seems as if hysteresis is needed but I don´t believe it can be implemented.
. . .

You could try something like this (untested). It uses hysteresis. It should work like this. If the voltage is inside the window, it tends to stick in the window. If it is outside the window, it tends to stick outside the window.

. . .
const float hyst = 0.1 ;  // adjust as required
bool inWindow = true ;
. . . 

void loop()
{
  int VinValue = analogRead (4);
  float VinVoltage = VinValue * (5.0 / 1023.0);

  if  ( 
        ( inWindow && VinVoltage > 2.9 - hyst  && VinVoltage < 3.7 + hyst) || 
        ( !inWindow && VinVoltage > 2.9 + hyst  && VinVoltage < 3.7 - hyst)
      )
  {
    digitalWrite (VinOK, HIGH);
    digitalWrite (VinLOW, HIGH);
    digitalWrite (VinHIGH, LOW);
    inWindow = true ;
  }
  else if ( VinVoltage <= 2.9 + hyst )
  {
    digitalWrite (VinOK, LOW );
    digitalWrite (VinLOW, HIGH);
    digitalWrite (VinHIGH, LOW);
    inWindow = false ;
  }
  else
  {
    digitalWrite (VinOK, LOW );
    digitalWrite (VinLOW, LOW);
    digitalWrite (VinHIGH, HIGH);
    inWindow = false ;
  }
}

Also see here for a discussion about hysteresis:
https://forum.arduino.cc/index.php?topic=526806.0

septillion:
What do you mean "the voltages approximate the limits"?

I meant that at the voltages of the window plus and minus approx. 0.25v the LEDs flicker meaning that the output isnt swing form GND to V+ nor from V+ to GND.
And I do have the cap, thanks.

PS Circuit is missing a 100nF ceramic cap over the chip. Every chip likes that :slight_smile:

6v6gt; Ill try your code and reply by Friday, Thankss