Job Sheet P2 - Antarmuka Arduino Dan Processing
Job Sheet P2 - Antarmuka Arduino Dan Processing
Tujuan Praktek :
Mahasiswa mampu membuat aplikasi antarmuka Arduino dan Processing.
Daftar Perangkat/Komponen :
No
Perangkat/Komponen
Jumlah
No
Perangkat/Komponen
Jumlah
Komputer/Laptop
RGB LED
Protoboard
Potensiometer 10k
Kabel jumper
Push Button
Secukupnya
Processing
// Sketch P2.1p : Baca data pot dan ubah warna
// layar Processing
// Copyright 2013 Jeremy Blum
//Import and initialize serial port library
import processing.serial.*;
Serial port;
float brightness = 0; //For holding value from pot
void setup()
{
size(500,500); //Window size
port = new Serial(this,"/dev/ttyACM0",9600);
port.bufferUntil('\n'); //Setup port to read
}
void draw()
{
background(0,0,brightness); //Updates the window
}
void serialEvent (Serial port)
{
//Gets val
brightness = float(port.readStringUntil('\n'));
}
Processing
void loop()
{
//Keep working as long as data is in buffer
while (Serial.available() > 0)
void setup()
{
size(640,256); //Size of HSV image
img = loadImage("hsv.jpg"); //Load in back image
//Open serial port
port = new Serial(this, "/dev/ttyACM0", 9600);
}
void draw()
{
background(0); //Black Background
image(img,0,0); //Overlay image
}
void mousePressed()
{
color c = get(mouseX, mouseY); //Get the RGB
// color where mouse was pressed
String colors = int(red(c))+","+int(green(c))
+","+int(blue(c))+"\n"; //extract values from clr.
print(colors); //print colors for debugging
Processing
import processing.serial.*;
Serial myPort; // Create object from Serial
int state = 31;
int fire = 1;
int right = 1;
int up = 1;
int left = 1;
int down = 1;
int x = 275;
int y = 275;
int c = 0;
void setup()
{
size(600, 600);
myPort=new Serial(this,"/dev/ttyACM0",9600);
}
void draw()
{
if(myPort.available() > 0)
{
state = myPort.read();
println(state);
println(binary(state));
fire = state & 1;
right = (state & 2) >> 1;
up = (state & 4) >> 2;
left = (state & 8) >> 3;
down = (state & 16) >> 4;
print(fire);
print(right);
print(up);
print(left);
println(down);
c = (fire == 0) ? 250 : 0;
if(right == 0 && left == 1)
state5 = val5;
/*
we create a 8 bit word (1 byte) whose 5
right bit represent from right to left
buttons fire, right, up, left, down status:
0 if pressed, 1 if not pressed
remaining bits are unused
I'm not that expert in bit operations,
there's probably a
better way (faster) to do the following.
*/
}
if(up == 0 && down == 1)
{
y = y - 2;
}
if(left == 0 && right == 1)
{
x = x - 2;
}
if(down == 0 && up == 1)
{
y = y + 2;
}
Serial.write(state);
}
x = x + 2;
int switchPin = 7;
int LEDPin = 13;
void setup()
{
pinMode (LEDPin, OUTPUT);
Processing
import processing.serial.*;
Serial port;
int val = 0;
int oldval = 0;
int numFrames = 15; // The number of frames
int frame = 0;
digitalWrite(switchPin, HIGH);
Serial.begin(9600);
void loop()
{
if (digitalRead(switchPin) == HIGH)
{
Serial.write(0);
digitalWrite(LEDPin, LOW);
}
else
{
Serial.write(1);
digitalWrite(LEDPin, HIGH);
}
delay(100);
}
}
image(images[frame], 0, 0);
oldval = val;
Processing
void setup()
{
Serial.begin(9600);
}
void loop()
import processing.serial.*;
val1 = analogRead(potPin1);
val2 = analogRead(potPin2);
Serial.print("A");
// Example identifier for sensor 1,
//send as character
Serial.print(val1);
// send sensor 1 value as decimal number
Serial.write(10);
// send ASCII string "10"
// ASCII 10 = newline character,
// used to separate the data strings
Serial.print("B");
// Example identifier for sensor 2,
//send as character
Serial.print(val2);
// send sensor 2 value as decimal number
Serial.write(10);
// send ASCII string "10"
// ASCII 10 = newline character,
// used to separate the data strings
void draw()
{
fill(0,2); // use black with alpha 2
rectMode(CORNER);
rect(0,0,width,height);
while (port.available() > 0)
{
serialEvent(port.read());
}
point(xCoordinate, yCoordinate);
}
void serialEvent(int serial)
{
if(serial != NEWLINE)
buff += char(serial);
}
else
{
if (buff.length() > 1)
{
temp = buff.substring(0, buff.length()(buff.length()-1));
// character of the sensor identified
if(temp.equals("A") == true)
{
//sensor A value
temp = buff.substring(1,buff.length());
temporary = float(temp);
xCoordinate = width/(1024/temporary);
println(xCoordinate);
}
if(temp.equals("B") == true)
{
//sensor B value
temp = buff.substring(1,buff.length());
temporary = float(temp);
yCoordinate = height/(1024/temporary);
println(yCoordinate);
}
// Clear the value of "buff"
buff = "";
}
}
Do-It-Yourself(DIY) :
Buatlah sendiri aplikasi antarmuka Arduino dan Processing..
Isilah Log Book dengan gambar skematik dan analisis sketch-nya.