Arduino sketch
/* 07/10/2011 - Massimo Pacilio This example code is in the public domain. */ const unsigned int piezoPin = 7; const unsigned int BAUD_RATE = 9600; char key = 0; void setup() { pinMode(piezoPin, OUTPUT); Serial.begin(BAUD_RATE); } void loop() { if (Serial.available()) { key = Serial.read(); if (key == byte(37)) { // left arrow tone(piezoPin, 1000); } else if (key == byte(39)) { // right arrow tone(piezoPin, 800); } else if (key == byte(38)) { // up arrow tone(piezoPin, 500); } else if (key == byte(40)) { // down arrow tone(piezoPin, 200); } else { tone(piezoPin, 0, 1); } } }
Processing sketch
import processing.serial.*;
color fillVal = color(0,0,0); // imposta la variabile 'fillVal' Serial port; void setup() { size(100, 100); //println("Available serial ports:"); //println(Serial.list()); port = new Serial(this, Serial.list()[0], 9600); } void draw() { background(0,0,0); if (keyPressed == true) { // finché il tasto è premuto fill(fillVal); // associa il colore di riempimento a 'fillVal' ellipse(50, 50, 50, 50); // disegna un cerchio al centro } else { keyReleased(); } } void keyPressed() { if (key == CODED) { if (keyCode == UP) { fillVal = color(255,255,255); // 'fillVal' corrisponde al bianco } else if (keyCode == DOWN) { fillVal = color(255,0,0); // ... al rosso } else if (keyCode == LEFT){ fillVal = color(0,255,0); // ... al verde } else if (keyCode == RIGHT) { fillVal = color(0,255,150); // ... al verde chiaro } } else { fillVal = 0; } port.write(keyCode); if (keyPressed == false) { port.write('0'); } } void keyReleased() { port.write('0'); }
Video