Process:
For this assignment, we had to finish the three in-class examples that connect p5js and Arduino. In the beginning, I thought I understood the code and the connection but then I realized that I did not fully understand it. So I decided to review it line by line and try to understand the communication between Arduino and P5js.
Then Yupu and I decided to work together on these three examples we divided them so that he worked on the first one I worked on the second and we both worked on the third together. We helped each other debug the code and figure out how to implement it together. Honestly, this process of debugging helped me understand the communication between the two (P5js and Arduino) better.
Example 1: Circle Moving
// Week 11.2 Serial port - Light sensor, ball and light example 1 int leftLedPin = 7; int rightLedPin = 4; void setup() { // Start serial communication so we can send data // over the USB connection to our p5js sketch Serial.begin(9600); // We'll use the builtin LED as a status output. // We can't use the serial monitor since the serial connection is // used to communicate to p5js and only one application on the computer // can use a serial port at once. pinMode(LED_BUILTIN, OUTPUT); // Outputs on these pins pinMode(leftLedPin, OUTPUT); pinMode(rightLedPin, OUTPUT); // Blink them so we can check the wiring digitalWrite(leftLedPin, HIGH); digitalWrite(rightLedPin, HIGH); delay(200); digitalWrite(leftLedPin, LOW); digitalWrite(rightLedPin, LOW); // start the handshake while (Serial.available() <= 0) { digitalWrite(LED_BUILTIN, HIGH); // on/blink while waiting for serial data Serial.println("0,0"); // send a starting message delay(300); // wait 1/3 second digitalWrite(LED_BUILTIN, LOW); delay(50); } } void loop() { // wait for data from p5 before doing something while (Serial.available()) { digitalWrite(LED_BUILTIN, HIGH); // led on while receiving data if (Serial.read() == '\n') { int sensor = analogRead(A1); //potentiometer Serial.println(sensor); } } digitalWrite(LED_BUILTIN, LOW); }
Example 2: LED Brightness
// Week 11.2 Example LED brightness int leftLedPin = 6; void setup() { // Start serial communication so we can send data // over the USB connection to our p5js sketch Serial.begin(9600); // We'll use the builtin LED as a status output. // We can't use the serial monitor since the serial connection is // used to communicate to p5js and only one application on the computer // can use a serial port at once. pinMode(LED_BUILTIN, OUTPUT); // Outputs on these pins pinMode(leftLedPin, OUTPUT); // start the handshake while (Serial.available() <= 0) { digitalWrite(LED_BUILTIN, HIGH); // on/blink while waiting for serial data Serial.println("0,0"); // send a starting message delay(200); // wait 1/3 second digitalWrite(LED_BUILTIN, LOW); delay(200); } } void loop() { // wait for data from p5 before doing something while (Serial.available()) { digitalWrite(LED_BUILTIN, HIGH); // led on while receiving data int brightness = Serial.parseInt(); analogWrite(leftLedPin, brightness); if (Serial.read() == '\n') { Serial.println("0,0"); } } digitalWrite(LED_BUILTIN, LOW); }
Example 3: Gravity, Ball, and LED
// week 11 example 3 sensor analg and digital light and abouncing ball int leftLedPin = 7; int rightLedPin = 4; void setup() { // Start serial communication so we can send data // over the USB connection to our p5js sketch Serial.begin(9600); // We'll use the builtin LED as a status output. // We can't use the serial monitor since the serial connection is // used to communicate to p5js and only one application on the computer // can use a serial port at once. pinMode(LED_BUILTIN, OUTPUT); // Outputs on these pins pinMode(leftLedPin, OUTPUT); pinMode(rightLedPin, OUTPUT); // Blink them so we can check the wiring digitalWrite(leftLedPin, HIGH); digitalWrite(rightLedPin, HIGH); delay(200); digitalWrite(leftLedPin, LOW); digitalWrite(rightLedPin, LOW); // start the handshake while (Serial.available() <= 0) { digitalWrite(LED_BUILTIN, HIGH); // on/blink while waiting for serial data Serial.println("0,0"); // send a starting message delay(300); // wait 1/3 second digitalWrite(LED_BUILTIN, LOW); delay(50); } } void loop() { // wait for data from p5 before doing something while (Serial.available()) { digitalWrite(LED_BUILTIN, HIGH); // led on while receiving data int LED_STATE = Serial.parseInt(); //int right = Serial.parseInt(); if (Serial.read() == '\n') { digitalWrite(rightLedPin, LED_STATE); int sensor2 = analogRead(A1); delay(5); Serial.print(sensor2); Serial.print(','); Serial.println('Y'); } } digitalWrite(LED_BUILTIN, LOW); }