THREE ARDUINO EXERCISES

Exercise 1: make something that uses only one sensoron arduino and makes the ellipse in processing move on the horizontal axis, in the middle of the screen, and nothing on arduino is controlled by processing.

PROCESSING_EXERCISE 1

import processing.serial.*;// serial object library
Serial myPort; //local serial object from serial library
int xPos=0;
int yPos=0;
 
void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[2]; //link processing to serial port
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}
 
void draw(){
  background(255);
  ellipse(xPos,height/2,30,30);
}
 
void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n'); //strips data of serial port
  s=trim(s);
  if (s!=null){
    
    println(s);
    int value=int(s);
    
    // locates position of ellipse using potentiometer
    xPos=(int)map(value,0,1023,0, width);
  
  }
  myPort.write("\n");
}

ARDUINO_EXERCISE 1

void setup() {
  //initialize the serial port and data upload
  Serial.begin(9600);
  Serial.println("0");
}
 
void loop() {
  //capture data in arduino 
  int sensor= analogRead(A0);
  //put data in the serial port
  while (Serial.available()) {
    if (Serial.read() == '\n') {
      delay(1);
      //prints the value
      Serial.println(sensor);
    }
  }
}

Exercise 2: make something that controls the LED brightness from processing.

PROCESSING_EXERCISE 2

import processing.serial.*;
Serial myPort;
int xPos=0;
int Sensor=0;
boolean Red=false;
boolean Green=false;
void setup(){
size(960,720);
printArray(Serial.list());
String portname=Serial.list()[2];
println(portname);
myPort = new Serial(this,portname,9600);
myPort.clear();
myPort.bufferUntil('\n');
}
void draw(){
background(255);
ellipse(mouseX,mouseY,30,30);
if ((mouseX >= 400) && (mouseY >= 600)) {
Red =true;
} else Red = false;
if ((mouseX <= 400) && (mouseY <= 600)){
Green =true;
}
else Green = false;

}
void serialEvent(Serial myPort){
String s=myPort.readStringUntil('\n');
s=trim(s);
if (s!=null){
println(s);
int values[]=int(split(s,','));
if (values.length==2){
xPos=(int)map(values[0],0,1023,0, width);

}
}
myPort.write(int(Red)+","+int(Green)+"\n");
print("0,0");
}

ARDUINO_EXERCISE 2

int left = 0;
int right = 0;

void setup() {
Serial.begin(9600);
Serial.println("0,0");
pinMode(2, OUTPUT);
pinMode(5, OUTPUT);
}

void loop() {
while (Serial.available()) {
right = Serial.parseInt();
//left = Serial.parseInt();
if (Serial.read() == '\n') {
digitalWrite(2, right);
//digitalWrite(5, left);
int sensor = analogRead(A0);
delay(1);
//int LDRPIN = analogRead(A1);
//delay(1);
Serial.print(sensor);
//Serial.print(',');
//Serial.println(LDRPIN);
}
}
}

Exercise 3: take the gravity wind example and make it so every time the ball bounces one led lights up and then turns off, and you can control the wind from one analog sensor.

PROCESSING_EXERCISE 3

import processing.serial.*;
Serial myPort;
int xPos=0;

int Sensor=0;
boolean Red=false;
boolean Green=false;
PVector velocity;
PVector gravity;
PVector position;
PVector acceleration;
PVector wind;
float drag = 0.99;
float mass = 50;
float hDampening;
void setup(){
size(640,360);
printArray(Serial.list());
String portname=Serial.list()[1];
println(portname);
myPort = new Serial(this,portname,9600);
myPort.clear();
myPort.bufferUntil('\n');
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);
}
void draw(){
background(255);
if (!keyPressed){
wind.x=0;
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; // A little dampening when hitting the bottom
position.y = height-mass/2;
Red = true; 
}
else { Red = false;
}
}
void serialEvent(Serial myPort){
String s=myPort.readStringUntil('\n');
s=trim(s);
if (s!=null){
println(s);
int values[]=int(split(s,','));
if (values.length==2){
xPos=(int)map(values[0],0,1023,0, width);
// yPos=(int)map(values[1],0,1023,0, height);
}
}
myPort.write(int(Red)+","+int(Green)+"\n");
print("0,0");
}
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);
}
}

ARDUINO_EXERCISE 3

int left = 0;
int right = 0;

void setup() {
Serial.begin(9600);
Serial.println("0,0");
pinMode(2, OUTPUT);
// pinMode(5, OUTPUT);
}

