Luobin_Homework

A simple Arduino – Processing communication project.

The 3 LEDs are controlled by a button switch. The potentiometer is used to control one of the green lights, and send serial data to processing. Processing simply uses the data to draw a dynamic sphere on the screen.

 

P1_LBW

 

 

ARDUINO CODE

————————————————————

int switchState = 0;

void setup(){
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(2, INPUT);
pinMode(A1, INPUT);
Serial.begin(9600);
}
void loop(){

switchState = digitalRead(2);
float wait = map(analogRead(A1), 0, 1024, 0, 100);
Serial.println(analogRead(A1));

if(switchState == LOW){

digitalWrite(3, HIGH);
delay(wait);
digitalWrite(3, LOW);
delay(wait);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}
else{
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);

delay(250);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(250);

}
}

 

PROCESSING CODE

___________________________________________________

import processing.serial.*;
Serial myPort;
String val;
float rad;
void setup()
{
size(500, 500, P2D);
frameRate(60);

// serial example from processing
// I know that the first port in the serial list on my mac
// is Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you’re using.
String portName = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
myPort = new Serial(this,”COM3″, 9600);
}
void draw()
{
background(0);
noStroke();
fill(0, rad, 0);
if ( myPort.available() > 0)
{ // If data is available,

//try read port data, failed
// float inByte = myPort.read();
println(rad);
val = myPort.readStringUntil(‘\n’);

//exclude null for result
//if not, will return null pointer exception
if(val != null){
rad = float(val);
}

map(rad, 0, 1024, 0, 255);
ellipse(rad, rad, rad, rad);

}
//print it out in the console

// rad = float(val);

}

 

Leave a Reply

Your email address will not be published.