====== Arduino et Processing ======
{{{project
picture:
machines:
materiaux:
logiciels:Arduino Processing
fichiers:
liens:
tags: Arduino Processing Firmware
usager:thierry_dasse
licence:cc-by-sa
}}}
===== Firmware Arduino =====
==== Le firmware ====
#define DIGITAL 0
#define ANALOG 1
#define DIGITALPIN 14
#define ANALOGPIN 6
#define ANALOGDELTA 1
char digitalListen[DIGITALPIN];
char digitalPrevious[DIGITALPIN];
char analogListen[ANALOGPIN];
int analogPrevious[ANALOGPIN];
char command[8];
char key[8];
//char value[8];
int pos = 0;
char c;
int strtoint(char *c) {
int pos = 0;
int val = 0;
while (c[pos] != 0) {
val = 10*val + c[pos++] - '0';
}
return val;
}
void initPin() {
for(int i = 0; i ANALOGDELTA) {
analogPrevious[i] = val;
Serial.print("V A");
Serial.print(i);
Serial.print(",");
Serial.println(val);
}
}
}
}
}
==== Exemples d'utilisation ====
R A0
L A0
C A0
C
M 8,O
W 8,1
W 8,0
M 6,I
W 6,1
L 6
+ montage
===== Applications avec Processing =====
==== Lire un potentiometre en A0 ====
import processing.serial.*;
Serial port;
int potentiometre;
void setup() {
size(800,600);
fill(0);
String portName = Serial.list()[0];
port = new Serial(this,portName,57600);
}
void draw() {
port.write("R A0\n");
while (port.available() > 0) {
int c = port.read();
print((char)c);
}
delay(500);
}
==== Blink ====
import processing.serial.*;
Serial port;
int potentiometre;
void setup() {
size(800,600);
fill(0);
String portName = Serial.list()[0];
port = new Serial(this,portName,57600);
delay(2000);
port.write("M 8,O\n");
}
void draw() {
delay(100);
port.write("W 8,1\n");
delay(100);
port.write("W 8,0\n");
delay(100);
}
==== Led et souris ====
import processing.serial.*;
Serial port;
int potentiometre;
void setup() {
size(800,600);
fill(0);
String portName = Serial.list()[0];
port = new Serial(this,portName,57600);
delay(2000);
port.write("M 8,O\n");
}
void draw() {
if (mousePressed) {
port.write("W 8,1\n");
} else {
port.write("W 8,0\n");
}
}
==== Background ====
import processing.serial.*;
Serial port;
int potentiometre;
String command = "";
int value = 0;
void setup() {
size(800,600);
String portName = Serial.list()[0];
port = new Serial(this,portName,57600);
delay(2000);
}
void draw() {
background(0,value,value);
port.write("R A0\n");
while (port.available() > 0) {
int c = port.read();
command = command + (char)c;
if (c == '\n') {
value = Integer.valueOf(command.split(",")[1].trim());
value = value >> 2;
println(value);
command = "";
}
}
}
==== Line ====
import processing.serial.*;
Serial port;
int potentiometre;
String command = "";
int value;
void setup() {
size(800,600);
fill(0);
String portName = Serial.list()[0];
port = new Serial(this,portName,57600);
delay(2000);
port.write("L A0\n");
strokeWeight(2);
stroke(204, 102, 0);
}
void draw() {
background(255);
while (port.available() > 0) {
int c = port.read();
command = command + (char)c;
if (c == '\n') {
value = Integer.valueOf(command.split(",")[1].trim());
line(100,value+50,700,value+50);
command = "";
}
}
}
Et pour la prochaine fois :
une ardoise magique avec deux potentiomètres et un bouton poussoir