void loop() {
while (Serial.available()) {
right = Serial.parseInt();
left = Serial.parseInt();
if (Serial.read() == '\n') {
digitalWrite(2, right);
// digitalWrite(5, left);
int sensor = analogRead(A0);
//delay(1);
//int LDRPIN = analogRead(A1);
delay(1);
 Serial.print(sensor);
//Serial.print(',');
//Serial.println(LDRPIN);
}
}
}

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)

Final Project Initial Concept

  1. Sometimes you just need to put your head down…

I want to create some sort of contraption fitting for the era of Zoom classes. When the user places their head on a pillow, an Arduino sensor will detect if a certain distance is crossed. At that point, there will be some sort of timer activated on Processing that will allow the user to nap for three minutes and sound an alarm. It would be pretty cool to also use a speech to text library from Processing to also record the transcript of the Zoom class so the user can make sure they didn’t miss anything when they wake up. I’m not sure how technologically feasible this is as it would depend on the accuracy of the speech processing library but it would be nice to have some sort of giant alarm that sounds if the user’s name is said in class.

2. Some sort of drawing game. What you draw on Processing is connected to a grid of LEDs/neopixels that correspond to what you draw.

3. Some sort of data visualization web piece using P5.js and Arduino. The user can filter the visualization on P5.js and see the effects on the analog piece (led output).

Final Project Idea

My idea for the time being is making a WHAC-A-MOLE game with a twist of my own using ardiuno and processing together where you use a controller to play the game.

 

The controller will require the following tools:

  • 4 buttons > for the main menu + hitting/whacking the moles
  • motor sensor > so that when it does not detect a movement in a duration of time the game would close
  • LED > to indicate if the game is on or off + another LED that lights if you got the point
  • potentiometer > to adjust the volume of the game

The game would be a normal WHAC-A-MOLE, but I will draw the setup of the game on my own. I will probably look into more ways I could take this game to another level.

Preliminary Concept for Final Project

Concept:

Recreate a piano game using both processing and Arduino. I would like to create a piano interface that would be visually available on the screen of the computer through processing. The input from the user would be through the Arduino where pushing differing buttons will play different notes and the potentiometer will allow accessing different parts of the piano. The visual part of the piano would also change according to the position of the potentiometer.

Specific Features:

I would like to implement two main modes for the game.

  1. The first mode would be a free piano play. Basically, this game mode will allow the user to explore through the piano. Record what is played and edit a final composition.
  2. The second mode would be more like a game where the user will have to play the notes according to what the screen displays. Basically, the game will consist on notes(balls) falling on specific notes to which the user must push a specific button to resemble he/she playing that specific note. The game will also include lives which will be recorded by led lights on the Arduino. The user will lose a life if he/she pushes the incorrect button or fails to push it on time.

This idea was inspired from a game called Smule on the iPad/iPhone

Week 11 (3 Tasks)

Task 1:

Arduino:

//int left = 0;
//int right = 0;
int brightness = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
}
 
void loop() {
  while (Serial.available()) {
    right = Serial.parseInt();
    left = Serial.parseInt();
    if (Serial.read() == '\n') {
      digitalWrite(2, right);
      digitalWrite(5, left);
      int sensor = analogRead(A0);
      delay(1);
      int sensor2 = analogRead(A1);
      delay(1);
      Serial.print(sensor);
      Serial.print(',');
      Serial.println(sensor2);
    }
  }
}

Processing:

import processing.serial.*;
Serial myPort;
int xPos=0;
int yPos=0;
boolean onOff=false;
boolean onOff2=false;


void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[3];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}
 
void draw(){
  background(255);
  ellipse(xPos,yPos,30,30);
  if (mousePressed){
    if(mouseX<=width/2)
      onOff2=true;
    else
      onOff=true;
  }else{
    onOff=onOff2=false;
  }
}
 
void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    println(s);
    int values[]=int(split(s,','));
    if (values.length==2){
      xPos=(int)map(values[0],0,1023,0, width);
      yPos=(int)map(values[1],0,1023,0, height);
    }
  }
  myPort.write(int(onOff)+","+int(onOff2)+"\n");
}

Task 2

Arduino:

int right = 0;
int brightness = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
}
 
