March 17th Lab: André Lucas

Lab 5 – Serial Communication

Date of Lab – 03/17/17

Date of Documentation – 03/22/17

Instructor – Antonius

Partner – Vanelly Garces

 

Goal:   Our goal during this lab was to create an output from processing to arduino (lighting up an LED) and also make the arduino create an output through processing on the computer.

 

Materials used:

  • Arduino
  • LED
  • Wires
  • Button
  • 10k resistors

Processing  to Arduino:

So for processing to arduino, Vanelly and I were a little confused on how to to start and what code exactly to use. We looked through notes and got a general idea, but after asking Marcela for some help, we were able to come up with this code:

Arduino:

int val;

void setup() {

Serial.begin(9600);

pinMode(13, OUTPUT);

}

void loop() {

// receive a value from processing

while (Serial.available()) {

val = Serial.read();

}

// test the value

if (val == ‘H’) {

digitalWrite(13, HIGH);

} else if (val == ‘L’) {

digitalWrite(13, LOW);

}

delay(10); // wait for Processing

}

 

And Processing:

import processing.serial.*;

Serial myPort;

int val;

void setup() {

printArray(Serial.list());

// this prints out the list of all

// available serial ports on your computer.

myPort = new Serial(this, Serial.list()[ 1 ], 9600);

// Change the Serial.list()[0] to your port

}

void draw() {

// to send a value to the Arduino

if (mousePressed) {

myPort.write(‘H’);

} else {

myPort.write(‘L’);

}

}

So essentially what we had to do was tell our arduino that H is high, meaning on, and L is low, meaning off. After, we had to create the code for processing saying that mousepressed means H for on and when the mouse is not being pressed that means the value of L is being expressed, meaning off. We didn’t use the breadboard for this part, we just twisted a resistor around the LED and placed it in PIN 13.

 

 

 

Processing to Arduino was much more simple.

This was our code:

Processing:

import processing.serial.*;

Serial myPort;

int input = 0;

void setup(){

printArray(Serial.list());

myPort = new Serial(this, Serial.list()[1], 9600);

 

background(0,200,200);

size(500,500);

}

void draw(){

while(myPort.available() > 0){

input = myPort.read();

}

if(input == 1){

rect(250,250,30,30);

}

else{

background(0,0,0);

}

}

 

And Arduino:

int val;

void setup() {

Serial.begin(9600);

pinMode(7, INPUT);

}

void loop() {

// receive a value from processing

while (Serial.available()) {

val = Serial.read();

}

if (val == ‘H’) {

digitalWrite(13, HIGH);

} else if (val == ‘L’) {

digitalWrite(13, LOW);

}

if(digitalRead(7) == HIGH){

Serial.write(1);

}

else{

Serial.write(0);

}

}

 

Essentially what we had to do here is change the code for processing to have a background and be a larger rectangle. We made the background black and made the rectangle white. We had to use the breadboard this time and connect it to the arduino with a resistor and a button. For the arduino code, we had to tell it that the pin was now 7 instead of 13 and that when the button was pressed for the rectangle to show and when it wasn’t for it to go back to black.

 

Reflection:

 

We were able to successfully complete the tasks and it was cool to be able to create something on the screen from just pressing a simple button on the breadboard. My goal is to not have to ask for any help in the future, but thanks to Marcela’s help, we were able to get the LED to light up and all without a breadboard! I think I need to look more into the mouspressed function, though as it is still slightly confusing for me.

 

**I was not able to upload any of the pictures or videos I have for an unknown reason. I changed their format and everything to try and tailor it to the format of this platform, but it only told me error. If you would like to see them I can send them to you in an Email.**

 

Lab 5

In this week’s lab, we practiced serial communication between Arduino and processing. First I practiced using Arduino to input data and processing to output. I again used the ambient light sensor. The data got by Arduino would change according to the light intensity. The data got by Arduino ranges from 0 to 1024. So I thought the data would be the same when received by processing. But it was not the case. After trying for several times, I found that the data got by processing actually ranged from 0 to 256. Here is the little project I made, a circle whose radium can be changed by light intensity.

Then I tried to use processing to input and Arduino to output. After the experience of doing the previous project, it was much easier for me to complete this small project. It is a buzzer that can be controlled by keyboard.

Serial communication 1 Arduino part
int i=0;
int newi=0;

void setup(){
    Serial.begin(9600);
}

void loop(){
    i = analogRead(0);
    
    Serial.write(i);
    //Serial.println(i);
    delay(300);
    }

