Loading...
Logo
Processing Forum
Hello there dear people of Processing.

I'm now at my 4th day of learning Processing.
After a few brainbusters I've come to a point where I need your help.

I'm currently working on a test model of an idea that I have.
My goal is is have a step motor turning, with 8 magnets or rfid chips on the side.
This will be read by a sensor. This sensor will tell the software at what degrees (0, 45, 90, 135 etc) the step motor is.
The software will then adjust the movie to to right frame, according to the degrees of the step motor.

I've copy and pasted serval examples together and I've achieved some parts of it.
For now, the hardware part is just a press of the keyboard.
And instead of the movie, I have a timer.

But here's my problem. When the magnet passed the sensor (keyPressed), 
the current frame will be skipped to the right number.
So you will have a cut in the movie(which is a timer for now).

I want to achieve a speed up or slow down to adjust to the correct frame.
I think this is possible by tweening or easing, but I don't know how to implement this.

Underneath is my code. I hope this and my question makes any sense to you. If not, tell me.
If you have any tips on making this work, please let me know.

El Bucho

Copy code
  1. ///////////////////
  2. ///ElBucho////////
  3. //////////////////


  4. int actualSecs; //actual seconds elapsed since start
  5. int startSec = 0 ; //used to reset seconds shown on screen to 0
  6. int scrnSecs; //seconds displayed on screen (will be 0-360)
  7. int restartSecs; //number of seconds elapsed at last click or 360 sec interval
  8. int totalSec = 360; // total seconds

  9. void setup() {
  10.   size(600, 500);

  11.   smooth();
  12.   PFont font;
  13.   font = createFont("Arial", 72); // Arial, 16 point, anti-aliasing on
  14.   textFont(font, 72);

  15. void draw() {
  16.   background(0);

  17.   //////////////////////////////////// 
  18.   //*this section is the "mathy" part*//
  19.   ////////////////////////////////////
  20.   actualSecs = millis()/50; //converts to timer.
  21.   scrnSecs = actualSecs - restartSecs; //seconds to be shown on screen  
  22.   
  23. //the skip part
  24.   if ((scrnSecs) >= 338 && (scrnSecs) <= 359 ||(scrnSecs) >= 0 && (scrnSecs) <= 22)  {  //range 1 skip to 0 degrees
  25.        if (keyPressed) {
  26.        restartSecs = actualSecs - 0;
  27.        scrnSecs = 0; 
  28.        }
  29.   }
  30.   if ((scrnSecs) >= 23 && (scrnSecs) <= 67) {  //range 2 skip to 45 degrees
  31.     if (keyPressed) {
  32.     restartSecs = actualSecs - 45;
  33.     scrnSecs = 45;
  34.     }
  35.   }
  36.   if ((scrnSecs) >= 68 && (scrnSecs) <= 112) {  //range 3 skip to 90 degrees
  37.     if (keyPressed) {
  38.     restartSecs = actualSecs - 90;
  39.     scrnSecs = 90;
  40.     }
  41.   }
  42.   if ((scrnSecs) >= 113 && (scrnSecs) <= 157) {  //range 4 skip to 135 degrees
  43.     if (keyPressed) {
  44.     restartSecs = actualSecs - 135;
  45.     scrnSecs = 135;
  46.     }
  47.   }
  48.   if ((scrnSecs) >= 158 && (scrnSecs) <= 202) {  //range 5 skip to 180 degrees
  49.     if (keyPressed) {
  50.     restartSecs = actualSecs - 180;
  51.     scrnSecs = 180;
  52.     }
  53.   }
  54.   if ((scrnSecs) >= 203 && (scrnSecs) <= 247) {  //range 6 skip to 225 degrees
  55.     if (keyPressed) {
  56.     restartSecs = actualSecs - 225;
  57.     scrnSecs = 225;
  58.     }
  59.   }
  60.   if ((scrnSecs) >= 248 && (scrnSecs) <= 292) {  //range 7 skip to 270 degrees
  61.     if (keyPressed) {
  62.     restartSecs = actualSecs - 270;
  63.     scrnSecs = 270;
  64.     }
  65.   }   
  66.   if ((scrnSecs) >= 293 && (scrnSecs) <= 337) {  //range 8 skip to 315 degrees
  67.     if (keyPressed) {
  68.     restartSecs = actualSecs - 315;
  69.     scrnSecs = 315;
  70.     }
  71.   }   

  72. // the text part
  73.   
  74.     if ((scrnSecs) >= 0 && (scrnSecs) <= 22) {  //range 1 skip to 0 degrees
  75.     text("1, 0-22 0*", width/2,height/4);
  76.    }   
  77.   if ((scrnSecs) >= 23 && (scrnSecs) <= 67) {  //range 2 skip to 45 degrees
  78.     text("2, 23-67 45*", width/2,height/4);
  79.    }
  80.   if ((scrnSecs) >= 68 && (scrnSecs) <= 112) {  //range 3 skip to 90 degrees
  81.     text("3, 68-112 90*", width/2,height/4);
  82.    }
  83.   if ((scrnSecs) >= 113 && (scrnSecs) <= 157) {  //range 4 skip to 135 degrees
  84.     text("4, 113-157 135*", width/2,height/4);
  85.    }
  86.   if ((scrnSecs) >= 158 && (scrnSecs) <= 202) {  //range 5 skip to 180 degrees
  87.     text("5, 158-202 180*", width/2,height/4);
  88.    }
  89.   if ((scrnSecs) >= 203 && (scrnSecs) <= 247) {  //range 6 skip to 225 degrees
  90.     text("6, 203-247 225*", width/2,height/4);
  91.    }
  92.   if ((scrnSecs) >= 248 && (scrnSecs) <= 292) {  //range 7 skip to 270 degrees
  93.     text("7, 248-292 270*", width/2,height/4);
  94.    }   
  95.    if ((scrnSecs) >= 293 && (scrnSecs) <= 337) {  //range 8 skip to 315 degrees
  96.     text("8, 293-337 315*", width/2,height/4);
  97.    }   
  98.    if ((scrnSecs) >= 338 && (scrnSecs) <= 359) {  //range 9(1) skip to 0 degrees
  99.     text("9, 338-359 360*", width/2,height/4);
  100.    } 
  101.   

  102.   //if (keyPressed) { //if any key is pressed, restart timer
  103.     //restartSecs = actualSecs - skipSec ; //stores elapsed SECONDS
  104.     //scrnSecs = skipSec; //restart screen timer
  105.   //}

  106.   if (scrnSecs == totalSec) { //after 360 secs, restart second timer 
  107.     restartSecs = actualSecs;   //placeholder for this second in time
  108.     scrnSecs = startSec; //reset to zero
  109.   }

  110.   println(scrnSecs); //print timer to console (secs)

  111.   //displays time on screen
  112.   textAlign(CENTER);
  113.   fill(255);
  114.   text(nf(scrnSecs, 2), width/2, height/2);
  115. }
 

Replies(1)

I see you posted a while ago, so I hope my answer doesn't come too late.

The easiest way to create easing, is simply like this:
Copy code
  1. int goal;
  2. int pos;

  3. if (goal>pos) {
  4. pos++;
  5. } else if (goal<pos)
  6. pos--;
  7. }
That creates a linear transition. And you should put it in a function so you can reuse it in different instances.

If you'd like a more "eased" version:

Copy code
  1. int goal;
  2. int pos;

  3. if (goal>pos) {
  4. pos+=pos-goal;
  5. } else if (goal<pos) {
  6. pos-=goal-pos;
  7. }