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);  
  }
}
Task 3: Bouncing Ball and Winds

In this activity, I proceeded from the given Sketch. I added LED lighting whenever  the balls touch the ground. The LEDs are controlled digitally by the balls as they touch ground. Additionally, I also added two Potentimeters that act as winds controlling the horizontal movement of the ball. Whenever the Right winds are stronger than the Left winds, the balls move towards the right side and vice versa. I also added an arrow to indicate winds direction.

My Schematics can be seen here:

Here is the Video Demonstration:

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

My Arduino Code is here:

 // Setting Global Variables
const int ledPin1 = 9; 
const int ledPin2 = 6; 
int WindLeftPin=A0;
int WindRightPin=A1;
int brightness = 0;

void setup()
{

  pinMode(ledPin1, OUTPUT);  
  pinMode(ledPin2, OUTPUT);  
 // Start serial communication
  Serial.begin(9600);     
}

void loop()

{if (Serial.available())  
{
  int WindLeft = analogRead(WindLeftPin);
  int WindRight = analogRead(WindRightPin);
  Serial.print(WindLeft);
  Serial.print(",");
  Serial.print(WindRight);
  Serial.println();
  int value = Serial.parseInt();
  if (value == 0) 
  {
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
  }
  else if (value == 1)
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
  } 
}
}

 

 

Leave a Reply