Serial communication 1 processing part
import processing.serial.*;   
Serial myPort;
float i;
float newi;
void setup(){
    myPort=new Serial(this,"/dev/cu.usbmodem1421",9600);  
    size(600,600);
    background(0);
}

void draw(){
    while ( myPort.available() > 0) {
    i = myPort.read();
    newi = map(i,0,256,0,2048);
    }
        
        println(newi);
        noStroke();
        fill(26,76,102,10);
        ellipse(300,300,(2046-newi)/2,(2046-newi)/2);
    }

Serial communication 2 Arduino part

int i;
void setup() {
  Serial.begin(9600);
  pinMode(13,OUTPUT);
}

void loop() {
  while (Serial.available()){
    i = Serial.read();
  }
  if ( i == 'H'){
    tone(13,2000);
   
  }
  else{
    noTone(13);
    }
}

Serial communication 2 processing part
import processing.serial.*;
Serial myPort;
int i;

int seq = 0;

void setup(){
  size(500,500);
  background(0);
  
  myPort = new Serial(this,"/dev/cu.usbmodem1421",9600); 
}
void draw(){
  
  if (key == ' '){
     myPort.write('H');
  }   
  else if (key == 's') {
    myPort.write('L');
  }
}

Lab 6 Documentation by Amber Wang (Marcela)

Lab 6: Serial Communication

Date: March 17, 2017

Instructor: Marcela Godoy

Partner: Esme

Documented by Amber Wang

In today’s lab, we are supposed to build serial communication between Arduino and Processing.

  1. Arduino -> Processing

The first task we did was to use Arduino to tell Processing to do something. Esme and I was kind of lost and confused about what to do at first, so we first reviewed the slides and examples of lecture 9. We tried again one Arduino-Processing class example which is to use a potentiometer to control the size of a circle on the screen. By practicing this class example, we understood where to check the port number and how to adjust the code. So the “printArray” function will give you an array of several ports and we are supposed to choose the port in accord with “usb” and write the number of that port inside the [ ] after “Serial.list()”. After the review, we started to build our own codes. In order to use Arduino to control Processing, we first chose an electronic component as an input in Arduino. Because we just used a potentiometer, this time we decided to use a button. For the change in Processing, we refer to another in-class example in lecture 12 which is using “mousePressed” to change the background color of the window. We found similarity between the mousePressed and button because both of them are boolean. So we decided to use the button to change the window color.

lab6w1 lab6w2

As you can see in the code above, we connected the button to pin 12 and use “Serial.write” to send the value read from pin 12 to Processing. Then in Processing, first we import the serial library and set a variable to store the value from Arduino. Then I write a function to say if the button is pressed, we get three random R,G,B values and set the background according to the random set. However, I made a big mistake here because the value sent from Arduino to Processing is “1,0” not “true/ false”. So the error note reminds me that I cannot convert from int to boolean. I thought mousePressed was “true/false” so button pressed must be the same. But in fact Serial.write is sending bits and byte! Then Lewis helped us to fix it and set the initial value to be 0. Then when the button is pressed the value will be 1.

Another mistake we made was the circuit of a button. We firstly just directly connected the two pins of the button to pin 12 and ground, which has no effect. But the right way to do it is to add a resistor to the circuit and connect one side of resistor to the power, the other side to the button. The pin 12 is supposed to connect to the point between button and resistor to detect the change in voltage of the circuit.

After fixing the circuit we managed to use the button to change the background color. However, I noticed a small unstable flashing on the screen whenever I press the button and change the color. We asked Lewis to help us again. He then set another variable “pv” to our code and add “pv == 0” to our condition. This perfectly solve the flashing problem because without the condition, when you press the button, the function of changing the background color will keep running again and again, but with the condition “pv == 0”, the background color will only change when last state is “not pressed” which is “0”.

Revised codes:

lab6.1

The finished effect is as follows:

2. Processing -> Arduino

After lab, I did another practice using processing to tell Arduino to do something. I chose LED as the output on Arduino and keyboard as input in Processing. I built a circuit using three different LED lights with three resistors and connected the to digital pin 9, 10 and 11. In Processing, after filling in the port bracket I set a “keyPressed” function. Inside the function, I set three conditions to pass three characters when the three keys were pressed.  Then in Arduino, I set a variable to store the value from Processing and make another three conditions. So when key “r” is pressed, the red light will be on; when “g” is pressed, the green light, etc.

lab6.2

The finished effect is as follows:

 

 

Lab 6 | Arduino Exchanges

