0% found this document useful (0 votes)
40 views13 pages

Lapres 3

The document describes 3 experiments controlling LEDs using Arduino and Processing: 1. Controlling 8 LEDs by moving the mouse horizontally in Processing and sending values to Arduino. 2. Controlling 4 LEDs in Processing by clicking on buttons, which send character values to Arduino to turn individual LEDs on/off. 3. Reading character values from a text file in Processing and sending them to Arduino to light up the corresponding LED.

Uploaded by

Dicky Ihza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views13 pages

Lapres 3

The document describes 3 experiments controlling LEDs using Arduino and Processing: 1. Controlling 8 LEDs by moving the mouse horizontally in Processing and sending values to Arduino. 2. Controlling 4 LEDs in Processing by clicking on buttons, which send character values to Arduino to turn individual LEDs on/off. 3. Reading character values from a text file in Processing and sending them to Arduino to light up the corresponding LED.

Uploaded by

Dicky Ihza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Nama Anggota : Dicky Ihza Permana P.

/ 1210191045

Pradono Kristio Putro / 1210191053

Moch. Raditya A.A. / 1210191059

Kelas : 3 D4 TB

Mata Kuliah / Dosen : Praktikum Mikroprosesor dan Antar Muka 2 / Moh. Ridwan

Tanggal : Senin, 13 September 2021

PRAKTIKUM 3

KONTROL MULTI LED MENGGUNAKAN ARDUINO DAN PROCESSING

A. Percobaan

1. Menyalakan 8 LED dengan menggeser mouse ke arah horizontal.

a. Rangkaian
b. Listing Code Arduino

int ledPins[] = {2,3,4,5,6,7,8,9};


void setup()
{
Serial.begin(9600);
for(int i = 0; i < 8; i++){
pinMode(ledPins[i],OUTPUT);
}
}
void loop() {
byte byteRead;
if (Serial.available()) {
byteRead = Serial.read();
byteRead=byteRead-'0';
for(int i=2; i<10; i++){
digitalWrite(i, LOW);
}
if(byteRead>0){
for(int i=1; i<(byteRead+1); i++){
digitalWrite(i+1, HIGH);
}
}
}
}

c. Listing Code Processing

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

b. Listing Code Arduino

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);
}
}

c. Listing Code Processing

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

- Button Yellow ON → LED Yellow ON

- Button Red ON → LED Red ON

- Button Green ON → LED Green ON


- Button Blue ON → LED Blue ON

- All ON
3. Menyalakan 8 LED dengan data dari file teks (txt).

a. Rangkaian

b. Listing Code Arduino

int ledPins[] = {2,3,4,5,6,7,8,9};


void setup()
{
Serial.begin(9600); for(int i = 0; i < 8; i++){
pinMode(ledPins[i],OUTPUT);
}
}
void loop() {
byte byteRead;
if (Serial.available()) {
byteRead = Serial.read();
byteRead=byteRead-'0';
if(byteRead==0){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
}
if(byteRead>0){
digitalWrite((byteRead+1), HIGH);
}
}

c. Listing Code Processing

import processing.serial.*; Serial comPort;


int counter=0;
int numItems=0;
boolean sendStrings=false;
StringLoader sLoader;
void setup(){
comPort = new Serial(this, "COM1", 9600);
background(255,0,0);
}
void draw(){
}
void mousePressed() {
sendStrings=!sendStrings;
if(sendStrings){
background(0,255,0);
sLoader=new StringLoader();
sLoader.start();
}
else{
background(255,0,0);
counter=0;
}
}
public class StringLoader extends Thread{
public StringLoader(){ }
public void run() {
String textFileLines[]=loadStrings("Data.txt");
String lineItems[]=splitTokens(textFileLines[0],",");
numItems=lineItems.length;
for(int i = counter; i<numItems; i++){
comPort.write(lineItems[i]);
delay(500);
comPort.write("0");
}
counter=numItems;
}
}

d. Output

You might also like