X > Value for 2 sec, problem

Okie after all i search and found im stucked. I want to change the state of a LED if a variable in my program is > 4 (sum_back > 4 in my problem ) for 2 sec. If the value continues to be > 4 even more than the 2 sec passes, i want to keep its state and dont change again. Something like that but no with switch (video on the end) : http://volatilebit.com/electronics/minor-arduino/item/11-press-hold-switch-arduino

Anyone can help;

I want to change the state of a LED if a variable in my program

which I didn't post

AWOL:

I want to change the state of a LED if a variable in my program

which I didn't post

what you mean mate?

What do I mean? I mean, you didn't post a program, so how are we supposed to help? Ouija board? Crystal ball?

..when you turn ON (or off) , it cannot be changed the next N seconds ?

Its part of a big code , i hope this help you:

     sum_back=0;
     for(int i=40;i<46;i++)
    {
      for(int j=45;j<46;j++)
      {
        pixel = cam.getPixel(&f_data,i,j);
        if(pixel==1)
        {
         sum_back++;
        }
      }
    }



    if(sum_back>4)
    {
      start_hold=millis();
      allow = true;
    }
    else
    {
      allow=false;
    }
    
    if(allow==true && sum_back>4)
    {
      if((millis()-start_hold) >= HOLD_DELAY)
      {
        if(mode=='F')
        {
          mode='B';
        }
        else if(mode=='B')
        {
         mode='F'; 
        }
      }
    }

so if the tracked pixels in the place i want are > 4 for 2+ seconds, i want the variable mode switch from F->B or B->F.

It changes the state,but if i keep my object on the same possition over 2sec… it continues changing state

I like the comments. Good luck with your project.

Could help me and told me if i missed serious info that could make you understand. Its my 2-3 post on forum . If you want asnwer heronic go to other post

Try posting ALL of your code with appropriately located comments describing what it does. You did get the code tags working! Good work!

this is not what I would do:

 if(sum_back>4)
    {
      start_hold=millis();
      allow = true;
    }
    else
    {
      allow=false;
    }
    
    if(allow==true && sum_back>4)

1st.test : sum_back>4 will set allow true

2nd test : Why test both? The logic here is NOT suited to my head..

Tell again, what is the ACTUAL problem ?

When i go my object on the position of the screen of tracking (tracked pixels > 4) for 2 sec, the state of mode is changing from F->B or B->F but if i keep my object there the state is changing all time by the period i set on the start (2 sec about).

I want the state change if i keep my object 2 sec there, if i keep more it wont change. Will need to remove the object (tracked pixels 0 ) and put it again to the tracking position (tracked pixels > 4) to change state.

     sum_back=0;                                  
     for(int i=40;i<46;i++)
    {
      for(int j=45;j<46;j++)
      {
        pixel = cam.getPixel(&f_data,i,j);                      // sum_back :  counts the number of tracked pixels on the position i want
        if(pixel==1)
        {
         sum_back++;
        }
      }
    }
    if(sum_back>4)
    {
      start_hold=millis();
      allow = true;                               
    }
    else
    {
      allow=false;
    }
    
    if(allow==true && sum_back>4)
    {
      if((millis()-start_hold) >= HOLD_DELAY)                             // hold_delay : is the period the object must be on the position ,
      {                                                                                                          // actulay the time that sum_back must be > 4 
        if(mode=='F')
        {
          mode='B';
        }
        else if(mode=='B')                                                                      // mode: a char variable that must be 'F' or 'B' , must change 
        {                                                                                                        // if sum_back > 4 for 2 sec
         mode='F'; 
        }
      }
    }

knut_ny:

2nd test : Why test both? The logic here is NOT suited to my head..

Tell again, what is the ACTUAL problem ?

yeah its wrong, was one of my tries to make it not changing when i keep my object for more that 2 sec to the spot..

Write some code that does your pixelly comparison stuff and just returns a true/false value. I suggest implementing it as a function.

Now you're just looking for the result to remain true for longer than two seconds. You can do it like this:

Call the comparison code repeatedly. If the result is false, save the value of millis() in a variable that records when the result was last false. Subtract the value of that variable from the current value of millis() to see how long ago the result was last false. If that exceeds two seconds, your condition has been met so you can do whatever-it-is you want to happen.

int HOLD_DELAY = 2000;
int last_back=0;
unsigned long start_hold;
boolean allow = false;

And thats how i think of it , code seems right to me… but continue to change state when object is on tracked area for more than 2 sec

     sum_back=0;
     for(int i=40;i<46;i++)
    {
      for(int j=45;j<46;j++)
      {
        pixel = cam.getPixel(&f_data,i,j);                    // count the tracked pixels
        if(pixel==1)
        {
         sum_back++;
        }
      }
    }
  


    if(sum_back>4 && last_back==0)                // for pixels > 4 , mark the time , allow mode state changes ..
    {
      start_hold=millis();
      allow = true;
    }
     
    if(allow==true && sum_back>4 && last_back>4)             //if pixels remains > 4 , for longer than 2 sec .. change state of mode and prevent multiple state changes
    {
      if((millis()-start_hold) >= HOLD_DELAY)
      {
        if(mode=='F')
        {
          mode='B';
        }
        else if(mode=='B')
        {
         mode='F'; 
        }
        allow = false;      
      }
    }
    
    last_back=sum_back;

(I'd like to tide up som codeing) Are all your variables declared public? (and nowhere else)

some of them i use on an interupt fuction for printing on LCD

this one…:
for(int j=45;j<46;j++) WHY a for loop for 1 turn ?

by i,j we check a specific area. The area was bigger but we get it smaller until we find the problem. Will be bigger on end

I cannot see that code shown is the problem. You mentioned interrupts. What is hidden in those routines. Do they change any variables to communicate with other code ? In that case - take a look at VOLATILE declaration

i used volatile on start of programm to the variables are used inside the print LCD interupt.

noone of the variables on the code i posted used on interupts, only "mode" is printing on the interupt