Loop & bg problem

Hello!!!
I have this code:

import ddf.minim.*; 
import ddf.minim.analysis.*;  
Minim minim;   
FFT fft;  
AudioInput in;   
float amp = 20; // used to make signal stronger/weaker 
float ampWave = 20*amp; 
float avgAudio; // store avg volume globally 

float bass; 
float high; 

void setup() {
  background(255);
  size(500, 500);
  noStroke();
  smooth();

  colorMode(HSB, 360, 100, 100);


  minim = new Minim(this); // initalize in setup 
  in = minim.getLineIn(Minim.STEREO, 512); // audio in + bufferSize 512 or 1024 
  fft = new FFT(in.bufferSize(), in.sampleRate());  
  fft.logAverages(22, 3); // 3 = 30, 4 = 40, slices of frequency
}

void draw() {

  if (keyPressed) {
    fft.forward(in.mix); // IMPORTANT! -update for FFT anaylsis
    bass = fft.calcAvg(20, 300)*amp; // fft.calcAvg(minFreq, maxFreq)  
    high = fft.calcAvg(300, 20000)*amp;
  }

  if (bass > high) {
    background(255);
    noStroke();
    fill(random(180,360), 100, 100);

    int n_vertex = int(random(7, 20));

    beginShape();
    for (int i = 0; i < n_vertex; i++) {
    vertex(random(width), random(height));
    }
    
    endShape(CLOSE);
  

} else if (high > bass) {
    background(255);
    noStroke();
    fill(random(1,180), 100, 100);

    int n_vertex = int(random(3, 7));

    beginShape();
    for (int i = 0; i < n_vertex; i++) {
    vertex(random(width), random(height));
    }
    
    endShape(CLOSE);
  }


  println(bass+" / "+high);
}

and the problem basically is that after I stop to press the keyboard it continues to draw shapes and I cannot “freeze” it…also the background is not still (255) but it become grey.
Any idea of this two problems?

thanks

Could you please post the entire code (setup function and import) and format it with </> in the message editor like this :

println("Hello World!");

Thanks

Hello there!
Is there any way you can post your full code? It’s difficult to figure out the initial setup without knowing the rest of the program. If for some reason you don’t want to post your full code (it is highly recommended that you do), I would say look at the standalone methods keyPressed(){} and keyReleased{}. Also, if you want the background to be white regardless, try putting the background(255) just after the “void draw(){” and not in any parenthetical statements. This will set the background to 255 every time the draw loop iterates, as the first thing in the loop.

EDIT: You posted the full code! Sorry.

done! sorry I didn’t notice that.

I tried but it doesn’t work anyway

Rioma,
The background turning gray has to do with the fact that you are in Color Mode HSB, not RGB. HSB stands for Hue, Saturation, and Brightness, and so the way to get white would be background(0, 0, 100). RGB stands for Red, Green Blue, so you get colors by combining those. Try background(0, 0, 100) if you want to stay in HSB.

that’s true…so stupid -.-"

and for the problem that it continues to draw random polygons? any idea? thanks in advance

About that. What exactly is it that you want the program to do? Do you want to draw shapes while a key is being pressed, and then when the user stops pressing that key, do you want the screen to go blank, or do you want to display the last shape that was displayed before the keys stopped being pressed?

If you want the screen to just go blank, I recommend putting the polygon drawing sections in the same loop as the keyPressed statement.

I would like to display last drawn shape

Alright, why don’t you try using an array of PVectors instead of vertices? Both vertex() and PVector() hold two values, one for x, one for y. Try creating a PVector[] vertices = new PVector[0]; (before the setup(){} )then use

 int n_vertex = int(random(7, 20));
vertices = new PVector[n_vertex];
    beginShape();
    for (int i = 0; i < n_vertex; i++) {
    vertices[i] = new PVector(random(width), random(height));
    }

instead of

int n_vertex = int(random(7, 20));

    beginShape();
    for (int i = 0; i < n_vertex; i++) {
    vertex(random(width), random(height));
    }
    
    endShape(CLOSE);

and for the other condition (high> bass) as well.
Then, right before the end of the draw loop, use

if(vertices.length > 0){
  beginShape();
  for(int i = 0; i< vertices.length; i++){
   vertex(vertices[i].x, vertices[i].y); 
  }
  endShape(CLOSE);
}

This should work, because every time a new shape is created, its vertices are stored in an array, in the form of PVectors. This way, you can draw it anytime. Hope this helps.

import ddf.minim.*; 
import ddf.minim.analysis.*;  
Minim minim;   
FFT fft;  
AudioInput in;   
float amp = 20; // used to make signal stronger/weaker 
float ampWave = 20*amp; 
float avgAudio; // store avg volume globally 

float bass = 1; 
float high = 2; 
PVector[] vertices = new PVector[0]; 

void setup() {
  background(255);
  size(500, 500);
  noStroke();
  smooth();

  colorMode(HSB, 360, 100, 100);


  //  minim = new Minim(this); // initalize in setup 
  //  in = minim.getLineIn(Minim.STEREO, 512); // audio in + bufferSize 512 or 1024 
  //  fft = new FFT(in.bufferSize(), in.sampleRate());  
  //  fft.logAverages(22, 3); // 3 = 30, 4 = 40, slices of frequency
}

void draw() {
  //colorMode(RGB);
  background(0, 0, 100);
  //if (keyPressed) {
  //  fft.forward(in.mix); // IMPORTANT! -update for FFT anaylsis
  //  bass = fft.calcAvg(20, 300)*amp; // fft.calcAvg(minFreq, maxFreq)  
  //  high = fft.calcAvg(300, 20000)*amp;
  //}
  if (keyPressed) {
    if (bass > high) {
      // background(255);

      noStroke();
      fill(random(180, 360), 100, 100);

      int n_vertex = int(random(7, 20));
      vertices = new PVector[n_vertex];
      beginShape();
      for (int i = 0; i < n_vertex; i++) {
        vertices[i] = new PVector(random(width), random(height));
      }

      endShape(CLOSE);
    } else if (high > bass) {

      // background(255);
      noStroke();
      fill(random(1, 180), 100, 100);

      int n_vertex = int(random(3, 7));
      vertices = new PVector[n_vertex];

      for (int i = 0; i < n_vertex; i++) {
        vertices[i] = new PVector(random(width), random(height));
      }
    }


    println(bass+" / "+high);
  }

  if (vertices.length > 0) {
    beginShape();
    for (int i = 0; i< vertices.length; i++) {
      vertex(vertices[i].x, vertices[i].y);
    }
    endShape(CLOSE);
  }
}

is not exactly what I need…is perfect that it “freeze” the shape but I cannot recognise anymore the shapes