Indice
Descrizione
In questa puntata del tutorial di Processing, scopriremo come far comunicare gli sketch di Processing con i progetti che utilizzano la scheda di Arduino. Vedremo quindi degli esempi di come inviare i messaggi in entrambe le direzioni, sia da Processing ad Arduino che viceversa.
import processing.serial.*;
Serial port;
boolean ledOn = false;
void setup() {
size(500, 300);
//printArray(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
}
void draw() {
background(190);
if (mousePressed && !ledOn) {
port.write("on\n");
ledOn = true;
} else if (!mousePressed && ledOn) {
port.write("off\n");
ledOn = false;
}
if (ledOn) {
fill(230, 0, 0, 200);
} else {
fill(255, 0, 0, 80);
}
stroke(50);
arc(150, 80, 70, 70, -PI, 0);
noStroke();
rect(115, 80, 70, 50);
stroke(50);
line(115, 80, 115, 130);
line(185, 80, 185, 130);
rect(100, 130, 100, 20);
fill(127);
rect(130, 150, 10, 100);
rect(160, 150, 10, 80);
}
#define LED_PIN 9
void setup(){
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
}
void loop(){
if(Serial.available()){
String command = Serial.readStringUntil('\n');
digitalWrite(LED_PIN, command == "on");
}
delay(50);
}
import processing.serial.*;
Serial port;
boolean ledOn = false;
int counter = 0;
PFont font;
void setup() {
size(500, 300);
//printArray(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
font = createFont("Arial", 100);
}
void draw() {
background(190);
if (mousePressed && !ledOn) {
port.write("on\n");
ledOn = true;
counter++;
} else if (!mousePressed && ledOn) {
port.write("off\n");
ledOn = false;
}
if (ledOn) {
fill(230, 0, 0, 200);
} else {
fill(255, 0, 0, 80);
}
stroke(50);
arc(150, 80, 70, 70, -PI, 0);
noStroke();
rect(115, 80, 70, 50);
stroke(50);
line(115, 80, 115, 130);
line(185, 80, 185, 130);
rect(100, 130, 100, 20);
fill(127);
rect(130, 150, 10, 100);
rect(160, 150, 10, 80);
if (port.available() > 0) {
String command = port.readStringUntil('\n');
if (command != null && trim(command).equals("on")) {
counter = 0;
}
}
fill(0);
textFont(font);
textAlign(CENTER);
text(counter, 350, 180);
}
#define LED_PIN 9
#define BUTTON_PIN 2
int lastButtonState = LOW;
void setup(){
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}
void loop(){
int buttonState = digitalRead(BUTTON_PIN);
if(buttonState != lastButtonState && buttonState == HIGH){
Serial.println("on");
}
lastButtonState = buttonState;
if(Serial.available()){
String command = Serial.readStringUntil('\n');
digitalWrite(LED_PIN, command == "on");
}
delay(50);
}