Εγκατάσταση του Arduino
- Εγκατάσταση του δωρέαν - ανοικτού λογισμικού Arduino απο το διαδίκτυο (http://arduino.cc).
- Σύνδεση του Arduino με τον υπολογιστή.
- Γραφικό Περιβάλλον του Arduino
Figure 1: Arduino GUI
Play with Piezobuzzer
- File/Examples/Basics/Fade
- File/Examples/Digitas/toneMelody
/* Fade This example shows how to fade an LED on pin 9 using the analogWrite() function. This example code is in the public domain. */ int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by // the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { // set the brightness of pin 9: analogWrite(led, brightness); // change the brightness for next time through the loop: brightness = brightness + fadeAmount; // reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(30); }
Η πρώτη διεπαφή με το Arduino
- Δημιουργία εφαρμογής για διάβασμα αναλογικής εισόδου
- Δημιουργία για να αναβοσβήνω το LED
Send messages from p5 to Arduino
Examples/Libraries/serial/SimpleWrite file:///Applications/Arduino.app/Contents/Resources/Java/reference/Serial_Print.html
Δημιουργία πρωτόκολλου επικοινωνίας Arduino-p5-SC
Η χρήση τη Standart Firmata αποτελεί μια λύση για την επικοινωνία του Arduino με άλλα λογισμικά, αλλα δεν είναι η μοναδική. Στο συγκεκριμένο κεφάλαιο θα δημιουργήσουμε το δικό μας πρωτόκολλο επικοινωνίας.
1) Προγραμματισμός του Arduino
/* Manually write to Serial Aris Bezas Fri, 13 May 2011, 14:20 */ int analog0 = 0; int analogValue0 = 0; void setup() { Serial.begin(9600); } void loop() { // read analog values analogValue0 = analogRead(analog0); // read the value from the Analog0 // print analog values Serial.print("z"); // analog0 Serial.print(analogValue0); Serial.print("\n");// new line }
Όπως γίνεται αντιληπτό, εκυπώνουμε στην σειριακή έξοδο τον χαρακτήρα 'z', στη συνέχεια την τιμή της αναλογικής εισόδου και πηγαίνουμε στην επόμενη γραμμή.
2) Διάβασμα της σειριακής εξόδου απο την Processing και αποστολή OSC μηνυμάτων
Επικοινωνία SuperCollider με το Processing
code
/* Read analog0 from Serial Port and send OSC message to SC Aris Bezas Fri, 13 May 2011, 14:31 */ // Library's import processing.serial.*; import oscP5.*; import netP5.*; Serial port; // Create object from Serial class OscP5 oscP5; NetAddress myRemoteLocation; // Serial Variables String buff = "", temp = " "; int NEWLINE = 10, temporary, counter; int analog0; int lineHeight = 25; void setup(){ frameRate(100); //Open serial port port = new Serial(this, Serial.list()[0], 9600); oscP5 = new OscP5(this,57120); myRemoteLocation = new NetAddress("127.0.0.1",57120); temp = buff; } void draw(){ serial(); sendOSC(); } void serial() { while (port.available() > 0) serialEvent(port.read()); } void serialEvent(int serial) { if(serial != NEWLINE) { // Keep reading characters until '\n' found buff += char(serial); } else { // this isolates just the beginning sensor identified temp = buff.substring(0, buff.length()-(buff.length()-1)); if(temp.equals("z") == true) { temp = buff.substring(1, buff.length()); temporary = int(temp); analog0 = temporary; // println(analog0); } buff = ""; // Clear the value of ?buff? } } void sendOSC() { OscMessage an = new OscMessage("analog0"); an.add(analog0); oscP5.send(an, myRemoteLocation); }
3) Διάβασμα OSC μηνυμάτων απο το SuperCollider και διάδραση με ηχητικό δείγμα
Η Processing στέλνει μήνυμα με συντακτικο [ analog0, 85 ] (όπου 85 η τιμή που αλλάζει και παίρνει την τιμή που διαβάει η αναλογική είσοδο), στην IP: 127.0.0.1 απο είναι ο localhost (ο ίδιος ο υπολογιστής) και στο port: 57120, όπου ακούει ο Server του SuperCollider. Έτσι για να χρησιμοποιήσουμε την παραπάνω τιμή, δημιουργούμε έναν OSCResponder.
/* SynthDef for play a buffer (foubuf), create a new Synth (x) and OSCRespoder to set buffer rate Aris Bezas Fri, 13 May 2011, 15:11 */ //:foubuf v.001 SynthDef(\foubuf, {| out = 0, bufnum = 0, rate = 1, trigger = 1, loop = 1, pos = 0, level = 1| Out.ar(out, Pan2.ar( PlayBuf.ar(1, bufnum, rate, trigger, 0, loop ), pos, level ) ) }).send(s); x = Synth(\foubuf); //:OSC Responders OSCresponder(nil, "analog0", { |t, r, msg| x.set(\rate, msg[1]);}).add;//Responder x.set(\bufnum, 13) x.set(\loop, 0) x.set(\level, 0.3) x.set(\pos, -1)
Ασκηση με Χρονοδρομολόγηση Φαναριών
/* city: - http://arduino.cc/en/Tutorial/BlinkWithoutDelay */ //traffic lights int counter=0; int led1 = 13; int led2 = 12; int led3 = 11; int ledState1 = LOW; // ledState used to set the LED int ledState2 = LOW; int ledState3 = LOW; long previousMillis1 = 0; // will store last time LED was updated long previousMillis2 = 0; long previousMillis3 = 0; long intervalg = 5; // interval at which to blink (milliseconds) long intervalo = 1; long intervalr = 5; int myCounter = 0; int trigger=1; //street lights int analog0 = A0; int sensorValue; unsigned long currentMillis; // the setup routine runs once when you press reset: void setup() { currentMillis = millis(); Serial.begin(9600); // initialize the digital pin as an output. pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); } // the loop routine runs over and over again forever: void loop() { counter++; traficlights(); // Serial.print(ledState1); Serial.print("\t"); Serial.print(ledState2); Serial.print("\t"); Serial.print(ledState3); Serial.print("\t"); Serial.print(trigger); Serial.print("\t"); Serial.print(counter); Serial.print("\n"); // Serial.println(myCounter); //fotismosDromou(); } void traficlights() { if(trigger==1){ if (counter>= 480) { trigger=2; counter=0; } } if(trigger==2){ if(counter>=196){ trigger=3; counter=0; } } if (trigger==3){ if(counter>=480){ trigger=1; counter=0; } } if (trigger==1){ ledState2=LOW; ledState3=LOW; ledState1 = HIGH; }else if (trigger==2){ ledState1=LOW; ledState3=LOW; ledState2=HIGH; }else if (trigger==3){ ledState1=LOW; ledState2=LOW; ledState3=HIGH; } }
Multitouch Interfaces
Setup TouchOSC with iPod-iPhone-iPad to your private Network:
- Setup the Network
- Create a network location from system preferences <TouchOSC>
- Go to AirPort and create a network Name <TouchOSCNet>
- At the Ethernet page check configure Manually and enter an IP <192.0.0.21>
- At the Airport->Advanced->TCP/IP->Configure Manually->IPv4:192.0.0.21
- Apply the settings
- From iPod go to Setting->General->Network->Wi-Fi and connect to <TouchOSCNet>
- Create an interface with TouchOSC
- Open TouchOSC application
- Create your interface
- Save as your .touchosc
- Press Sync button at TouchOSC appliction to syncronize with iPod (or iPhone or iPad)
- Upload the interface to your iPod-iPad-iPhone
- Open TouchOSC app at your iPod
- At Network Preferences edit Host: 192.0.0.21 Port (outgoing): 57120 (to send OSC messages to SuperCollider Server)
- At Layout your Layout that you already made at TouchOSC on your PC
- Press done to see your interface
- See the result
- Open SuperCollider and print all incoming OSC messages