In-class Exercises

Here were the prompts we got for this week:

  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
  2. make something that controls the LED brightness from processing
  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

Here’s the code for each:

Exercise 1:

Arduino sketch

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
void setup() {
Serial.begin(9600);
Serial.println("0,0");
}
void loop() {
while (Serial.available()) {
if (Serial.read() == '\n') {
int sensor = analogRead(A0);
Serial.println(sensor);
}
}
}
void setup() { Serial.begin(9600); Serial.println("0,0"); } void loop() { while (Serial.available()) { if (Serial.read() == '\n') { int sensor = analogRead(A0); Serial.println(sensor); } } }
void setup() {
  Serial.begin(9600);
  Serial.println("0,0"); 
}

void loop() {

 while (Serial.available()) {
 if (Serial.read() == '\n') {
      int sensor = analogRead(A0);
      Serial.println(sensor);
    }
  }
}

Processing sketch

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import processing.serial.*;
Serial myPort;
int xPos=0;
void setup(){
size(960,720);
printArray(Serial.list());
String portname=Serial.list()[6];
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');
s=trim(s);
if (s!=null){
println(s);
int values=int(s);
if (values >= 0){
xPos=(int)map(values,0,1023,0, width);
}
}
myPort.write("\n");
}
import processing.serial.*; Serial myPort; int xPos=0; void setup(){ size(960,720); printArray(Serial.list()); String portname=Serial.list()[6]; 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'); s=trim(s); if (s!=null){ println(s); int values=int(s); if (values >= 0){ xPos=(int)map(values,0,1023,0, width); } } myPort.write("\n"); }
import processing.serial.*;
Serial myPort;
int xPos=0;
void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[6];
  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');
  s=trim(s);
  if (s!=null){
    println(s);
    int values=int(s);
    if (values >= 0){
      xPos=(int)map(values,0,1023,0, width);
    }
  }
  myPort.write("\n");
}

Exercise 2:

Changes Brightness according to mouseX

