How to control loop

edited February 2018 in Arduino

Hello, I'm relatively new to processing and I'm trying to make a reaction game using an Arduino library.

Answers

  • If you want to draw your score every frame, then move the call that draws the score outside the if statement.

    As for the ball, can you please be more specific? What exactly are you trying to do? Can you write out a series of steps, in English, that you want the ball to follow? Then think about implementing those steps in code.

  • you can hit ctrl-t to get better indents automatically

  • Thankyou, yes I've tried any for some reason it still doesn't show. As for the ball, on my page number two I am going to continue with the loop, however this time I want it to enter the screen at random times to be unexpected. I presume this would be random number generating? But I'm clueless as to how to do that.

  • this should give you (1)

    //PImage bg;
    int pageNumber = 1;
    int circX=0;
    int score=0;
    int totalscore=0;
    /*
    
     */
    import processing.serial.*;
    import cc.arduino.*;
    import com.tinkerkit.*;
    
    Arduino arduino;
    
    //declare the button
    TKButton but;
    
    void setup() {  
    
      size(600, 600); 
      // bg = loadImage("background.jpg");
    
      // background (0);
    
      println(Arduino.list());
    
      arduino = new Arduino(this, Arduino.list()[1], 57600);    
    
      //for every tinkerkit component we have to pass
      //the arduino and the port
      but = new TKButton(arduino, TK.I0);
    }
    
    void draw() {
      if (pageNumber==1) {
    
        background (0);
        score++;
    
        // display !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        fill(255);
        textSize(45);
        text (score, 267, 146);
    
        //if the button is pressed, change the ellipse to red
        if (but.read() == TK.HIGH) {
          fill (194, 49, 49);
          ellipse (301, 300, 255, 255);
          circX+=10;
          textSize(45);
          fill(249, 253, 255, 200);
          if (circX >=500) {
            circX=0;
          }
        }    
        // -----------------------------
        else {
          fill (255, 255, 255);
          ellipse (301, 300, 255, 255);
        }
    
    
        fill(0);
        ellipse(circX, height/2, 50, 50);
        circX+=10;
    
        if (circX > width) {
          circX=0;
        }
    
    
        //print the button values
        println(but.read());
    
        if ((circX > 200) && (circX < 400) && (but.read() == TK.HIGH)) {
          score+=1;
          println("score is "+score);
        }
      }
      // ----------------------------------
      else if (pageNumber==2) {  // better use else if here 
        //background(bg);
      }
    }
    
    void keyPressed() {
      if (key =='1') {
        pageNumber = 1;
        println(pageNumber);
      }
      if (key =='2') {
        pageNumber = 2;
        println(pageNumber);
      }
    }
    
  • edited January 2018

    this shows (2)

    a timer with a random value let's the ball wait before it starts at 0 again

    (I deleted the arduino stuff to be able to run the sketch)

    post your entire sketch when asking something please

    //PImage bg;
    int pageNumber = 1;
    int circX=0;
    int score=0;
    int totalscore=0;
    
    boolean timerIsOn=false;
    int timer; 
    int wait;
    
    void setup() {  
    
      size(600, 600);
    }
    
    void draw() {
      if (pageNumber==1) {
    
        background (0);
    
    
        if (timerIsOn) {
    
          // do nothing 
    
          // display 
          fill(255);
          textSize(45);
          text (score, 267, 146);
    
          fill (194, 49, 49);
          ellipse (301, 300, 255, 255);
    
          if (millis()-timer>wait) {
            timerIsOn=false;
          }
        } else {
    
          score++;
    
          // display 
          fill(255);
          textSize(45);
          text (score, 267, 146);
    
          //if the button is pressed, change the ellipse to red
          fill (194, 49, 49);
          ellipse (301, 300, 255, 255);
          circX+=10;
          textSize(45);
          fill(249, 253, 255, 200);
    
          fill(0);
          ellipse(circX, height/2, 50, 50);
          circX+=10;
    
          if (circX > 500) {
            circX=0;
            timerIsOn=true;
            timer=millis(); 
            wait=int(random(90, 4900)); // these are millis
          }
        }//else timer
      }//page number 
      // ----------------------------------
      else if (pageNumber==2) {  // better use else if here 
        //background(bg);
      }
    }
    
    void keyPressed() {
      if (key =='1') {
        pageNumber = 1;
        println(pageNumber);
      }
      if (key =='2') {
        pageNumber = 2;
        println(pageNumber);
      }
    }
    
  • Thankyou Chrisir, the score is now showing at all times, but (1) still has some problems as the score keeps rising on its own even when the button isn't being pressed.

  • edited January 2018 Answer ✓

    For (1): This line: score++; belongs into the if-clause and not outside of it then.

  • Thankyou Chrisir! This worked for the first part so that section is now up and running. (1) is now the first page, now I've added a second page where I want to bring in the timer for the loop to make it more difficult (next level etc), however I'm struggling to understand which parts of (2) I need to add into the second page to make it work? Apologies again for any misunderstanding.

  • edited January 2018

    The variable timerIsOn is dictating this behaviour of making the ball wait before it re-appears.

    When we wait for the ball to appear timerIsOn is true: which is written as if (timerIsOn) { which means the same as if (timerIsOn==true) {.

    Now, let's see, we have two situations:

    • timerIsOn is true (we wait and show no ball) and
    • timerIsOn is false, we run and show the ball.

    When does timerIsOn change?

    • As soon as the ball leaves the screen, we set timerIsOn to true, saying the program: wait and don't show the ball. We do some preparations here, e.g. set the variables wait and timer and we say circX=0;.

    • When the random time stored in wait has passed, we start the the next ball by saying timerIsOn=false;.

    Because of the imporance of timerIsOn the code below falls in two parts:

        if (timerIsOn) {
    
           // wait for the ball 
    
        } else { 
    
          // ball runs !!! timerIsOn==false
    
        }
    

    You need :

        if (timerIsOn) {
    
          // wait for the ball 
    
          // display the scene (red circle etc.)
          fill(255);
          textSize(45);
          text (score, 267, 146);
    
          fill (194, 49, 49);
          ellipse (301, 300, 255, 255);
    
          if (millis()-timer>wait) {   // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            timerIsOn=false;
          }
    
        } else {
          // ball runs !!! timerIsOn==false
    
          // normal stuff: display ball etc. 
    
          if (circX > 500) {  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            circX=0;
            timerIsOn=true;  // !!!!!!!!!!!!!!!!!!!!
            timer=millis(); 
            wait=int(random(90, 4900)); // these are millis
          }
        }//else timer
    

    you also need some vars:

    boolean timerIsOn=false;
    int timer; 
    int wait;
    
  • Ok thank you I really appreciate the detail. Is the code from 'you need:' ^ the only code I need within page 2?

  • edited January 2018

    you wrote:

    Is the code from 'you need:' ^ the only code I need within page 2?

    ??

    No, not at all. Above is a full sketch that shows the entire thing (from January 4th).

    the code from 'you need' is only the core idea regarding the timer. I shortened it to make it easier for you to read. Especially I deleted the section with normal stuff: display ball etc.

    You really need to read and learn my code please. Not only copy it....

Sign In or Register to comment.