Date 17 March 2017
Section Instructor Marcela Godoy
Partner Mark Chan
Aim Practice exchange data between Processing and Arduino using serial communication by making a Processing sketch that sends instructions to Arduino and make Arduino respond with instructions to Processing.
Material used Laptop with Processing and Arduino IDE; Arduino; breadboard; potentiometer; LED lamp; 220 Ohm resistor; wires

Motivation

In an effort to be creative, we decided to use Processing and Arduino to make a bare-bones, Tetris-style game, where a block would fall from the top of the screen and be adjusted by a variable resistor connected to Arduino. To make Processing pass a meaningful signal to Arduino, we decided to add a red LED light that would blink to indicate that a user had “lost the game,” i.e. failed to fit the block into the empty space.

Building the circuit

The circuit itself was trivial, and it took us only a few minutes to build it as we envisioned it, recording analog input from the variable resistor and sending digital output to the LED:

IMAG0723

 

Coding steps

Although reading input from Arduino felt fairly straightforward, we struggled to get accurate readings from the potentiometer. Increasing the delay in the code further made the input more accurate. We also struggled with a conversion bug in our Arduino code, which long divided the input location (which is a hexadecimal number) by four rather than the analog input itself.

Once we were able to successfully record the input, we set about making the game; we made it so that Processing would initially select a random interval on the screen, and draw a yellow barrier on each side. A purple box would gradually fall down on the screen — its horizontal position would be regulated by the potentiometer. Coding this was trivial and rather similar to my Lab 3:

Our basic gameplay with an adjustable purple box aiming to hit an interval was complete.

Once we had resolved the basic gameplay, we started coding the winning condition; we set a boundary from the bottom at which the program would evaluate whether the box fitted the boundaries and execute different functions based on whether it did. A successful fit would trigger a green congratulatory message, and a failure would trigger a red notification and light up the red LED on our board. Making the conditions and sending a signal to the LED based on the code we looked at in the preceding lecture was trivial:

A demonstration of the response when the game is lost — notice the LED that lights up.

A demonstration of the response when the game is won.

 

Evaluation

I quite enjoyed making this simple game — although a lot more could be done to it to make it more interesting and user-friendly (such as looping it), it powerfully demonstrates how simple it is to create a basic, yet fun game with Arduino and Processing. In this exercise, my main learning came from the quirks of signaling between Arduino and Processing — such as setting delays and signal speeds — but I felt as even that worked out quite well aside from a few beginner errors.

/*
   Arduino Exchanges
   by Sjur Hamre <sh3670> and Mark Chan <mc5859>
   IX Lab 17 March, 2017

   The Arduino component of a simple Tetris-style game. 
   Based on the NYU Shanghai IMA-provided sample code.
   
   Public Domain
*/


int val;

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}


void loop() {
  int sensorValue = analogRead(A0) / 4;
  Serial.write(sensorValue);

  // Delay to record input from the potentiometer accurately
  delay(140);
  while (Serial.available()) {
    val = Serial.read();
  }

  if (val == 'L') { // If a loss is recorded, send signal to LED
    digitalWrite(13, HIGH);
  }
  delay(10);

}



/*
   Processing Exchanges
   by Sjur Hamre <sh3670> and Mark Chan <mc5859>
   IX Lab 17 March, 2017

   The Processing component of a simple Tetris-style game. 
   Based on the NYU Shanghai IMA-provided sample code.
   
   Public Domain
*/


import processing.serial.*;

int x = int(random(10, 90));
Serial myPort;
int valueFromArduino;
int win = 0;
int lose = 0;
int s = 0;

int boxPos;

void setup() {
  size(500, 500);
  myPort = new Serial(this, "COM8", 9600); // Note: change to appropriate port
}


void draw() {
  // to read the value from the Arduino
  while ( myPort.available() > 0) {
    valueFromArduino = myPort.read();
    
    // Setup the game
    background(0);
    boxPos = int(map(valueFromArduino, 0, 255, 0, width-30)); // Map input value from potentiometer
    fill(255, 0, 255);
    rect(boxPos, s, 30, 30); // Draw the box

    fill(255, 255, 0);
    rect(0, height-50, x*5, 50); // Setup area that the box should fall within
    rect(x*5+50, height-50, width, 50);
    s=s+10; // Move box downward
    
    if(win==1) {
        win();
        break;
    }
    if(lose==1) {
        lose();
        break;
    }  
      
    if (s>height-80) { // When the box passes the yellow area, check if it's within boundaries
      if (x*5<boxPos && boxPos<x*5+20) {
        win = 1;
      } else {
        lose = 1;
      }
    }
  }
}