Arduino sketch

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int led = 0;
int right = 0;
void setup() {
Serial.begin(9600);
Serial.println("0,0");
pinMode(2, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
while (Serial.available()) {
led = Serial.parseInt();
//left = Serial.parseInt();
if (Serial.read() == '\n') {
analogWrite(2, led);
//digitalWrite(5, left);
Serial.println();
}
}
}
int led = 0; int right = 0; void setup() { Serial.begin(9600); Serial.println("0,0"); pinMode(2, OUTPUT); pinMode(5, OUTPUT); } void loop() { while (Serial.available()) { led = Serial.parseInt(); //left = Serial.parseInt(); if (Serial.read() == '\n') { analogWrite(2, led); //digitalWrite(5, left); Serial.println(); } } }
int led = 0;
int right = 0;
void setup() {
  Serial.begin(9600);
  Serial.println("0,0");
  pinMode(2, OUTPUT);
  pinMode(5, OUTPUT);
}
void loop() {
  while (Serial.available()) {
    led = Serial.parseInt();
    //left = Serial.parseInt();
    if (Serial.read() == '\n') {
      analogWrite(2, led);
      //digitalWrite(5, left);
      Serial.println();
    }
  }
}

 

Processing sketch

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import processing.serial.*;
Serial myPort;
//int xPos=0;
//int yPos=0;
int onOff=0;
//boolean onOff2=false;
void setup(){
size(960,720);
printArray(Serial.list());
String portname=Serial.list()[6];
println(portname);
myPort = new Serial(this,portname,9600);
myPort.clear();
myPort.bufferUntil('\n');
}
void draw(){
background(255);
//ellipse(xPos,yPos,30,30);
onOff = mouseX;
}
void serialEvent(Serial myPort){
myPort.readStringUntil('\n');
int mappedValues=(int)map(onOff,0,width,0, 255);
println(mouseX+" "+mappedValues);
myPort.write(mappedValues+"\n");
}
import processing.serial.*; Serial myPort; //int xPos=0; //int yPos=0; int onOff=0; //boolean onOff2=false; void setup(){ size(960,720); printArray(Serial.list()); String portname=Serial.list()[6]; println(portname); myPort = new Serial(this,portname,9600); myPort.clear(); myPort.bufferUntil('\n'); } void draw(){ background(255); //ellipse(xPos,yPos,30,30); onOff = mouseX; } void serialEvent(Serial myPort){ myPort.readStringUntil('\n'); int mappedValues=(int)map(onOff,0,width,0, 255); println(mouseX+" "+mappedValues); myPort.write(mappedValues+"\n"); }
import processing.serial.*;
Serial myPort;
//int xPos=0;
//int yPos=0;
int onOff=0;
//boolean onOff2=false;
void setup(){
  size(960,720);
  printArray(Serial.list());
  String portname=Serial.list()[6];
  println(portname);
  myPort = new Serial(this,portname,9600);
  myPort.clear();
  myPort.bufferUntil('\n');
}
void draw(){
  background(255);
  //ellipse(xPos,yPos,30,30);
 
    onOff = mouseX;
  
}
void serialEvent(Serial myPort){
 myPort.readStringUntil('\n');
  
  int mappedValues=(int)map(onOff,0,width,0, 255);
    println(mouseX+" "+mappedValues);

  myPort.write(mappedValues+"\n");
}

 

Exercise 3:

Arduino sketch

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int left = 0;
int right = 0;
int ledOn=0;
void setup() {
Serial.begin(9600);
Serial.println("0,0");
pinMode(2, OUTPUT);
}
void loop() {
while (Serial.available()) {
ledOn = Serial.parseInt();
if (Serial.read() == '\n') {
digitalWrite(2, ledOn);
delay(2);
int sensor = analogRead(A0);
delay(1);
Serial.println(sensor);
}
}
}
int left = 0; int right = 0; int ledOn=0; void setup() { Serial.begin(9600); Serial.println("0,0"); pinMode(2, OUTPUT); } void loop() { while (Serial.available()) { ledOn = Serial.parseInt(); if (Serial.read() == '\n') { digitalWrite(2, ledOn); delay(2); int sensor = analogRead(A0); delay(1); Serial.println(sensor); } } }
int left = 0;
int right = 0;
int ledOn=0;

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

void loop() {

 while (Serial.available()) {
    
    ledOn = Serial.parseInt(); 
    if (Serial.read() == '\n') {
      
      digitalWrite(2, ledOn);
      delay(2);
     
      int sensor = analogRead(A0);
      delay(1);
      Serial.println(sensor); 
    }
  }
 
}


 

Processing sketch

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
PVector velocity;
PVector gravity;
PVector position;
PVector acceleration;
PVector wind;
float drag = 0.99;
float mass = 50;
float hDampening;
import processing.serial.*; //serial library built-in
Serial myPort;
int bounce = 0;
float oldPos = 0;
int oldValue;
void setup() {
String portname=Serial.list()[6];
println(portname);
myPort = new Serial(this, portname, 9600);
myPort.clear(); //cleaning out serial buffer
myPort.bufferUntil('\n'); //buffer /fill it up until \n
size(960, 720);
printArray(Serial.list()); //print out all the port that's available
//find which one is your arduino that's the one port
//this can change everytime - so recheck - check if it is the right port
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);
delay(1000);
}
void draw() {
bounce = 0;
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 ((int(position.y) >= height-mass/2) && (oldPos!=int(position.y))){
bounce = 1;
}
if (position.y > height-mass/2) {
oldPos=int(position.y);
velocity.y *= -0.9; // A little dampening when hitting the bottom
position.y = height-mass/2;
//bounce = 1;
}
// println("Y:"+position.y+" "+cond);
}
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 (key==' '){
mass=random(15,80);
position.y=-mass;
velocity.mult(0);
}
}
void serialEvent(Serial myPort) {
myPort.write(bounce+"\n"); //send information to the arduino
String s=myPort.readStringUntil('\n');
s=trim(s);
if (s!=null){
int value=int(s);
int mappedValues=(int)map(value,0,1023,0, width);
// println(s);
println(value+" "+oldValue);
if ((mappedValues <= width/2)&&(value!=oldValue)){
wind.x=-1;
}
else if ((mappedValues > width/2)&&(value!=oldValue)){
wind.x=1;
}
else
wind.x=0;
oldValue = value;
}
}
PVector velocity; PVector gravity; PVector position; PVector acceleration; PVector wind; float drag = 0.99; float mass = 50; float hDampening; import processing.serial.*; //serial library built-in Serial myPort; int bounce = 0; float oldPos = 0; int oldValue; void setup() { String portname=Serial.list()[6]; println(portname); myPort = new Serial(this, portname, 9600); myPort.clear(); //cleaning out serial buffer myPort.bufferUntil('\n'); //buffer /fill it up until \n size(960, 720); printArray(Serial.list()); //print out all the port that's available //find which one is your arduino that's the one port //this can change everytime - so recheck - check if it is the right port 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); delay(1000); } void draw() { bounce = 0; 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 ((int(position.y) >= height-mass/2) && (oldPos!=int(position.y))){ bounce = 1; } if (position.y > height-mass/2) { oldPos=int(position.y); velocity.y *= -0.9; // A little dampening when hitting the bottom position.y = height-mass/2; //bounce = 1; } // println("Y:"+position.y+" "+cond); } 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 (key==' '){ mass=random(15,80); position.y=-mass; velocity.mult(0); } } void serialEvent(Serial myPort) { myPort.write(bounce+"\n"); //send information to the arduino String s=myPort.readStringUntil('\n'); s=trim(s); if (s!=null){ int value=int(s); int mappedValues=(int)map(value,0,1023,0, width); // println(s); println(value+" "+oldValue); if ((mappedValues <= width/2)&&(value!=oldValue)){ wind.x=-1; } else if ((mappedValues > width/2)&&(value!=oldValue)){ wind.x=1; } else wind.x=0; oldValue = value; } }
PVector velocity;
PVector gravity;
PVector position;
PVector acceleration;
PVector wind;
float drag = 0.99;
float mass = 50;
float hDampening;


import processing.serial.*; //serial library built-in
Serial myPort; 


int bounce = 0;
float oldPos = 0;
int oldValue;

void setup() {
 String portname=Serial.list()[6];  
 println(portname);
  myPort = new Serial(this, portname, 9600);
  myPort.clear(); //cleaning out serial buffer
  myPort.bufferUntil('\n'); //buffer /fill it up until \n
  
    size(960, 720);
  printArray(Serial.list());  //print out all the port that's available
 //find which one is your arduino that's the one port
  //this can change everytime - so recheck - check if it is the right port

 
  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);
  delay(1000);
}

void draw() {
    bounce = 0;

  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 ((int(position.y) >= height-mass/2) && (oldPos!=int(position.y))){
      bounce = 1;
    }
  if (position.y > height-mass/2) {
      oldPos=int(position.y);
      velocity.y *= -0.9;  // A little dampening when hitting the bottom
      position.y = height-mass/2;
      //bounce = 1;
    }

// println("Y:"+position.y+" "+cond);
}
  
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 (key==' '){
    mass=random(15,80);
    position.y=-mass;
    velocity.mult(0);
  }
}

void serialEvent(Serial myPort) {
  myPort.write(bounce+"\n"); //send information to the arduino
  String s=myPort.readStringUntil('\n');
  s=trim(s);
  
  if (s!=null){
    int value=int(s);
    int mappedValues=(int)map(value,0,1023,0, width);
  //  println(s);
        println(value+" "+oldValue);

    if ((mappedValues <= width/2)&&(value!=oldValue)){
      wind.x=-1;
    }
    else if ((mappedValues > width/2)&&(value!=oldValue)){
      wind.x=1;
  }
  else
  wind.x=0;
      oldValue = value;


  }

}

Video demo

Leave a Reply