void loop() {
  int sensor2 = analogRead(A1);
  Serial.println(sensor2);
  while (Serial.available()) {
    brightness = Serial.parseInt();
    if (Serial.read() == '\n') {
      analogWrite(5, brightness);
      int sensor = analogRead(A0);
      delay(1);
      int sensor2 = analogRead(A1);
      delay(1);
      Serial.print(sensor);
      Serial.print(',');
      Serial.println(sensor2);
    }
  }
}

Processing:

import processing.serial.*;
Serial myPort;
int xPos=0;
int yPos=height/2;
boolean onOff=false;
boolean onOff2=false;
int brightness = 0;


void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[3];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}
 
void draw(){
  background(255);
  ellipse(xPos,yPos,30,30);
  if (mousePressed){
    if(mouseX<=width/2)
      onOff2=true;
    else
      onOff=true;
  }else{
    onOff=onOff2=false;
  }
//Brightness is added
  brightness = (int)map(mouseX, 0, width, 0, 255);
}
 
void serialEvent(Serial myPort){
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  if (s!=null){
    println(s);
    int values[]=int(split(s,','));
    if (values.length==2){
      xPos=(int)map(values[0],0,1023,0, width);
      yPos=(int)map(values[1],0,1023,0, height);
    }
  }
  myPort.write(int(onOff)+","+int(onOff2)+"\n");
  myPort.write(int(brightness)+"\n");
}

As mouse moves to right, brightness increases.

Task 3

Arduino:

int right = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
}
 
void loop() {
  int sensor2 = analogRead(A1);
  Serial.println(sensor2);
  while (Serial.available()) {
    brightness = Serial.parseInt();
    if (Serial.read() == '\n') {
      analogWrite(5, brightness);
      int sensor = analogRead(A0);
      delay(1);
      int sensor2 = analogRead(A1);
      delay(1);
      Serial.print(sensor);
      Serial.print(',');
      Serial.println(sensor2);
    }
  }
}

Processing:

import processing.serial.*;
Serial myPort;
PVector velocity;
PVector gravity;
PVector position;
PVector acceleration;
PVector wind;
float drag = 0.99;
float mass = 50;
float hDampening;
boolean onOff=false;
boolean onOff2=false;


void setup() {
  printArray(Serial.list());
  String portname=Serial.list()[3];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
  size(640,500);
  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);
}

void draw() {
  background(255);
  if (!keyPressed){
    //wind.x=0;
    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;  // A little dampening when hitting the bottom
      position.y = height-mass/2;
      onOff = true;
    }
  else{
    onOff = false;
  }
}
  
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(){ //LEFT AND RIGHT ARE NOT USED ANYMORE
  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){
    println(s);
    int values[]=int(split(s,','));
    if (values.length==2){
      wind.x=(int)map(values[0],490,800,-1, 1);
    }
  }
  myPort.write(int(onOff)+"\n");
}

Preliminary Idea for the Final Project

For the final project, what I’m thinking of is to create some sort of interactive design, so that a person has control over what’s written and displayed on the artwork. Essentially, creating a project, where I would incorporate Web Camera, Arduino, and Processing.

I want to make the project to be the reflection of a person standing behind the webcam so that the content of the artwork is you. So to say, the project will reflect the shape of a person in different geometrical figures and will follow your direction, positions that you change from time to time, and so on. I feel that it would be great to make some sort of distorted mirror of the user’s reflection, which will make the project both entertaining and captivating.

I have attached the video below for the reference.

Final Project Preliminary Idea [week 11]

For the final project, I want to create a game that will help improve a player’s mental health. In this game, a player should destroy objects to maintain the stress level of the character and simultaneously by releasing their stress by hitting 3D buttons. Moreover, this game requires concentration which is proved to be helpful in combating mental health problems.

[UPDATE]

  • The finalized concept for the project

I modified the idea I had a few days ago, but the goal stayed the same – create a project that will help to cope with stress. Stress is one of the most common problems people face in their daily lives, and as a student before final exams, I realize it especially strongly.

This time it won’t be a game but a way to create a work of art while releasing the stress. The algorithm of creating an art piece will be following:

  1. Choose the word that causes your stress. It could be just “stress,” “assignment,” “Calculus” or I would’ve written “C++” last semester. Write it, and it will appear on the screen.
  2. Now the most fascinating part begins: you will hit this word! Obviously, not the word itself, but the latex stretched over the frame. Behind it, there will be a distance sensor to understand where you hit this time and display your actions on the screen.
  3. Each hit will be seen as a spot in a certain color and a certain place. It will be possible to change the color of the spots by pressing the buttons on the Arduino, as well as the intensity of the applied color with a potentiometer.
  4. Also, there will be few songs to choose from to hit the word that causes your stress with even more satisfaction.
  5. I am thinking of doing these colored spots using generative art to make each piece even more unique.