void win() { // Green, cheerful screen
    background(0,250,0);
    textSize(100);
    fill(255,255,255);
    text("You Win", 30,250);
}
void lose() { // Red, not-so-cheerful screen
    background(250,0,0);
    textSize(100);
    fill(255,255,255);
    text("You Lose", 30,250);
  myPort.write('L'); // Send signal to shine red LED
  
}

IxLab: Lab6-Serial Communication(Dan)

Date: March 17, 2017

Instructor: Dan

Partner: Junyao(June) Wu

 

Aim: The aim of today’s lab is around serial communication. We are instructed to write codes that enables input on one side (Processing or Arduino) to create output on the other side (Arduino or Processing).

 

Processing To Arduino: To control an Arduino output from the computer through the Processing language, we used the keyPressed variable and the key variable to generate the effect that when a key is pressed, buzzer will play a respective pre-set tone.

To start with, we tried to create a program that make the buzzer to generate a tone when a key is pressed, yet we mistook the boolean variable keyPressed for a char. After closer reading of the textbook, we fixed the problem by introducing the key variable into the program.

Then we started to try two keys, which, if succeed, will make multiple keys possible. We used the if conditional to let Arduino understand that different keys are pressed. Yet we didn’t succeed on our first try, which results in play the same tone whichever key is pressed.

Then, when checking the code, we realized that it’s because we misused a ; in the if condition. And then the problem is fixed.

屏幕快照 2017-03-17 下午2.19.14

The codes are as follows:

//Arduino
const int buzzer = 13;
int val;
void setup() {
Serial.begin(9600);
pinMode(buzzer,OUTPUT);
}

void loop() {

// receive a value from processing
while (Serial.available()) {

val = Serial.read();
}
// test the value
if (val == ‘b’) {
tone(buzzer,1000);
} else if (val == ‘q’) {

noTone(buzzer);
} else if (val==’c’) {
tone(buzzer,500);
}

delay(10); // wait for Processing
Serial.println(val);

}

//Processing
import processing.serial.*;
Serial myPort;
int val;
void setup() {
printArray(Serial.list());
// this prints out the list of all
// available serial ports on your computer.

myPort = new Serial(this, Serial.list()[1], 9600);
// Change the Serial.list()[0] to your port
}
void draw() {
// to send a value to the Arduino
if (keyPressed) {
if(key==’b’){
myPort.write(‘b’);
}if(key==’c’){
myPort.write(‘c’);
}
} else {
myPort.write(‘q’);
}
}

 

Arduino To Processing: We started late on doing this part of the lab, for that we spent too much time on debugging the first part, so we decided to use a potentiometer to control the size of a circle on the computer. Yet the process isn’t easy, either, for that we got confused with the kind of variable that can be transferred between Arduino and Processing, and keep getting error. But thankfully we made it just on time.

The codes are as follows:

//Arduino
int potpin=0;
void setup() {
Serial.begin(9600);
}

void loop() {
int val=analogRead(potpin);
int valu=map(val,0,1023,0,200);
Serial.write(valu);
delay(20);
Serial.println(valu);

}

//Processing
import processing.serial.*;
int val = 0; // To store data from serial port
Serial myPort;// The serial port object
void setup() {
size(500,500);
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[1], 9600);

}
void draw(){
background(0);
while ( myPort.available() > 0) { // If data is available,
val = myPort.read(); // read it and store it in val
}
fill(255);
ellipse(250,250,val,val);
}

Interaction Lab: Lab 6-Serial Communication (June)

Lab Date: March,17,2017
Instructor: Professor Dan
Lab Partner:
 Tony

The Aim of Today’s Lab: Exchange data between Processing and Arduino using serial communication.

Exercise 1: Use Processing to tell Arduino to do something

For the first exercise, we wanted to make the buzzer sound different when we pressed different keys. We used the sample code from Thursday’s class and modified codes in both Processing and Arduino. We changed the “MousePressed” into “KeyPressed”, and we used the “Tone” function to assign different tones to different keys on the keyboard. We tried “B”& “C” these two keys and coded in Processing. However, when we tried for the first time, the tone didn’t change. We then discovered this very small mistake: we added one more semicolon after “if(key==’c’)” (as it can be seen from the screenshot below).

Code

After deleting this semicolon, the serial communication worked out fine and “B” “C” gave different tones as what we’ve set.

https://www.youtube.com/watch?v=4nUMODutgg0

 

The Processing code is in the box below. Here’s the Arduino Code that we used:

