IFD:PhysicalComp2011/code: Difference between revisions

From Medien Wiki
(sketches from 5.12. added)
Line 774: Line 774:
   // incomming value:
   // incomming value:
   rect(0,height-incomingVal,width,height);
   rect(0,height-incomingVal,width,height);
}
</source>
<br /><br />
== 05.12.2011 Arduino & Processing sketches ==
=== IV LED Brightness (Arduino) ===
<source lang="java">
/* MOUSE LED (ARDUINO)
*
* This sketch uses the PWM (Pulse width modulation)
* function of the Arduino to create voltages between
* 0V and 5V (analog output) to set the brightness of a LED.
* The value for the LED brightness is received via
* the serial connection from a processing sketch where
* it is created from the mouse's y position.
*
* Look at the example "I Blinking LED" for how to
* connect the LED, but make sure that you use the
* pin declared in this sketch.
* Frederic Gmeiner, 2011
*/
// pin connected to the LED
// (make sure that it is labeled with PWM)
int myLedPin = 11; 
// for storing the incoming data
byte myIncomingData;     
void setup() {
  // initialize serial communication
  Serial.begin(9600);
}
void loop() {
  // only read if there is new data on the serial port
  while (Serial.available() > 0) {
    myIncomingData = Serial.read();
  }
  // set brightness of the LED with the last value from the serial port
  analogWrite(myLedPin, myIncomingData);
  // wait 10 milliseconds to avoid overloading the serial port
  delay(10);
}
</source>
<br /><br />
=== IV LED Brightness (Processing) ===
<source lang="java">
/* MOUSE LED (PROCESSING)
*
* Here we take the y-position of the mouse
* and map it to the range of 0-255 to
* set the background color and to
* send it to the serial port of the
* Arduino.
* Frederic Gmeiner, 2011
*/
import processing.serial.*;
// serial port object
Serial myPort;
// for storing the scaled y position value
int myValue;
void setup()
  size(200, 500);
  // print all serial devices
  println("Available serial ports:");
  println(Serial.list());
  // set the port which is connected to the arduino board
  // !!! CHANGE THE "/dev/tty.usbserial-A70061cx" TO THE NAME OF YOUR
  // PORT !!!
  myPort = new Serial(this, "/dev/tty.usbserial-A70061cx", 9600);
}
void draw()
{
  // get the y position of the mouse, which can be something
  // between 0 and the HEIGHT of the sketch window and map
  // it to the range of 0 - 255. The map() function returns a
  // float value so we have to cast the output to an integer
  // before assigning it to myValue
  myValue = int( map(mouseY,0,height,0,255));
  // for debugging: print myValue to the console
  println(myValue);
  // set the background color to myValue
  background(myValue);
  // send myValue to the arduino port
  myPort.write(myValue);
}
}
</source>
</source>