213
edits
| mNo edit summary |  (Examples from 28.11.2011 added) | ||
| Line 662: | Line 662: | ||
| </source> | </source> | ||
| <br /><br /> | |||
| == 28.11.2011 Arduino & Processing sketches == | |||
| === III Potentiometer (Arduino) === | |||
| [[File:III_poti_schematic.png|200px|thumb]] | |||
| [[File:III_potentiometer_arduino.png|200px|thumb]] | |||
| <source lang="java"> | |||
| /* TURNING THE KNOB (ARDUINO) | |||
|  *  | |||
|  * Here we connect a potentiometer (variable resistor) to | |||
|  * the analog pin 3 of our Arduino to control the state  | |||
|  * of an LED connected to pin 13 and also to send the | |||
|  * value via the serial connection to a processing sketch. | |||
|  * | |||
|  * The value we get from an analog pin is ranging from  | |||
|  * 0 to 1023 (10 Bit). In order to send it as one Byte  | |||
|  * (8 Bit) we have to scale the incomming value using | |||
|  * the map() function.  | |||
|  * | |||
|  * NOTE: You can use the same code for many different  | |||
|  * types of variable resistors (e.g. photo resistor,  | |||
|  * bend sensor, etc.) | |||
|  *  | |||
|  * Frederic Gmeiner, 2011  | |||
|  */ | |||
| // specify the pin numbers we are going to use: | |||
| int ledPin = 13; | |||
| int potiPin = 3; | |||
| // create a variable to hold the value from the poti: | |||
| int potiValue; | |||
| void setup(){ | |||
|   // set the pin mode of the led pin to  act as an output: | |||
|   pinMode(ledPin,OUTPUT); | |||
|   // establish a serial connection: | |||
|   Serial.begin(9600); | |||
| } | |||
| void loop(){ | |||
|   // read the current value of the poti pin  | |||
|   // and store it in the variable potiValue: | |||
|   potiValue = analogRead(potiPin); | |||
|   // if the value is over a certain threshold | |||
|   // (here it's 511 or the middle of the range), | |||
|   // turn the LED on, otherwise turn it off: | |||
|   if(potiValue > 511){ | |||
|     digitalWrite(ledPin,HIGH); | |||
|   }else{ | |||
|     digitalWrite(ledPin,LOW); | |||
|   } | |||
|   // in oder to send the poti value as one byte (0-255) | |||
|   // we have to scale it from the original range (0 - 1023): | |||
|   int scaledVal = map(potiValue,0,1023,0,255); | |||
|   // send the scaled value via the serial port as a byte: | |||
|   Serial.print(scaledVal,BYTE); | |||
|   // wait a little bit to not overload the serial buffer: | |||
|   delay(50); | |||
| } | |||
| </source> | |||
| <br /><br /> | |||
| === III Potentiometer (Processing) === | |||
| <source lang="java"> | |||
| /* TURNING THE KNOB (PROCESSING) | |||
|  *  | |||
|  * Example of how to read incomming data | |||
|  * from the serial connection. | |||
|  *  | |||
|  * Frederic Gmeiner, 2011  | |||
|  */ | |||
| // first we have to import the serial library: | |||
| import processing.serial.*; | |||
| // variable for our Serial object: | |||
| Serial myPort; | |||
| // variable to hold the incomming value | |||
| int incomingVal; | |||
| void setup(){ | |||
|   size(255,255); | |||
|   // this line is very helpful to find out the name of the serial  | |||
|   // port of your Arduino. It lists all available ports of your | |||
|   // computer. (Make sure that your arduino is connected to a USB | |||
|   // port before running this sketch)  | |||
|   println(Serial.list()); | |||
|   // make a new Serial object with the name of our current Arduino  | |||
|   // port. NOTE: The name of the port is different for each Arduino | |||
|   // so we have to change it accordingly. On a windows system it is | |||
|   // called different like "COM3" or similar.  | |||
|   myPort = new Serial(this,"/dev/tty.usbserial-A70061cx",9600); | |||
| } | |||
| void draw(){ | |||
|   background(0); | |||
|   // only read from the serial port if there is new data  | |||
|   // available and store this in the variable incommingVal:   | |||
|   while(myPort.available() > 0){ | |||
|     incomingVal = myPort.read(); | |||
|   } | |||
|   // set the fill color to the incomming value:  | |||
|   fill(incomingVal); | |||
|   // draw a rectangle depending with the height of the | |||
|   // incomming value: | |||
|   rect(0,height-incomingVal,width,height); | |||
| } | |||
| </source> | |||
| <br /><br /> | <br /><br /> | ||
edits