Learning Arduino for beginners EP#34 using processing with your PC to control your Arduino

In this tutorial we will take a look at how you can use your computer with processing to control your Arduino project…

click on video below for full tutorial

 

Processing Code:

import controlP5.*; //library
import processing.serial.*; //library
Serial port; //do not change
ControlP5 cp5; //create ControlP5 object
PFont font; //do not change

void setup() { //same as arduino program

size(300, 300); //window size, (width, height)
port = new Serial(this, “COM3”, 9600); //connected arduino port
cp5 = new ControlP5(this); //do not change
font = createFont(“Georgia Bold”, 20); //font for buttons and title

cp5.addButton(“ON”) //name of button
.setPosition(95, 50) //x and y upper left corner of button
.setSize(120, 70) //(width, height)
.setFont(font) //font
.setColorBackground(color(255, 0, 0)) //background r,g,b
.setColorForeground(color(0, 255, 0)) //mouse over color r,g,b
.setColorLabel(color(0, 0, 0)) //text color r,g,b
;

cp5.addButton(“OFF”)
.setPosition(95, 150)
.setSize(120, 70)
.setFont(font)
.setColorBackground(color(255, 0, 0))
.setColorForeground(color(0, 255, 0))
.setColorLabel(color(0, 0, 0))
;
}

void draw() {

background(0, 0, 0); // background color of window (r, g, b)

}

void ON() {
port.write(1);
}

void OFF() {
port.write(2);
}

 

ARDUINO Code:

void setup() {

pinMode(13, OUTPUT); //set pin as output , red led

Serial.begin(9600); //start serial
}

void loop(){

if(Serial.available()){ //id data available

int val = Serial.read();

if(val == 1){ //if 1 received
digitalWrite(13, HIGH); //turn on
}
if(val == 2){ //if 2 received
digitalWrite(13, LOW); //turn off
}

}
}

Advertisements