const int buzzer = 13;
int val;
void setup() {
Serial.begin(9600);
pinMode(buzzer,OUTPUT);
}
void loop() {
// receive a value from processing
while (Serial.available()) {
val = Serial.read();
}
// test the value
if (val == ‘b’) {
tone(buzzer,1000);
} else if (val == ‘q’) {
noTone(buzzer);
} else if (val==’c’) {
tone(buzzer,500);
}
delay(10); // wait for Processing
Serial.println(val);

}

Exercise 2: Use Arduino to tell Processing to do something

After finishing the first half of the assignment, we realized that the time left for the second half of the assignment was quite limited for us, as a result of debugging too much. So, we chose to use the potentiometer as the input device, and use the current it regulates as the input to control the radius of a circle drew by processing.

But we had some misunderstanding about the kind of data, that the function Serial.write() is able to receive. After correcting the mistake, which is to convert the value from float to int, we managed to let the program work.

https://youtu.be/XA9wdgiQUzM

Here’s the Arduino Code that we used:

int potpin=0;
void setup() {
Serial.begin(9600);
}

void loop() {
int val=analogRead(potpin);
int valu=map(val,0,1023,0,200);
Serial.write(valu);
delay(20);
Serial.println(valu);

}

 

// the Processing code of the first exercise
import processing.serial.*; 
Serial myPort; 
int val; 
void setup() { 
  printArray(Serial.list());
  // this prints out the list of all 
     // available serial ports on your computer.

  myPort = new Serial(this, Serial.list()[1], 9600); 
// Change the Serial.list()[0] to your port
  } 
void draw() { 
  // to send a value to the Arduino 
  if (keyPressed) { 
    if(key=='b'){
    myPort.write('b'); 
    }if(key=='c'){
    myPort.write('c');
    }
  } else { 
    myPort.write('q'); 
  } 
}


// the processing code for the second exercise
import processing.serial.*; 
int val = 0; // To store data from serial port 
Serial myPort;// The serial port object 
void setup() { 
size(500,500); 
printArray(Serial.list()); 
myPort = new Serial(this, Serial.list()[1], 9600);

}
void draw(){
  background(0);
  while ( myPort.available() > 0) { // If data is available,
    val = myPort.read(); // read it and store it in val
  }
  fill(255);
  ellipse(250,250,val,val);
}

Lab 6 – Serial Communication

Date: March 17th 2017

Instructor: Antonius

Partner: Emerald Dou

Aim/Goal:

  • Know how to exchange data between Processing and Arduino using serial communication.
  • Make a Processing sketch that tells Arduino to do something.
  • Make Arduino tell Processing to do something.

 

Materials needed

  • Wires; LED light (red, green); Breadboard; Arduino; temperature.

 

Exercises / Experiments

  1. Exercises one: Processing to Arduino
  • We going to make a Processing sketch that tells Arduino to do something first. So we decide to make a small animation that showing a small sheep go across the road. And the Arduino part is traffic light with red light and green light. At once we press the key, the green light would turn on and red light would turn off. (Red light would be always on except the green light turn on.) At once the green light turn on, the sheep would cross the street in the Processing sketch. After we make this decision, Emerald star to coding the sheep on the Ardrino and I star to build the traffic light. At the beginning, we trying to make a traffic light system for not only sheep bus also cars. However, we think the traffic light for cars is not necessary and the code for it is too difficult for us to combine it with Processing. Finally we make a traffic light system with only tow LED light.

IMG_6468

After that, we start to write the code to combine the Processing with Arduino. The first question we met is we don’t know which one is our serial Port, after Luis told us to add “printArray(Serial.list());” in our code, we can see that the port we connect is number 1. Therefore, the code should be “myPort = new Serial(this, Serial.list()[1], 9600); ” The second problem we met is we don’t know how to code to make the sheep run for a specific while. After we ask Antonius for help, we int “counter” for our code to make the sheep running continuously.

 

2. Arduino to Prcessing

For this exercise we are going to use temperature sensor to control a sun going up and going down. While the temperature going up, the sun would going up with it, and the color of the backgound would be red. First of all we connect the temperature sensor to the Arduino. And then we draw the sun on Processing.

3屏幕快照1 2

The problem we meet is we don’t know how to code that could make the sun going down. Because we use the “if…else if…”, at once the sensor value is bigger than 50, the sun will going up and can’t be stopped. I will edit this documentation after we solve this question.

1. Processing to Arduino

<proccessing>
import processing.serial.*;
Serial myPort;
int val;
int digitalReadFromArduino;
int counter = 0;
int x = 0;
int y;
void setup(){
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[1], 9600);  
  size(1000,300);
  
}

