Lapres 3
Lapres 3
/ 1210191045
Kelas : 3 D4 TB
Mata Kuliah / Dosen : Praktikum Mikroprosesor dan Antar Muka 2 / Moh. Ridwan
PRAKTIKUM 3
A. Percobaan
a. Rangkaian
b. Listing Code Arduino
import processing.serial.*;
int new_sX, old_sX;
int nX, nY; Serial
myPort; void
setup(){ size( 800,400 );
strokeWeight( 10 );
myPort = new Serial(this, "COM4", 9600);
myPort.bufferUntil('\n');
}
void draw(){
background( 100 );
stroke(255);
ellipse( nX, nY, 10, 10 );
line(nX,0,nX,height);
}
void mouseMoved(){
nX = mouseX;
nY = mouseY;
new_sX=(int)map(nX,0,800,0,10);
if(new_sX==old_sX){
} else {
old_sX = new_sX;
myPort.write(""+new_sX);
}
}
d. Output
2. Menyalakan 4 LED dengan kontrol processing.
a. Rangkaian
int incomingByte = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if (Serial.available() < 1) {
return;
}
incomingByte = Serial.read();
// LED-1
if (incomingByte == 65) {
digitalWrite(2, HIGH);
}
if (incomingByte == 66) {
digitalWrite(2, LOW);
}
//LED-2
if (incomingByte == 67) {
digitalWrite(3, HIGH);
}
if (incomingByte == 68) {
digitalWrite(3, LOW);
}
//LED-3
if (incomingByte == 69) {
digitalWrite(4, HIGH);
}
if (incomingByte == 70) {
digitalWrite(4, LOW);
}
//LED-4
if (incomingByte == 71) {
digitalWrite(5, HIGH);
}
if (incomingByte == 72) {
digitalWrite(5, LOW);
}
}
import processing.serial.*;
Serial myPort;
int val;
int ButtonValueY = 0;
int ButtonValueR = 0;
int ButtonValueG = 0;
int ButtonValueB = 0;
int clickflag = 0;
void setup() {
size(480, 500);
smooth();
String portName = Serial.list()[0];
myPort = new Serial(this, "COM4", 9600);
fill(150, 150, 0);
rect(100, 100, 50, 50);
fill(150, 0, 0);
rect(100, 200, 50, 50);
fill(0, 150, 0);
rect(100, 300, 50, 50);
fill(0, 0, 150);
rect(100, 400, 50, 50);
}
void draw() {
if (mousePressed) {
clickflag = 0;
return;
}
if (clickflag == 1)
{
return;
}
clickflag = 1;
if ((mouseX > 100) && (mouseX < 150) && (mouseY > 100)
&& (mouseY < 150))
{
if (ButtonValueY == 0) {
ButtonValueY = 1;
fill(255, 255, 0);
myPort.write('A');
rect(100, 100, 50, 50);
} else {
fill(150, 150, 0);
ButtonValueY = 0;
myPort.write('B');
rect(100, 100, 50, 50);
}
}
if ((mouseX > 100) && (mouseX < 150) && (mouseY > 200)
&& (mouseY < 250))
{
if (ButtonValueR == 0) {
ButtonValueR = 1;
fill(255, 0, 0);
myPort.write('C');
rect(100, 200, 50, 50);
} else {
fill(150, 0, 0);
ButtonValueR = 0;
myPort.write('D');
rect(100, 200, 50, 50);
}
}
if ((mouseX > 100) && (mouseX < 150) && (mouseY > 300)
&& (mouseY < 350))
{
if (ButtonValueG == 0) {
ButtonValueG = 1;
fill(0, 255, 0);
myPort.write('E');
rect(100, 300, 50, 50);
} else {
fill(0, 150, 0);
ButtonValueG = 0;
myPort.write('F');
rect(100, 300, 50, 50);
}
}
if ((mouseX > 100) && (mouseX < 150) && (mouseY > 400)
&& (mouseY <450))
{
if (ButtonValueB == 0) {
ButtonValueB = 1;
fill(0, 0, 255);
myPort.write('G');
rect(100, 400, 50, 50);
} else {
fill(0, 0, 150);
ButtonValueB = 0;
myPort.write('H');
rect(100, 400, 50, 50);
}
}
}
d. Output
- All ON
3. Menyalakan 8 LED dengan data dari file teks (txt).
a. Rangkaian
d. Output