Arduino hands-on workshop Giorno 2 – Interfacciamento con PC: Arduino + Processing Roberto Gallea – [email protected]
Sep 01, 2014
Arduino hands-on workshop
Giorno 2 – Interfacciamento con PC: Arduino + Processing
Roberto Gallea – [email protected]
Comunicazione seriale
• Arduino può utilizzare la connessione seriale (USB) non solo per l’alimentazione…
• …Ma anche per comunicare con il computer host
– Scambio dati
– Debug
Monitor seriale
Comunicazione seriale
• Non solo Monitor Seriale
• Comunicazione Seriale
• Scambio di dati con altre applicazioni
• rocessing
Esempio «a tumpulata»
const int ledPin = 9; // the pin that the LED is
attached to
void setup()
{
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
byte brightness;
// check if data has been sent from the computer:
if (Serial.available()) {
// read the most recent
brightness = Serial.read();
// set the brightness of the LED:
analogWrite(ledPin, brightness);
}
}
import processing.serial.*;
Serial port;
void setup() {
size(256, 150);
println("Available serial ports:");
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
//port = new Serial(this, "COM1", 9600);
}
void draw() {
// draw a gradient from black to white
for (int i = 0; i < 256; i++) {
stroke(i);
line(i, 0, i, 150);
}
// write the current X-position of the mouse to the serial port
// as a single byte
port.write(mouseX);
}
Esempio «a tumpulata»
Cos’è Processing
• Nasce come software per artisti e visual designers
• Linguaggio di programmazione specifico per generare e modificare immagini
• Diventato uno strumento ad uso generico
Info iniziali
• Aspetta… Ma sembra Arduino?!
• Il sw di Arduino deriva da esso, infatti sono molto simili
• Arduino -> setup(), loop()
• Processing -> setup(), draw()
(mini) Tutorial Processing
void setup() {
size(480, 120);
}
void draw() {
if (mousePressed) {
fill(0);
} else {
fill(255);
}
ellipse(mouseX, mouseY, 80, 80);
}
(mini) Tutorial Processing
color c = color(0);
float x = 0;
float y = 100;
float speed = 1;
void setup() {
size(200,200);
}
void draw() {
background(255);
move();
display();
}
void move() {
x = x + speed;
if (x > width) {
x = 0;
}
}
void display() {
fill(c);
rect(x,y,30,10);
}
Definisce la dimensione della finestra
Imposta il colore di sfondo
Aggiorna la posizione dell’oggetto
Disegna l’oggetto
(mini) Tutorial Processing
int[] xvals; int[] yvals; int[] bvals; void setup() { size(640, 360); noSmooth(); xvals = new int[width]; yvals = new int[width]; bvals = new int[width]; } int arrayindex = 0; void draw() { background(102); for(int i = 1; i < width; i++) { xvals[i-1] = xvals[i]; yvals[i-1] = yvals[i]; bvals[i-1] = bvals[i]; }
// Add the new values to the end of the array xvals[width-1] = mouseX; yvals[width-1] = mouseY; if(mousePressed) { bvals[width-1] = 0; } else { bvals[width-1] = 255; } fill(255); noStroke(); rect(0, height/3, width, height/3+1); for(int i=1; i<width; i++) { stroke(255); point(i, xvals[i]/3); stroke(0); point(i, height/3+yvals[i]/3); stroke(255); line(i, 2*height/3+bvals[i]/3, i, (2*height/3+bvals[i-1]/3)); } }
(mini) Tutorial Processing
int[] xvals; int[] yvals; int[] bvals; void setup() { size(640, 360); noSmooth(); xvals = new int[width]; yvals = new int[width]; bvals = new int[width]; } int arrayindex = 0; void draw() { background(102); for(int i = 1; i < width; i++) { xvals[i-1] = xvals[i]; yvals[i-1] = yvals[i]; bvals[i-1] = bvals[i]; }
// Add the new values to the end of the array xvals[width-1] = mouseX; yvals[width-1] = mouseY; if(mousePressed) { bvals[width-1] = 0; } else { bvals[width-1] = 255; } fill(255); noStroke(); rect(0, height/3, width, height/3+1); for(int i=1; i<width; i++) { stroke(255); point(i, xvals[i]/3); stroke(0); point(i, height/3+yvals[i]/3); stroke(255); line(i, 2*height/3+bvals[i]/3, i, (2*height/3+bvals[i-1]/3)); } }
Arduino + Processing: #1
Arduino + Processing: #1
int input = A0;
int val = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
val = analogRead(input);
Serial.write(val/4);
delay(10);
}
import processing.serial.*;
int value = 0;
Serial port;
void setup() {
port = new Serial(this,Serial.list()[0],9600);
// change to correct com port(1)
size(600,600); // size of the window
background(200); // color of the backround
}
void draw() {
while ((value = port.read()) != -1) {
println(value);
fill(value);
rect(0,0,600,600);
}
}
Arduino + Processing: #2
void setup() {
// initialize the serial communication:
Serial.begin(9600);
}
void loop() {
// send the value of analog input 0:
Serial.println(analogRead(A0));
// wait a bit for the analog-to-digital converter
// to stabilize after the last reading:
delay(2);
}
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
void setup () {
// set the window size:
size(400, 300);
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void draw () {
// everything happens in the serialEvent()
}
Arduino + Processing: #2
void setup() {
// initialize the serial communication:
Serial.begin(9600);
}
void loop() {
// send the value of analog input 0:
Serial.println(analogRead(A0));
// wait a bit for the analog-to-digital converter
// to stabilize after the last reading:
delay(2);
}
import processing.serial.*;
Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
void setup () {
// set the window size:
size(400, 300);
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// don't generate a serialEvent() unless you get a newline
myPort.bufferUntil('\n');
// set inital background:
background(0);
}
void draw () {
// everything happens in the serialEvent()
}
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// convert to an int and map to the screen height:
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
// draw the line:
stroke(127,34,255);
line(xPos, height, xPos, height - inByte);
// at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0);
}
else {
// increment the horizontal position:
xPos++;
}
}
}
Lampada RGB - Connessioni
Lampada RGB - Arduino
int Red = 3; //
int Green = 5; //pin definitions
int Blue = 6; //
int r = 125; //
int g = 125; //default all the pins to 125(white)
int b = 125; //
void setup()
{
Serial.begin(9600);
pinMode(Red,OUTPUT);
pinMode(Green,OUTPUT); // set the pin modes
pinMode(Blue,OUTPUT);
}
void loop()
{
if(Serial.available()==9) // while there is data
{
r= (Serial.read()-48)*100; // read the first byte
r=r + (Serial.read()-48)*10; // read the second
byte......
r=r + Serial.read()-48; // so on so forth
g= (Serial.read()-48)*100;
g=g+ (Serial.read()-48)*10;
g=g + Serial.read()-48;
b= (Serial.read()-48)*100 ;
b=b+ (Serial.read()-48)*10;
b=b + Serial.read()-48;
int d = Serial.read();// this is just to clear the end line
character
}
analogWrite(Red,r); //
analogWrite(Green,g);
analogWrite(Blue,b);
}
Lampada RGB - Processing
• Codice troppo lungo, su file.
Grazie!
BONUS: FIRMATA LIBRARY
Firmata
• Protocollo di comunicazione fra microcontrollori e computer
• Permette di controllare gli input/output del µcontrollore tramite porta seriale…
• …cioè non è necessario leggere e scrivere messaggi, la libreria lo fa per noi
Test Firmata
• Aprire e caricare lo sketch File->Esempi->Firmata->OldStandardFirmata
• Avviare firmata_test.exe
Lampada RGB con Firmata
• Stesso circuito
• Sketch StandardFirmata su Arduino
Lampada RGB con Firmata
• Stesso circuito
• Sketch StandardFirmata su Arduino
Lampada RGB con Firmata
• Stesso circuito
• Sketch StandardFirmata su Arduino
import processing.serial.*; import cc.arduino.*; Arduino arduino; int red; int green; int blue; void setup() { size(512, 200); arduino = new Arduino(this, Arduino.list()[0], 115200); } void draw() { red = constrain(mouseX/2, 0, 255); green = constrain(255 - mouseY, 0, 255); blue = constrain(255 - mouseX/2, 0, 255); background(red, green, blue); arduino.analogWrite(3, red); arduino.analogWrite(5, green); arduino.analogWrite(6, blue); }