void draw(){
  background(196,171,156);
  noStroke();
  //face
  fill(255);
  rect(800+x,100,50,10); 
  rect(820+x,110,10,40);
  rect(810+x,120,30,10);
  //horn
  fill(118,106,98);
  rect(810+x,80,10,20);
  rect(830+x,80,10,20);
  //eyes
  fill(0);
  rect(810+x,110,10,10);
  rect(830+x,110,10,10);
  //body
  fill(255);
  rect(840+x,110,70,30);
  //tail
  rect(910+x,130,10,10);
  //legs
  rect(860+x,140,10,30);
  rect(900+x,140,10,30);
  fill(214,207,203);
  rect(840+x,140,10,30);
  rect(850+x,140,10,10);
  rect(870+x,140,30,10);
  rect(880+x,150,10,20);
  rect(830+x,130,10,10);
  //feet
  fill(118,106,98);
  rect(840+x,170,10,10);
  rect(860+x,170,10,10);
  rect(880+x,170,10,10);
  rect(900+x,170,10,10);
  
  
  //press key - traffic light change
  if (keyPressed) {
    //myPort.write('H');
    counter = 300;
    //x--;
  } else {
    //myPort.write('L');  
  }
  if (counter > 0){
    counter--;
    x--;
    myPort.write('H');
    println(counter);
  } else {
    myPort.write('L'); 
  }
  while (myPort.available() > 0);{
  digitalReadFromArduino = myPort. read();
}

}

<Arduino>
int sheepRed = 12; //assign the sheep lights

int sheepGreen = 10;
int val;

int crossTime =8000; //time for sheep to cross
unsigned long changeTime;//time since key pressed

void setup() {
Serial.begin(9600);

pinMode(sheepRed, OUTPUT);

pinMode(sheepGreen, OUTPUT);
digitalWrite(sheepRed, HIGH); //turn on the red lights

}

void loop() {
// receive a value from processing
while (Serial.available()) {
val = Serial.read();
}

// test the value
if (val == 'H') {
digitalWrite(10, HIGH);
digitalWrite(12, LOW);

} else if (val == 'L') {
digitalWrite(10, LOW);
digitalWrite(12, HIGH);
}
delay(10); // wait for Processing

}

2. Arduino to processing.
 
<processing>
import processing.serial.*;
Serial myPort;
int valueFromArduino;
int y=50;
int g = 0;
int b = 0;
int x = 150;
void setup(){
  size(300,300);
  printArray(Serial.list());
  myPort = new Serial(this, Serial.list()[1], 9600);
}


void draw(){
  background(245,198+g,198+b);
  noStroke();
  fill(255,234,0);
  //sun - circle
  rect(130,90+x,40,10);
  rect(110,100+x,80,10);
  rect(100,110+x,100,20);
  rect(90,130+x,120,40);
  rect(100,170+x,100,20);
  rect(110,190+x,80,10);
  rect(130,200+x,40,10);
  //light
  rect(140,50+x,20,30);
  rect(140,220+x,20,30);
  rect(50,140+x,30,20);
  rect(220,140+x,30,20);
  //left - up
  rect(100,60+x,10,10);
  rect(90,70+x,10,10);
  rect(100,70+x,20,20);
  
  rect(70,90+x,10,10);
  rect(60,100+x,10,10);
  rect(70,100+x,20,20);
  //right - up
  rect(190,60+x,10,10);
  rect(180,70+x,20,20);
  rect(200,70+x,10,10);
  
  rect(220,90+x,10,10);
  rect(210,100+x,20,20);
  rect(230,100+x,10,10);
  //left - down
  rect(70,200+x,10,10);
  rect(60,190+x,10,10);
  rect(70,180+x,20,20);
  
  rect(100,230+x,10,10);
  rect(90,220+x,10,10);
  rect(100,210+x,20,20);
  //right - down
  rect(220,200+x,10,10);
  rect(210,180+x,20,20);
  rect(230,190+x,10,10);
  
  rect(190,230+x,10,10);
  rect(180,210+x,20,20);
  rect(200,220+x,10,10);
  //eyes / mouth
  fill(0);
  rect(130,110+x,5,10);
  rect(165,110+x,5,10);
  
  rect(145,125+x,10,2);
  rect(143,123+x,2,2);
  rect(155,123+x,2,2);
  
  while (myPort.available()>0){
    valueFromArduino = myPort.read();
  }
  if (valueFromArduino  > 52){
    x++;
    g++;
    b++;
  }else if (valueFromArduino < 52){
    x--;
    g--;
    b--;
  }
}

