In this tutorial we will learn how to connect Arduino to Processing.
We will to communication using the Serial Port.
We will use the Processing Development Environment to control the Arduino.
This is the first the Processing tutorial.
When we will press on the left and right button the LED turn on and turn off.
---
Hardware Required:
Arduino Board - https://goo.gl/Rqc5w2
LED (light emitting diode) - https://goo.gl/CPAXXf
Resistor 220ohm - https://goo.gl/FVgEyR
Jumper Wire - https://goo.gl/VRzUN4
Breadboard - https://goo.gl/GjpqVW
Recomended Site - https://goo.gl/HDkZBt
---
Download the Processing:
---
Get the Arduino Source Code:
const int ledPin = 13; //define the LED pin
void setup(){
pinMode(ledPin, OUTPUT); //initialize LED output
Serial.begin(9600); //start the serial communication
}
void loop(){
if(Serial.available() > 0){ //if some data is available of in the serial port
char ledPinState = Serial.read(); //read the value
if(ledPinState == '1'){ //if statement will be true(1)
digitalWrite(ledPin, HIGH); //turn ON the LED
}
if(ledPinState == '0'){ //if statement will be false(0)
digitalWrite(ledPin, LOW); //turn OFF the LED
}
}
}
Get the Processing Source Code:
import processing.serial.*; //import the library for a serial communication (sketch-import library-serial)
Serial myPort; //define name of the serial communication
PImage bg; //add the background image.Declare variable "bg" of type PImage
void setup(){ //we will set a resolution for the graphical window
size(259, 194); //set the image size 259 by 194 pixels
bg=loadImage ("redLED.png"); //upload the image into the program
//the image file must be in the data folder of the current sketch to load successfully
myPort=new Serial(this, "COM6", 9600); //se the name of our communication port (Arduino COM port)
}
void draw(){ //draw function - here is graphical screen
background(bg); //set the background image
if(mousePressed && (mouseButton == LEFT)){ //get the value of the mouse for the LED turn ON/OFF
myPort.write('1'); //if pressed the left buton on the screen we will send to the serial port character 1
}
if(mousePressed && (mouseButton == RIGHT)){
myPort.write('0'); //the led turn off
}
}