serial communication exercises 

exercise 1

This exercise was similar to what we did in class, except instead of controlling the height of the ball with a sensor, it was static, and set for the ball to be in the middle of the screen.

p5js

Arduino
void setup() {
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
  while (Serial.available() <= 0) {
    Serial.println("0,0"); // send a starting message
    delay(300);              // wait 1/3 second
  }
}

void loop() {
  while (Serial.available() > 0) {
    // read the incoming byte:
   int inByte = Serial.read();
    switch (inByte) {
      case 0:
        digitalWrite(2, LOW);
        digitalWrite(5, LOW);
        break;
      case 1:
        digitalWrite(2, HIGH);
        break;
      case 2:
        digitalWrite(5, HIGH);
        break;
    }

    int sensorValue = analogRead(A0);
    Serial.print(sensorValue);
    Serial.print(",");
    sensorValue = analogRead(A1);
    Serial.print(sensorValue);
    Serial.println();
  }
}

exercise 2

In the second exercise, we controlled the brightness of the LED with the x position of the mouse, by moving the mouse to the right the light got brighter, and moving it toward the left the light gets dimmer until it turns off. For this, we mapped the x position values into 255 to control the brightness of the LED.

p5js

Arduino
[code]
void setup() {
  Serial.begin(9600);
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
  while (Serial.available() <= 0) {
    Serial.println("0,0"); // send a starting message
    delay(300);              // wait 1/3 second
  }
}
void loop() {
  while (Serial.available() > 0) {
    // read the incoming byte:
   int inByte = Serial.read();
    switch (inByte) {
      case 0:
        digitalWrite(2, LOW);
        digitalWrite(5, LOW);
        break;
      case 1:
        digitalWrite(2, HIGH);
        break;
      case 2:
        digitalWrite(5, HIGH);
        break;
    }
    int sensorValue = analogRead(A0);
    Serial.print(sensorValue);
    Serial.print(",");
    sensorValue = analogRead(A1);
    Serial.print(sensorValue);
    Serial.println();
  }
}
[/code]

exercise 3 

This final exercise was a bit more complicated, we had to not only control the light with the ball bouncing, but the velocity of the ball using the potentiometer instead of the left and right keys. I think for me it was important to understand that the potentiometer was not controlling the wind, but was the wind. At first, when trying to find a solution I wanted to map the values within the value of the wind when really it needed to be mapped within the x position values so that the sensor would control the X position of the ball.

p5js

Arduino
void setup() {
  Serial.begin(9600);
  pinMode(5, OUTPUT);
  while (Serial.available() <= 0) {
    Serial.println("0,0"); // send a starting message
    delay(300);              // wait 1/3 second
  }
}

void loop() {
  while (Serial.available() > 0) {
    
   int inByte = Serial.read(); // takes data from p5JS about ball position
   
   int windValue = analogRead(A0);//Wind value from potentiometer
   
   analogWrite(5, inByte);//swtiches on LED when ball bounces

    
    Serial.print(windValue);
    Serial.println();
  }
}
Video

Leave a Reply