moving an ellipse in steps.

edited March 2017 in Arduino

I'm trying to write a code which moves an ellipse when it reads a particular serial input. But what happens is when it receives the particular value it moves all the way to the end of the screen. Looking at the code I can work out yes thats what will happen. But previously I was able to do the same thing with similar code and it worked out perfectly. I tried copying and pasting that codes into it but still it doesn't move in steps.

Here is the code I'm working on import processing.serial.*; Serial myPort; // Create object from Serial class int val;

int Xpos = 200;
int Ypos = 200;


void setup() {
  size(400, 400);
  noStroke();
  String portName = Serial.list()[2]; 
  myPort = new Serial(this, portName, 9600);
}
void draw() {

  move();


  background(123);
  ellipse(Xpos, Ypos, 80, 80);
}




void serialEvent(Serial myPort) {
  val = myPort.read();
  println(val);
}

void move(){

  if (val == 1) {
    if (Ypos>0) {
      Ypos = Ypos - 2;
    }
  }
  if (val == 2) {
    if (Ypos<height) {
      Ypos = Ypos + 2;
    }
  }
  if (val == 3) {
    if (Xpos>0) {
      Xpos = Xpos -2;
    }
  }
  if (val == 4) {
    if (Xpos< width) {
      Xpos = Xpos + 2;
    }
  }


}
Tagged:

Answers

  • it doesn't move in steps.

    probably it is moving in steps but they could be happening really fast. To experiment, try adding frameRate(1); inside your setup() function. If you see steps every seconds, then this implies that you have a high input rate.

    An alternative, call move like this: (if(frameCount%30==0)) move(); This will call move every second instead of 30 times per second.

    Kf

Sign In or Register to comment.