The process of hitting will probably help to reduce the stress, and the result like the unique artwork will help to elevate the mood. 

  • Arduino

The Arduino program will receive inputs from buttons, potentiometer, and ultrasonic distance sensor, and will send them to Processing. Distance sensor will determine the distance to the hand hit through the latex. 

  • Processing

The Processing program will receive data from Arduino, and use it in the art piece creation, by adding colored spots of different intensity on the chosen word. Pressed buttons’ colors will determine the colors used in Processing, potentiometer readings will change the intensity of the color. The distance values will affect the location of spots appearing on the screen. 

serial communication exercises [week 11]

Exercise 1:  make something that uses only one sensor  on arduino and makes the ellipse in processing move on the horizontal axis, in the middle of the screen, and nothing on arduino is controlled by processing;

Arduino:

const int knobLed = A0; //sign up variable
 
void setup() {
  Serial.begin(9600); //set the baud rate
  Serial.println("0"); //start of the handshake, start the call-response pattern
  pinMode(A0, INPUT);
}
 
void loop() {
  while (Serial.available()) { //whenever there is anything available in serial buffer, do
    if (Serial.read() == '\n') { //whenever there is new line, it means that all info came correctly
      int sensor = analogRead(knobLed); //read from potentiometer
      delay(1);
      Serial.println(sensor); //send info to the processing
    }
  }
}

Processing:

import processing.serial.*; //import library
Serial myPort; //create serial object
int xPos=0; 
int yPos;
 
void setup() {
  size(960, 720);
  yPos=height/2;
  printArray(Serial.list()); //printing array
  String portname=Serial.list()[1]; //choose the correct port number!
  println(portname);
  myPort = new Serial(this, portname, 9600); //set the baud rate
  myPort.clear(); //clear the buffer
  myPort.bufferUntil('\n'); //wait until new line
}
 
void draw() {
  background(255);
  ellipse(xPos, yPos, 30, 30);
}
 
void serialEvent(Serial myPort) { //whenever receives incoming serial message, this function is called 
  String s=myPort.readStringUntil('\n'); //read the string until new line character
  s=trim(s); //safeguard to trim extra characters (whitespaces)
  if (s!=null) { //check to make sure it's not null
    println(s); 
    int value = int(s);  //sending value s
    xPos=(int)map(value, 0, 1023, 0, width); 
  }
  myPort.write("\n"); //other side of the handshake, send back over to arduino
}

Exercise 2: make something that controls the LED brightness from processing;

Arduino:

float brightness;

void setup() {
  Serial.begin(9600); //set the baud rate
  Serial.println("0"); //start the handshake
  pinMode(5, OUTPUT);
}

void loop() {
  while (Serial.available()) {
    brightness = Serial.parseFloat(); //receive brightness info in a string from processing
    if (Serial.read() == '\n') { //whenever there is new line, it means that all info came correctly
       analogWrite(5, brightness); //change the brightness of LED accordingly
       Serial.print("\n"); //send  new line to processing
    }
  }
}

Processing:

import processing.serial.*;
Serial myPort; //create serial object
float brightness; //variable to send to arduino
 
void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[1]; //choose the correct port number!
  println(portname);
  myPort = new Serial(this,portname,9600); //set the baud rate
  myPort.clear(); //clear the buffer
  myPort.bufferUntil('\n'); //wait until new line
}

void draw(){
  background(255);
  ellipse(mouseX,mouseY,30,30);
  brightness = map(height-mouseY, 0, height, 0, 255); 
}

void serialEvent(Serial myPort){ //whenever receives incoming serial message, this function is called 
  println(brightness); 
  myPort.write(int(brightness)+"\n"); //other side of the handshake, send back over to arduino
}

Exercise 3: take the gravity wind example and make it so every time the ball bounces one led lights up and then turns off, and you can control the wind from one analog sensor;

Arduino:

const int knobLed = A0;
const int led1 = 3;
int onOff = 0;

void setup() {
  Serial.begin(9600); //set the baud rate
  Serial.println("0"); //start the handshake
}

