How to use timer (millis()) properly

Hi everybody,

i will post my code below, what im trying to accomplish is that when my x and y variables reach certain value and stay in those valuse for 5 sec, i want R, G and B values to change after that,and than reset that timer again, this is what i have come up with for now, but what happens is, when x and y fulfill that IF statement it instantly changes those R G B values , it doesnt wait for 5 sec like i would want to.. pls help if you can...

int start, timer1;
int R=0, G=0, B=0;
int leftSensor, rightSensor;

void Setup(){
...
start = millis();
...
}

void draw(){
...
red(leftSensor, rightSensor, timer1);
...
}

 void red(int x, int y, int t)
{
      if(x <= 62 && x >=58 && y<=65 && y >= 63 )
  {
      t = millis()-start;
      if(t >= 5000){
      R = 204; G = 0; B = 0;
      }
  }
  t = 0;
}
Tagged:

Answers

  • edited May 2017

    cant get my code show properly nvm

  • Answer ✓
    int time;
    
    void setup() {
      size(400, 400);
    }
    
    void draw() {
      background(0);
      fill(64);
      if (dist(mouseX, mouseY, 200, 200)<100) {
        fill(128);
        if (millis() > time) {
          fill(255);
        }
      } else {
        time = millis() + 3000;
      }
      ellipse(200, 200, 200, 200);
    }
    
  • edited May 2017

    Yup, this one should work, where did u find it if i may ask, is there example for it or it was your doing?

    Thank You very much mate :)

  • I wrote it, just now. And now I depart, in search of pancakes and coffee.

  • I wish you luck than, in your pursuit.

  • edited May 2017

    nvm, thx again, got it

  • edited May 2017

    How would i make this counter to work, i want it to reset every time it reaches 3000ms, i tried this but it doesnt work at all

             void red(int x, int y)
            {
                  if(x >= 56 && x<= 60 && y <=67 && y >= 63 )
              {
                  if(millis() > t){
                  R = 204; G = 0; B = 0;
                  }
              } else {
               t = millis() + 3000 ;
              }
            }
    

    it just instantly changes the RGB values, it doesnt wait at all,

  • edited May 2017

    Meh, i just removed it from void function and put in draw(), and made type boolean as a condition... now it works

  • edited May 2017

    @BodegaB -- one tip -- red() is a language built-in -- if you are making a color function you should probably name it something else, like turnRed, redBG, redTimer etc.

  • Didn't know that, thank you ;)

Sign In or Register to comment.