<Arduino>
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(0);
  // print out the value you read:
  Serial.println(sensorValue);
  delay(1);        // delay in between reads for stability
}

Lab6: Serial Communication

Date: March 17, 2017

Instructor: Antonius

Partner: Yun Huang

Goal: The goal of this lab is to exchange data between Processing and Arduino using serial communication, which means using Arduino to control things in Processing and also using Processing to control things in Arduino.

Exercise 1- Processing to Arduino

Materials needed: Two LEDs (one red, one green), Arduino, breadboard, wires.

At the very beginning I drew a sheep with Processing. So then based on this sheep, we thought of the idea about traffic lights and sheep. The red LED is always on, and when key pressed, the red LED will be off and the green one will be on for five seconds. The sheep starts moving when the green light on, and stops moving when the lights turn red again.

IMG_0462 IMG_0463

However, when really doing this, we came across a problem that we didn’t know how to control the time the sheep runs. Our thought is to write a code such that if key is pressed, the variable x will decrease (for the sheep is moving to the left) until it reaches to 5 seconds. But we found the code was hard to write according to this because we didn’t figure out how to tell the computer the time. We then asked Antonius for help, and he told us to set a time counter in Processing, which means when key is pressed, the value of the variable counter will quickly change from 0 to 300 (because the frame rate is 60 per second), and then begin to reduce. When the value reduces to 0, which refers to 5 seconds, the light turns red and the sheep stops.

 

*Processing code:

import processing.serial.*;
Serial myPort;
int val;
int counter = 0;
int x = 0;

void setup(){
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[1], 9600);
size(1000,300);

}

void draw(){
background(196,171,156);
noStroke();
//face
fill(255);
rect(800+x,100,50,10);
rect(820+x,110,10,40);
rect(810+x,120,30,10);
//horn
fill(118,106,98);
rect(810+x,80,10,20);
rect(830+x,80,10,20);
//eyes
fill(0);
rect(810+x,110,10,10);
rect(830+x,110,10,10);
//body
fill(255);
rect(840+x,110,70,30);
//tail
rect(910+x,130,10,10);
//legs
rect(860+x,140,10,30);
rect(900+x,140,10,30);
fill(214,207,203);
rect(840+x,140,10,30);
rect(850+x,140,10,10);
rect(870+x,140,30,10);
rect(880+x,150,10,20);
rect(830+x,130,10,10);
//feet
fill(118,106,98);
rect(840+x,170,10,10);
rect(860+x,170,10,10);
rect(880+x,170,10,10);
rect(900+x,170,10,10);
//traffic lights
fill(0);
rect(45,20,70,130);
rect(75,150,10,70);
fill(149,85,85);
ellipse(80,52.5,50,50);
fill(92,173,98);
ellipse(80,110,50,50);
//press key – traffic light change
if (keyPressed) {
counter = 300;
} else {

}
if (counter > 0){
counter–;
x–;
myPort.write(‘H’);
println(counter);
} else {
myPort.write(‘L’);
}
}

*Arduino code:

int sheepRed = 12; //assign the sheep lights

int sheepGreen = 10;
int val;

int crossTime =8000; //time for sheep to cross
unsigned long changeTime;//time since key pressed

void setup() {
Serial.begin(9600);

pinMode(sheepRed, OUTPUT);

pinMode(sheepGreen, OUTPUT);
digitalWrite(sheepRed, HIGH); //turn on the red lights

}

void loop() {
// receive a value from processing
while (Serial.available()) {
val = Serial.read();
}

// test the value
if (val == ‘H’) {
digitalWrite(10, HIGH);
digitalWrite(12, LOW);

} else if (val == ‘L’) {
digitalWrite(10, LOW);
digitalWrite(12, HIGH);
}
delay(10); // wait for Processing

}

Exercise 2 – Arduino to Processing

Materials needed: Arduino shell, Arduino, LM35 Temperature Sensor, wires.

This time we want to use the temperature sensor in Arduino to control the sunrise and sunset in Processing. If the temperature the sensor senses increases, the sun will rise and the background color will change from pink to red. Otherwise, the sun sets and the background colors turns light blue.

sun1 sun2 sun3 sun4 sun5

When doing this we also encountered problems. We found our sun wouldn’t go down if it went up first, and also wouldn’t go up if it went down first. It trouble us a lot, but lately we figured out that it was because of the if statement. Once it meets the first condition, it won’t consider the second. As a result, we considered about several possible ways to solve it. For example, insert two variables and keep restoring the sensor values to those variables, then compare their difference to 0. But we still haven’t figure out how to realize it yet, and the things we already have are as follows.