void loop() {
  while (Serial.available()) {
    onOff = Serial.parseInt(); //receive onOff info from processing
    if (Serial.read() == '\n') { //whenever there is new line, it means that all info came correctly
      int sensor = analogRead(knobLed); //read from potentiometer
      delay(1);
      Serial.println(sensor); //send info to processing
            if (onOff == 1) { 
              analogWrite(led1, 255);//turn on LED
            } else {
              analogWrite(led1, 0); //turn off LED
            }
    }
  }
}

Processing:

PVector velocity;
PVector gravity;
PVector position;
PVector acceleration;
PVector wind;
float drag = 0.99;
float mass = 50;
float hDampening;

import processing.serial.*;
Serial myPort; //create serial object
int knob;

void setup() {
  size(640,360);
  noFill();
  position = new PVector(0, 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);
  
  String portname=Serial.list()[1]; //choose the correct port number!
  myPort = new Serial(this,portname,9600); //set the baud rate
  myPort.clear(); //clear the buffer
  myPort.bufferUntil('\n'); //wait until new line
}

void draw() {
  background(255);
  if (!keyPressed){
    wind.x=knob;
    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;  // A little dampening when hitting the bottom
      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){ //whenever receives incoming serial message, this function is called 
  String s=myPort.readStringUntil('\n');  //read the string until new line character
  s=trim(s);  //safeguard to trim extra characters (whitespaces)
  if (s!=null){ //check to make sure it's not null
    println(s);
    int values[]=int(split(s,',')); //sending value s
    knob=(int)map(values[0],0,1023,0, width*.01);
  }
  if (round(velocity.y) < 0) {
    myPort.write(1 + "\n");
  } else {
    myPort.write(0 + "\n");
  }
}

Video:

week 11 exercise – Thais and Shaikha

Exercise 1

Make something that uses only one sensor  on arduino and makes the ellipse in processing move on the horizontal axis, in the middle of the screen, and nothing on arduino is controlled by processing
For this exercise we used the potentiometer to control the movement of the ellipse
Arduino

 

Exercise 1

Make something that uses only one sensor  on arduino and makes the ellipse in processing move on the horizontal axis, in the middle of the screen, and nothing on arduino is controlled by processing
For this exercise we used the potentiometer to control the movement of the ellipse
Arduino

 

 

Processing

 

 

Exercise 2

Make something that controls the LED brightness from processing.
For this exercise we used the x value of the position of the mouse to control the brightness of the LED.
Arduino
//Exercise 2

int ledPin = 5;

float brightness;

void setup() {
  Serial.begin(9600);
  Serial.println("0");
  pinMode(ledPin, OUTPUT);
}

void loop() {
  while (Serial.available()) {
    //get the brightness value from processing
    brightness = Serial.parseFloat();
    
    if (Serial.read() == '\n') {
    //turn on the LED with the given brightness
      analogWrite(ledPin, brightness);
      Serial.println(brightness);
    }
  }
}

 

Processing

 

//Exercise 2
import processing.serial.*;
Serial myPort;
int xPos = 0;
int yPos;

int brightness;

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

void draw() {
  background(255);

  //map the mouseX position to the range of birghtness of the LED
  brightness = int(map(mouseX, 0, width, 0, 255));
  
  yPos = height/2;
  ellipse(mouseX, yPos, 30, 30);
}

void serialEvent(Serial myPort) {
  String s=myPort.readStringUntil('\n');
  s=trim(s);

  //write brightness so that Arduino can use it
  myPort.write(brightness+ "\n");
}

 

Exercise 3

Take the gravity wind example (https://github.com/aaronsherwood/introduction_interactive_media/blob/master/processingExamples/gravityExamples/gravityWind/gravityWind.pde) and make it so every time the ball bounces one led lights up and then turns off, and you can control the wind from one analog sensor.
The analog sensor we used was the potentiometer.
Arduino
int left = 0;
int right = 0;

int light1 = 0;
int LED1 = LOW;

void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
}

void loop() {
  while (Serial.available()) {
    //    right = Serial.parseInt();

    //get whether LED should be turned on from Processing
    light1 = Serial.parseInt();
    
    Serial.println(light1);

    if (Serial.read() == '\n')
    {
      
      if (light1 == 1)
      {
        LED1 = HIGH;
      }
      else
      {
        LED1 = LOW;
        
      }
      digitalWrite(5, LED1);

      //get the vvalues from the potentiometer
      int sensor = analogRead(A0);
      delay(1);
     
      //      delay(1);
      Serial.println(sensor);
      
      //      Serial.print(',');
      //      Serial.println(sensor2);
      
      //Serial.println(LED1);
    }
  }
}

 

Processing