...create a simple ascending/descending series of values

edited September 2017 in Questions about Code

My aim would be to create a counter, which starts at 0, counts up to 10, then down again to 0, and again starts from the beginning. Somehow i can not figure out how to do that:

int countdown =0;
int seconds, seconds2, startTime;

void setup() {



    startTime = millis()/1000 + countdown;
}

void draw() {

  seconds = startTime + millis()/1000;

    if (seconds > 10)    seconds = startTime - millis()/1000;
    if (seconds < 0)    seconds = startTime + millis()/1000;

    println (seconds);
}
Tagged:

Answers

  • Conceptually your counter is a bouncing ball. Just like a ball might have a direction vector, and any collision with a wall causes the vector to switch direction....

    int x = 0;
    int xstep = 1;
    void setup(){
      frameRate(4);
    }
    void draw(){
        if (x >= 10){
          xstep = -1;
        } else if (x <= 0){
          xstep = 1;
        }
        x = x + xstep;
        println(x);
    }
    

    This will "bounce."

  • Yes, perfect! Bouncing-Ball, have not thought about that!

  • ...now thinking further, would it be possible to increase the "limit" (in this case 10) always with 1, if reached? f.e. in the 2nd pass to 11, the 3rd pass to 12,...

  • Answer ✓

    sure in line 7 replace 10 with upperBound

    After line 8 say upperBound++;

  • Answer ✓

    Before setup ():

    int upperBound=10;

  • thank you, actually simple. i think i'm starting to understand loops...

  • i "remapped" the number-range starting at 90, counting up and down. now i wonder how to set limits, f.e. at 130 (50 when counting down), where the loops resets to the starting position. or even better, shrink the range again down. i guess it is another if situation inside the existing one, or?

    int x = 90;
    int xstep = 1;
    int upperBound = 91;
    int lowerBound = 89;
    
    void setup(){
      frameRate(4);
    }
    void draw(){
    
    
    
    
        if (x >= upperBound){
          xstep = -1;
          upperBound++;
        } else if (x <= lowerBound){
          xstep = 1;
          lowerBound--;
        }
        x = x + xstep;
        println(x);
    }
    
  • That's right

    In line 16 you see ++

    replace this by upperBound += upperBoundStep;

    Then similar to line 14 say

    if (upperBound> 130)

    upperBoundStep = -1;

  • edited September 2017

    thanks for your fast answers! i don't understand it completely:

    int x = 90;
    int xstep = 1;
    int upperBound = 91;
    int lowerBound = 89;
    int upperBoundStep;
    
    void setup(){
      frameRate(8);
    }
    void draw(){
    
    
    
     if (x >= upperBound){
          xstep = -1;
          upperBound += upperBoundStep;
    
         if (upperBound > 130){
          upperBoundStep = -1;} 
    
    
        } else if (x <= lowerBound){
          xstep = 1;
          lowerBound--;
         }
        x = x + xstep;
        println(x);
     }
    
  • Basically the upperBound value dows the same as the x value:

    It behaves like a ball that gets reflected

  • In line 5

    upperBoundStep=1;

    upperBound changes very very slowly at the moment, only when x touches it (line 14-16)

  • int x = 90;
    int xstep = 1;
    
    int upperBound = 91;
    int lowerBound = 89;
    
    int upperBoundStep=8;
    
    int i; 
    
    void setup() {
      //  frameRate(8);
      size(1000, 800);
    }
    
    void draw() {
    
      if (x >= upperBound) {
    
        xstep = -1;
        upperBound += upperBoundStep;
        x=upperBound-1; 
    
        if (upperBound > 130) {
          upperBoundStep = -1 * abs(upperBoundStep); // always neg 
          upperBound=130;
        } else if (upperBound < 93) {
          upperBoundStep = abs(upperBoundStep); // always pos
          upperBound=93;
        }
        //
      } else if (x <= lowerBound) {
        xstep = 1;
        lowerBound--;
      }
    
      x = x + xstep;
    
      println(x);
      stroke(255, 0, 0);
      point(i, x) ; 
    
      stroke(0, 0, 255);
      point(i, upperBound) ; 
    
      stroke(0, 255, 2);
      point(i, lowerBound) ; 
    
      i++;
    }
    
  • or use sinus

    float x ;
    int xstep = 1;
    
    int upperBound = 91;
    int lowerBound = 89;
    
    int upperBoundStep=8;
    
    int i=0; 
    
    float radius=120; 
    float radiusAdd=-.1; 
    
    float x1 = 0; 
    void setup() {
      //  frameRate(8);
      size(1600, 800);
    }
    
    void draw() {
      x1=x; 
      x = sin(radians(i*15)) * radius + height/2; 
    
      println(x);
      stroke(255, 0, 0);
      point(i, x); 
      line(i, x, i-1, x1);
    
    
      //stroke(0, 0, 255);
      //point(i, upperBound) ; 
    
      //stroke(0, 255, 2);
      //point(i, lowerBound) ; 
    
      i++;
    
      radius+=radiusAdd;
      if (radius<48) 
        radiusAdd=.1;
      else if (radius>88)
        radiusAdd=-.1;
    }
    
Sign In or Register to comment.