*Processing code:

import processing.serial.*;
Serial myPort;
int valueFromArduino;

int g = 0;
int b = 0;
int x = 150;
void setup(){
size(300,300);
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[1], 9600);
}
void draw(){
background(245,198+g,198+b);
noStroke();
fill(255,234,0);
//sun – circle
rect(130,90+x,40,10);
rect(110,100+x,80,10);
rect(100,110+x,100,20);
rect(90,130+x,120,40);
rect(100,170+x,100,20);
rect(110,190+x,80,10);
rect(130,200+x,40,10);
//light
rect(140,50+x,20,30);
rect(140,220+x,20,30);
rect(50,140+x,30,20);
rect(220,140+x,30,20);
//left – up
rect(100,60+x,10,10);
rect(90,70+x,10,10);
rect(100,70+x,20,20);

rect(70,90+x,10,10);
rect(60,100+x,10,10);
rect(70,100+x,20,20);
//right – up
rect(190,60+x,10,10);
rect(180,70+x,20,20);
rect(200,70+x,10,10);

rect(220,90+x,10,10);
rect(210,100+x,20,20);
rect(230,100+x,10,10);
//left – down
rect(70,200+x,10,10);
rect(60,190+x,10,10);
rect(70,180+x,20,20);

rect(100,230+x,10,10);
rect(90,220+x,10,10);
rect(100,210+x,20,20);
//right – down
rect(220,200+x,10,10);
rect(210,180+x,20,20);
rect(230,190+x,10,10);

rect(190,230+x,10,10);
rect(180,210+x,20,20);
rect(200,220+x,10,10);
//eyes / mouth
fill(0);
rect(130,110+x,5,10);
rect(165,110+x,5,10);

rect(145,125+x,10,2);
rect(143,123+x,2,2);
rect(155,123+x,2,2);

while (myPort.available()>0){
valueFromArduino = myPort.read();
}
if (valueFromArduino > 52){
x++;
g++;
b++;
}
if (valueFromArduino < 52){
x–;
g–;
b–;
}

}

*Arduino code:

void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}

Lab 6 Documentation by Ryan

Instructor: Marcela

This week we took our first step into working with Arduino and Processing. We had to use libraries in Processing which allowed Processing to communicate with the Arduino.  Referring to the code that was used in our classes, as well as some of a previous project’s work.

IMG_3325

Using the potentiometer, the user can manipulate the size of the circle generated by processing. The circle uses colours from early smiley face project. Here’s a video of it in action:

That’s all for now!

 

[Arduino]
void setup() {
  Serial.begin(9600);
}


void loop() {
  int sensorValue = analogRead(A0);
  Serial.write(sensorValue);

  // too fast communication might cause some latency in Processing
  // this delay resolves the issue.
  delay(10);
}

[End of Arduino]

[Processing]
void setup() {
  size(500, 500);
  background(0);

  printArray(Serial.list());
  
  myPort = new Serial(this, Serial.list()[ 3 ], 9600);
}


void draw() {
  // to read the value from the Arduino
  while ( myPort.available() > 0) {
    valueFromArduino = myPort.read();
  }
  // println(valueFromArduino);
  fill(255, 255, 153);
  ellipse(250, 250, valueFromArduino, valueFromArduino);
}

[End of Processing]

Stoplight Lab – Lab 2

Instructor: Marcela

Class: Interaction Lab

Objective:

The purpose of this lab was to learn how to use the Arduino with the breadboard to create a circuit that we could control from our computer. We learned how to ground the circuit and how to use digital and analog pins as well as the 5v pin.

Process:

I started out by connecting three wires, one for each of the main lights of the stoplight. Then, I connected the resistors. I also used a button as an input to be used as the alert for someone crossing the road. After that, I set up the remaining two LEDs with resistors and the wiring was complete.

Since the code was provided to use for this lab, it was very easy because all we had to do was make the circuit.

17409705_968895143244816_1798717272_n

17409815_968895146578149_551211080_n17439808_968895153244815_1898274927_n

Next we had to make a circuit in which we controlled an RGB  LED with variable resistors.

Ir was a similar concept except this time, we had three inputs to control one output. The hardest part was getting the orientation of the RGB light correct since it is hard to tell which prong does what. Eventually we got it working, and using the code provided to us, we had fun playing around with the different lights.

17409978_968895159911481_1866352975_n 17439645_968895156578148_1616170394_n

Result: This was a fairly simple lab but it helped establish some very fundamental concepts about circuits. We also learned how to use different components such as variable resistors.