trouble speed value

edited January 11 in Arduino

Hi, we are having some question about our code. We hooked up a bicycle to an arduino, the sensor mesures the turning of the wheel and the faster you cycle the faster our svg will appear. But, the svg will appear regardsless of speed and does not stop when the wheel stops either. Can someone explain how these values work? How to ad more speeds and how to make it stop when the wheel stops?

RShape shape; int idx = 0; import geomerative.*; import processing.serial.*; int childCount;

Serial myPort; // Create object from Serial class String myString; // Data received from the serial port int val; // Data received from the serial port

void setup() { size(1000, 500); RG.init(this); background(0); printArray(Serial.list()); String portName = Serial.list()[3]; //change the 0 to a 1 or 2 etc. to match your port myPort = new Serial(this, portName, 9600);

shape = RG.loadShape("fhlaskdjhfasd.svg"); childCount = shape.countChildren();

for (int i = 0; i < childCount; i++) { } RG.ignoreStyles(); fill(255); //frameRate(1); stroke(0); noStroke(); }

int speed = 20;

void draw() { scale(0.5); shape.children[idx].draw();

if (frameCount % speed == 0) { if (idx < childCount -1) { idx++; } else { background(0); idx= 0; } }

while (myPort.available() > 0) { myString = myPort.readStringUntil('\n'); if (myString != null) { try { val=Integer.parseInt(myString.trim()); //println(myString); //println(val);

     if (val > 1 && val < 20 ) {
             speed --;
     }
 if (val > 70) {
  speed = 5;
}  else if ( val < 60) {
  speed ++;
}
    println(speed);

  } catch (NumberFormatException npe){
   // Not an integer so forget it
  }
}

}

//println(val); //print it out in the console }

void keyPressed() { if (keyCode == DOWN) { speed ++; } else if (keyCode == UP) { if (speed > 1) { speed --; } } println(speed); }

Thank you so much code wizards

Answers

  • In line 4 above val > 70 : better start the line with else , because otherwise line 7 with speed ++ gets executed too often (following your line numbers)

    Also you could set draw to 0 at beginning of draw and see what happens

Sign In or Register to comment.