1. Make an ellipse in p5 move on the horizontal axis using only one sensor. Video Link
int leftLedPin = 4; int rightLedPin = 6; const int trigPin = 10; const int echoPin = 9; void setup() { // Start serial communication at 9600 baud Serial.begin(9600); // Set up the LED and ultrasonic sensor pins pinMode(LED_BUILTIN, OUTPUT); pinMode(leftLedPin, OUTPUT); pinMode(rightLedPin, OUTPUT); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); // Blink LEDs to check wiring digitalWrite(leftLedPin, HIGH); digitalWrite(rightLedPin, HIGH); delay(200); digitalWrite(leftLedPin, LOW); digitalWrite(rightLedPin, LOW); } void loop() { // Check if there is data available from p5.js if (Serial.available()) { int left = Serial.parseInt(); // Get the left LED value int right = Serial.parseInt(); // Get the right LED value // Update LEDs based on values from p5.js digitalWrite(leftLedPin, left); digitalWrite(rightLedPin, right); // Clear any remaining characters in the serial buffer (such as '\n') Serial.read(); } // Trigger the ultrasonic sensor digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // determine the time for the echo long timeTraveled = pulseIn(echoPin, HIGH); // the distance in centimeters int distance = timeTraveled * 3.4/2; int sensor = analogRead(A1); // Send the distance value to p5.js Serial.print(sensor); Serial.print(","); Serial.println(distance); delay(100); }
2. Control the LED brightness from p5 Video Link
const int blue_LED = 4; const int red_LED = 6; void setup() { // put your setup code here, to run once: Serial.begin(9600); // Initalization of LED pins pinMode(blue_LED, OUTPUT); pinMode(red_LED, OUTPUT); // Blink LEDs to check wiring digitalWrite(blue_LED, HIGH); digitalWrite(red_LED, HIGH); delay(200); digitalWrite(blue_LED, LOW); digitalWrite(red_LED, LOW); } void loop() { // put your main code here, to run repeatedly: Serial.println(0); if (Serial.available()) { int left = Serial.parseInt(); // Get the left LED value int right = Serial.parseInt(); // Get the right LED value // Update LEDs based on values from p5.js digitalWrite(blue_LED, left); digitalWrite(red_LED, right); // removes remaining characters in the serial buffer } Serial.read(); delay(100); }
3. Gravity and Wind Video Link
const int LED_pin = 6; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode (LED_pin, OUTPUT); // digitalWrite(LED_pin, HIGH); // delay(200); // digitalWrite(LED_pin, LOW); } void loop() { // put your main code here, to run repeatedly: if (Serial.available()) { int LED_value = Serial.parseInt(); // Get the LED value // Update LEDs based on values from p5.js digitalWrite(LED_pin, LED_value); // Clear any remaining characters in the serial buffer (such as '\n') Serial.read(); } int sensor = analogRead(A0); Serial.println(sensor); }