LED Assignment – Hamad and Varya

Objective:

Arduino and Processing have numerous capabilities allowing them to connect to one another. Thus, this assignment presents these factors with three tasks my partner, Hamad, and I were instructed to follow.

Plan:

Go over what we touched upon in class and reference from numerous videos around the internet to explore the multiple ways possible to get to the goal desired.

Code:

Task 1:

ARDUINO & PROCESSING:

Task 2:

ARDUINO:

//INITIALIZE VARIABLE
float var;

//ARDUINO AND PROCESSING COMMUNICATION SETUP
void setup()
{
  Serial.begin(9600);
  //GET ONLY 1 INPUT - BRIGHTNESS
  Serial.println("0");
  pinMode(2, OUTPUT);
}

//MAIN FUNCTION
void loop()
{
  while (Serial.available())
  {
    //GET PROCESSING VARIABLE
    var = Serial.parseInt();
    if (Serial.read() == '\n') 
    {
      //GIVE LED BRIGHTNESS
      analogWrite(2, var); 
      Serial.println(var);
    }
  }
}

PROCESSING:

//LINK TO ARDUINO
import processing.serial.*;
Serial myPort;

//INITIALIZE VARIABLES
int xPos = 0;
float var;


void setup() 
{
  size(960, 720);
  printArray(Serial.list());
  String portname=Serial.list()[0];
  println(portname);
  myPort = new Serial(this, portname, 9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}

void draw() 
{
  background(255);
  ellipse(xPos, height/2, 30, 30);
  //GET X POSITION
  float var1 = mouseX; 
  //MAP WIDTH TO BRIGHTNESS
  var = map(var1, 0, width, 0, 255);
}

void serialEvent(Serial myPort) 
{ 
  String s=myPort.readStringUntil('\n');
  s = trim(s);
  if (s != null) 
  {
    println(s);
    int value = int(s);
    if (value >= 0) {
      xPos = (int)map(value, 0, 1023, 0, width);
    }
  }
  //SEND BACK MAPPED X VALUE TO ARDUINO
  myPort.write(var+ "\n");
}

 

Task 3:

ARDUINO:

//INITIALIZE VARIABLES
int left = 0;
int right = 0;
int var = 0;

//VARIBALES & LED SETUP
void setup()
{
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(2, OUTPUT);
}

void loop()
{
  while (Serial.available())
  {
    //STORES LED VALUE FROM PROCESSING
    var = Serial.parseInt();
    Serial.println(var);

    if (Serial.read() == '\n')
    {
      //TURN LED ON IF BALL TOUCHES GROUND
      if (var == 1)
      {
        digitalWrite(2, HIGH);
      }
    }

    //GET VALUES FROM POTENTIOMETER
    int sensor = analogRead(A0);
    delay(1);

    Serial.println(sensor);
  }
}

PROCESSING:

//LINK PROCESSING TO ARDUINO
import processing.serial.*;
Serial myPort;

//INITIALIZE VARIABLES
PVector velocity;
PVector gravity;
PVector position;
PVector acceleration;
PVector wind;
float drag = 0.99;
float mass = 50;
float hDampening;
int potValue; //variable from the potentiometer
int led = 0; //variable to change to switch the led off 

//REGULATIONS
void setup() 
{
  size(640, 360);
  noFill();
  position = new PVector(width/2, 0);
  velocity = new PVector(0, 0);
  acceleration = new PVector(0, 0);
  gravity = new PVector(0, 0.5*mass);
  wind = new PVector(0, 0);
  hDampening=map(mass, 15, 80, .98, .96);
}

//ARDUINO INTERACTION
void draw() 
{
  background(255);
  if (!keyPressed) 
  {
    //MOVE CIRCLE BASED ON POTENTIOMETER INPUT
    wind.x = potValue;
    velocity.x*=hDampening;
  }

  applyForce(wind);
  applyForce(gravity);
  velocity.add(acceleration);
  velocity.mult(drag);
  position.add(velocity);
  acceleration.mult(0);
  ellipse(position.x, position.y, mass, mass);
  if (position.y > height-mass/2) 
  {
    velocity.y *= -0.9;  
    position.y = height-mass/2;
  }
}

void applyForce(PVector force) 
{
  // Newton's 2nd law: F = M * A
  // or A = F / M
  PVector f = PVector.div(force, mass);
  acceleration.add(f);
}

void keyPressed() 
{
  if (keyCode==LEFT) 
  {
    wind.x=-1;
  }
  if (keyCode==RIGHT) 
  {
    wind.x=1;
  }
  if (key==' ') 
  {
    mass=random(15, 80);
    position.y=-mass;
    velocity.mult(0);
  }
}

void serialEvent(Serial myPort) 
{
  String s = myPort.readStringUntil('\n');
  s=trim(s);
  if (s != null) {
    int value = int(s);
    potValue = (int)map(value, 0, 1023, 0, width*.01);
  }

  //TURN LED ON IF BALL TOUCHES GROUND
  if (round(velocity.y) < 0) 
  {
    led = 1;
    myPort.write(int(led)+ "\n");
  }
}

 

VIDEO: (click)

Leave a Reply