21. Controlling arduino by processing
การควบคุมบอร์ด Arduino ด้วยโปรแกรม Processing ผ่านทางพอร์ทอนุกรม
คลิปวิดีโอการใช้งาน
รายละเอียดโปรแกรม
โปรแกรม Arduino
#define L1_ON digitalWrite(4,LOW)
#define L1_OFF digitalWrite(4,HIGH)
#define L2_ON digitalWrite(5,LOW)
#define L2_OFF digitalWrite(5,HIGH)
#define L3_ON digitalWrite(6,LOW)
#define L3_OFF digitalWrite(6,HIGH)
#define L4_ON digitalWrite(7,LOW)
#define L4_OFF digitalWrite(7,HIGH)
void setup() {
Serial.begin(9600); // initialize serial:
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
L1_OFF;
L2_OFF;
L3_OFF;
L4_OFF;
}
void loop() {
/* L1_ON;
delay(300);
L2_ON;
delay(300);
L3_ON;
delay(300);
L4_ON;
delay(300);
L1_OFF;
delay(300);
L2_OFF;
delay(300);
L3_OFF;
delay(300);
L4_OFF;
delay(300);
*/
}
void serialEvent() {
char inChar = (char)Serial.read();
//int inChar = Serial.read();
switch (inChar) {
case '0': L1_OFF; break;
case '1': L1_ON; break;
case '2': L2_OFF; break;
case '3': L2_ON; break;
case '4': L3_OFF; break;
case '5': L3_ON; break;
case '6': L4_OFF; break;
case '7': L4_ON; break;
default: break;
}
}
-------------------------------------------------------------------------------------
โปรแกรมบนพีซีใช้โปรแกรม Processing
//--- define variable and constant
import processing.serial.*;
ImageButtons Button1,Button2,Button3,Button4;
PImage Lamp_On, Lamp_Off;
Serial myPort;
void setup() {
size(350, 300);
println(Serial.list());
myPort = new Serial(this, Serial.list()[1], 9600); // [1]
smooth();
background(255); // Set the background to black
// Define and create image button
PImage sw_on = loadImage("sw_on.JPG");
PImage sw_off = loadImage("sw_off.JPG");
int x = width/20;
int y = height - (sw_on.height)-50;
int w = sw_on.width;
int h = sw_on.height;
Button1 = new ImageButtons(x, y, w, h, sw_on, sw_off,"0","1");
Button2 = new ImageButtons(x+80, y, w, h, sw_on, sw_off,"2","3");
Button3 = new ImageButtons(x+160, y, w, h, sw_on, sw_off,"4","5");
Button4 = new ImageButtons(x+240, y, w, h, sw_on, sw_off,"6","7");
stroke(255); // Set stroke color to white
println("Test");
textSize(14);
fill(0, 0, 0) ;
text("LED_1 LED_2 LED_3 LED_4", x+15, y-10);
}
void draw()
{
Button1.update(); Button1.display();
Button2.update(); Button2.display();
Button3.update(); Button3.display();
Button4.update(); Button4.display();
}
class Button
{
int x, y;
int w, h;
boolean over = false;
boolean pressed = false;
void pressed() {
if(over && mousePressed) {
pressed = true;
} else {
pressed = false;
}
}
boolean overRect(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
}
//-------------------------------------------------------
class ImageButtons extends Button
{
PImage img_on;
PImage img_off;
PImage down;
PImage currentimage;
String textoff;
String texton;
ImageButtons(int ix, int iy, int iw, int ih, PImage button_on, PImage button_off,String t_off, String t_on)
{
x = ix;
y = iy;
w = iw;
h = ih;
img_on = button_on;
img_off = button_off;
currentimage = img_off;
textoff = t_off;
texton = t_on;
}
void update(){
over();
pressed();
if(pressed)
{
if(currentimage==img_on)
{
currentimage=img_off;
println(textoff);
myPort.write(textoff);
}
else
{
currentimage=img_on;
println(texton);
myPort.write(texton);
}
delay(200);
}
}
void over(){
if( overRect(x, y, w, h) ) {
over = true;
} else {
over = false;
}
}
void display() {
image(currentimage, x, y);
}
}