Toggle Functions

Hi! First of all, I'm a newbie and this is the first time I'm writing a Processing sketch seriously after a lot of following examples from books. Here's where I'm stuck.

I'm trying to build a small application where patterns are generated with minim and audio input. I've defined all the functions etc and I was trying to toggle them with keyPressed and then I realized that I'll need to keep holding down the key for the pattern to keep looping. How do I solve this where once i press 1 or 2 it switches to that function and loops continuously? Below is what I've written.

//PATTERN SELECTION

if (keyPressed) { if (key == '1') { pattern01(); } else if (key == '2') { pattern02(); } else if (key == '3') { pattern03(); } }

Answers

  • Answer ✓

    Rather than using key pressed inside the draw() method you are better using the keyPressed or keyReleased method. Personally I prefer the latter. in this method use a variable to remember the key e.g.

    char keychar = '0'; // Initialise to an unused value
    
    void keyPressed() {
      keychar = key;
    }
    

    then in draw use a switch statement to decide which method to call e.g.

    switch(keychar){
    case '1':
      pattern01()
      break;
    case '2':
      pattern02()
      break;
    case '3':
      pattern03()
      break;
    }
    
  • Oh wow! Thanks, it works (even though I don't exactly know how)!

    I kind of get what you're doing but the switch function is something I've not used before, will look it up.

    Thanks again!

    :D

  • The switch statement allows you to choose from multiple options whereas the if statement only gives you a choice of 2, although you can chain them together as you did.

    The switch statement looks for the first case statement that matches the value stored in keychar and then starts executing the program from that point. When a break statement is found the program jumps to the next statement after the switch in this case line 12.

  • I've run into a new problem now though, I had a few other keyPressed options enabled, like using the arrow keys for changing the value of one integer and w and b to change the background colour to white and black - they don't seem to be working properly anymore - I'm guessing it's coz the key is being stored inside keychar instead of functioning how it should.

  • Can I get keyPressed to ignore a few keys?

  • Oh wait, I could do that with a if and || functions I'm guessing. If (key=='w') || (key == 'b'){keychar='0'} else keychar = key

    let me see if that works

  • Try changing the keyPressed() in my previous post to method to keyReleased()

  • edited July 2015

    BTW keychar = key; does not change the value in key it simply makes a copy.

  • Nope, not helping.

    Here's the full sketch, tell me what's going on? The arrow keys are freezing the output.

    import ddf.minim.spi.*;
    import ddf.minim.signals.*;
    import ddf.minim.*;
    import ddf.minim.analysis.*;
    import ddf.minim.ugens.*;
    import ddf.minim.effects.*;
    
    Minim minim;
    AudioInput in;
    float x = 50.0;
    char keychar = '0';
    
    void keyPressed() {
      keychar = key;
    }
    
    void setup() {
      size(640, 480);
      minim = new Minim(this);
      in = minim.getLineIn();
      frameRate(25);
      smooth();
      background(0); //Background Colour
    }
    
    void draw() {
    
      //INTENSITY OF X  
    
      if (keyPressed && (key == CODED)) {
        if (keyCode == UP) {
          x++;
        } else if (keyCode == DOWN) {
          x--;
        }
      }
      if (x<0) {
        x=0;
      }
    
      //PATTERN SELECTION
    
      switch(keychar) {
      case '1':
        pattern01();
        break;
      case '2':
        pattern02();
        break;
      case '3':
        pattern03();
        break;
      }
    
      //FOLLOWING FOR STROBE
    
      if (keyPressed) {
        if ((key == 'b') || (key=='B')) {
          background(0);
        }
        if ((key == 'w') || (key == 'W')) {
          rectMode(RADIUS);
          fill(255);
          rect(width/2, height/2, width, height);
        }
      }
    }
    
    
    
    //ALL PATTERNS GO BELOW
    
    void pattern01() {
      for (int i = 0; i < in.bufferSize () - 1; i++) {
       // background(0);
        rectMode(RADIUS);
        rect(width/2, height/2, in.left.get(i)*x, in.right.get(i)*x);
      }
    }
    
    void pattern02() {
      for (int i = 0; i < in.bufferSize () - 1; i++) {
       // background(0);
        ellipseMode(RADIUS);
        ellipse(width/2, height/2, in.left.get(i)*x, in.right.get(i)*x);
      }
    }
    
    
    void pattern03() {
      for (int i = 0; i < in.bufferSize () - 1; i++) {
        rectMode(RADIUS);
        fill(0);
        rect(width/2, height/2, x, x);
        translate(in.left.get(i)*x, in.right.get(i)*x);
        fill(255);
        rect(width/2, height/2, x, x);
        translate(in.left.get(i)*x, -in.right.get(i)*x);
      }
    }  
    
  • And the w & b keys are supposed to just change the background but it's overriding the pattern and filling up the whole screen.

  • edited July 2015

    If I just leave pattern01(); or pattern02(); instead of the whole switch function, the rest seems to work perfectly.

  • Answer ✓

    Difficult because I am not sutre what you hope to achieve try changing lines 13-15 to

    void keyPressed() {
      if (key >= '1' && key <= '3')
        keychar = key;
    }
    
  • yes! That fixes it!

    Eternally grateful!

Sign In or Register to comment.