Window comparator

I'm trying to make a comparator, if the input voltage changes slowly, everything works normally, however, If I quickly turn the potentiometer (I wired to the input), the two outputs are actives at the same time !

(I don't want to slow down the input response.)
My code is:

double gap = abs((SetLevel - Hyst) - analog_input_1); 
double gap2 = abs((SetLevel + Hyst) - analog_input_1);   
if (analog_input_1 >= (SetLevel - Hyst)) 
if (analog_input_1 >= (SetLevel + Hyst))
      {
         downout = gap2 ;
         upout == 0;
           }
          else
            {
          downout == 0;
      }
        analogWrite(DWN,downout);
if (analog_input_1 <= (SetLevel - Hyst))
if (analog_input_1 <= (SetLevel + Hyst))
      {
         upout = gap;
         downout == 0;
          }
          else
            {
          upout == 0;
      }
         analogWrite(UP,upout);

that's not your code it's a (poorly indented) snippet (Snippets R Us!)

if Hyst is positive and not null then if analog_input_1 is less or equal to (SetLevel - Hyst) then you are sure it's less than (SetLevel + Hyst)...

something fishy there

Hi, @glamo
It would be better for you to post your complete code.

If it is very big and involved, then write some code that JUST uses the inputs, variables and outputs involved.

If you wrote this code in stages, you should already have such a section of code.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Hi,
What do you expect this bit of code to do?

if (analog_input_1 >= (SetLevel - Hyst)) 
if (analog_input_1 >= (SetLevel + Hyst))
      {
         downout = gap2 ;
         upout == 0;
           }
          else
            {
          downout == 0;
      }
        analogWrite(DWN,downout);

Tom... :smiley: :+1: :coffee: :australia:

Hello JML,
Of course this code is not created by my self, it is from Arduino Gods

Thanks Tom Gerge, you are so kind as usual.
When there is a slow voltage change, everything works correctly, but when the fast voltage changes, it is not worked, I try to understand the reason

crippled gods?

Hi,
A circuit diagram would also help.
What model Arduino controller are you using?

I would think ONE if... else... statement with some conditional logic in it would work.

Pseudo code.

Read INPUT

if( (INPUT >= (Setlevel - Hyst)  &&  INPUT <= (Setlevel + Hyst))
{ 
INPUT INSIDE WINDOW
}
else
{
INPUT OUTSIDE WINDOW
}

Something like that.

Tom... :smiley: :+1: :coffee: :australia:

Hello glamo
Take a view here to design a window comparator:

Have a nice day and enjoy programming in C++ and learning.
Errors and omissions excepted.
Дайте миру шанс!

Thanks Tom, I am using Arduino Uno.
I will try your code

Thank you Paul

I hope someone with eagle eyes will see the problem line

it's possibly in the code you did not post...

➜ do yourself a favour and please read How to get the best out of this forum and post accordingly

No, my crystalball is in the dish washer

Top rack only

Not sure. How heat resistant is fairy dust? :thinking:

I know that if you don't keep them covered, they can burn your house down, but the lens doesn't have to get too hot to focus the energy.

Hello Tom,
The problem mentioned only occurs in fast voltage changes
Is there any way to calculate or measure the code instruction time/cycle?

Hi,

Did you write some code using my pseudo code as a guide?
It would have been fewer lines than your original code.

How are you monitoring the changes?
If you are using the Serial monitor on the IDE, make sure you use 115200 baud rate.

Your COMPLETE code would help us work out your problem.
To add code please click this link;

Are you trying to control a PWM output, if so where are your lines of code to prevent the PWM value not going below 0 or higher than 255?

Why use a pot and not a centre off SPDT instead of a pot?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Hi,
Can you explain the logic behind using a pot, window comparator and a PWM output are supposed to work?

  1. If the pot value is inside the window, what is supposed to happen/output?
  2. If the pot value is outside the window, what is supposed to happen/output?

OR ARE YOU TRYING;

  1. If the pot value is above the window, what is supposed to happen/output?
  2. If the pot value is inside the window, what is supposed to happen/output?
  3. If the pot value is below the window, what is supposed to happen/output?

Tom.... :smiley: :+1: :coffee: :australia: