[Serial Communication Excercise] Cole and Minh

Exercise 1:

Excercise 2

Exercise 3:

const int ledPin = 11;
int brightness = 0;
const int sensorPin = A0;
 
void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(ledPin, OUTPUT);
}
 
void loop() {
  while (Serial.available()) {
    int onOff = Serial.parseInt();
    if (Serial.read() == '\n') {
      digitalWrite(ledPin, onOff);
      int sensor = analogRead(sensorPin);
      delay(1);
      Serial.println(sensor);
    }
  }
}


//PROCESSING CODE
  velocity.mult(drag);
  position.add(velocity);
  acceleration.mult(0);
  ellipse(position.x,position.y,mass,mass);
  fill(255,255,0);
  if (position.y > height-mass/2) {
      velocity.y *= -0.9;  // A little dampening when hitting the bottom
      position.y = height-mass/2;
      onOff = true;
  }
  else{
    onOff = false;
  }
}
 
void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  int val = parseInt(s);
  if (s!=null){
      wind.x=map(val,0,1023,-2, 2);  //2 instead of 1 to make it stronger
  }
  myPort.write(int(onOff)+"\n");
}
  
void applyForce(PVector force){
  // Newton's 2nd law: F = M * A
  // or A = F / M
  PVector f = PVector.div(force, mass);
  acceleration.add(f);
}

void keyPressed(){
  if (keyCode==LEFT){
    wind.x=-1;
  }
  if (keyCode==RIGHT){
    wind.x=1;
  }
  if (key==' '){
    mass=random(15,80);
    position.y=-mass;
    velocity.mult(0);
  }
}

Video for exercise 3:

 

Leave a Reply