Week 11: Class Activities

Task 1: Sensor Controlled Ellipse

I used a potentiometer to control the horizontal movement of an ellipse in p5.js. Additionally, I displayed text showing the connection status and the current position of the ellipse in (x, y) format.

My Sketch (with the P5js Code) can be found here:

My Arduino Code is here:

// Pin for reading analog values from sensor
int Pin=A0;
void setup() 
{
 Serial.begin(9600);
}
void loop() 
{ 
//Reading values from sensor
   int sensor = analogRead(Pin);
   delay(5);
//Writting the sensor value through serial
   Serial.println(sensor);
}
Task 2: LED Brightness control from P5

I created a simple custom interface with On and Off Button to turn on the LED. While the LED is On there is a slider that can be adjusted to increase or decrease the brightness of the LED.

My Sketch (with the P5js Code) can be found here:

My Arduino Code is here:

 // Setting Global Variables
const int ledPin = 9; 
int brightness = 0;

void setup()
{
  pinMode(ledPin, OUTPUT);  
 // Start serial communication
  Serial.begin(9600);      
}

void loop()
{
// Check if data is available to read
  if (Serial.available())  
  {
// Read Data as Integer 
    int value = Serial.parseInt(); 
// Clear the serial data 
    Serial.flush();
    if (value > 0) 
    {
      brightness = value;
      Serial.println(brightness);
  // Analog write the brightness   
      analogWrite(ledPin, brightness);  
    }
  }else
  {
    analogWrite(ledPin, 0);  
  }
}

 

Leave a Reply