Serial Communication Assignments

Task 1:

  1. make something that uses only one sensor  on arduino and makes the ellipse in p5 move on the horizontal axis, in the middle of the screen, and nothing on arduino is controlled by p5

    To do this assignment, I used a potentiometer that is connected to pin A0, and was reading from the potentiometer and using the result to control the xPosition of the ellipse. I mapped the values from 0, 1023 to become from 0,width of the p5 screen to have good results. Here is the code that I used for the assignment

    let ellipseXPos=100;
    
    function setup() {
      createCanvas(640, 480);
      textSize(18);
    }
    
    function draw() {
      // one value from Arduino controls the background's red color
      background(104, 255, 255);
      map(ellipseXPos,0,1023,0,width);
      ellipse(ellipseXPos,height/2,100,50);
      
      
    
      // the other value controls the text's transparency value
     
    
      if (!serialActive) {
        text("Press Space Bar to select Serial Port", 20, 30);
      } else {
        text("Connected", 20, 30);
      }
    
      // click on one side of the screen, one LED will light up
      // click on the other side, the other LED will light up
      
    }
    
    function keyPressed() {
      if (key == " ") {
        // important to have in order to start the serial connection!!
        setUpSerial();
      }
    }
    
    function readSerial(data) {
      ////////////////////////////////////
      //READ FROM ARDUINO HERE
      ////////////////////////////////////
    
      if (data != null) {
        ellipseXPos=data;
        }
    
      
    }
    
    //Arudino Code
    /*
    void setup() {
      Serial.begin(9600);
    }
    
    void loop() {
      // wait for data from p5 before doing something
     
        
      
          int sensor = analogRead(A0);
          delay(1);
          Serial.println(sensor);
        
      
    }
    */
    

    Task 2

  2. make something that controls the LED brightness from p5
    To do this task I decided to use the up and down arrows to control the brightness of the LED from p5. However, I spent so much time doing this because I was not able to setup the communication between arduino and p5 correctly. However, I was inspired by how other people did it and finally figured out how to do it. Here is part of the code

    function keyPressed() {
      if (key == " ") {
        // important to have in order to start the serial connection!!
        setUpSerial();
      }
      if(keyCode==UP_ARROW){
        val+=50;
      }
      if(keyCode==DOWN_ARROW){
        val-=50;
      }
    }
    function readSerial(data) {
      ////////////////////////////////////
      //READ FROM ARDUINO HERE
      ////////////////////////////////////
      
      if (data != null) {
        // if there is a message from Arduino, continue
        //////////////////////////////////
        //SEND TO ARDUINO HERE (handshake)
        //////////////////////////////////
        let sendToArduino = brightness + "\n";
        writeSerial(sendToArduino);
      }
    }
    
    // int LED = 3;
    // void setup() {
    //   Serial.begin(9600);
    //   pinMode(LED, OUTPUT);
    //   // start the handshake
    //   while (Serial.available() <= 0) {
    //     Serial.println("Wait");  // send a starting message
    //     delay(300);               // wait 1/3 second
    //   }
    // }
    // void loop() {
    //   // wait for data from p5 before doing something
    //   while (Serial.available()) {
    //     int brightness = Serial.parseInt(); 
    //     if (Serial.read() == '\n') {
    //       analogWrite(LED, brightness); // turn on LED and adjusts brightness
    //       Serial.println("Light is ON"); 
    //     }
    //   }
    // }
    

     

Task 3:

For this task I used the gravity wind example to come up with a code that lights up the LED when the ball is bouncing, also I used a potentiometer to control the wind speed as seen in this part of the code.

if (position.y > height-mass/2) {
      velocity.y *= -0.9;  // A little dampening when hitting the bottom
      position.y = height-mass/2;
      LEDbrightness=1;
      
    
    }
  else{
  LEDbrightness=0;
  }
  if(windSpeed<500){
  wind.x=-1;
  }
  else if(windSpeed>700){
    wind.x=1;
  }
  else{
    wind.x=0;
  }

Here is a video that shows how the code above works:

Leave a Reply