213
edits
(Examples from 28.11.2011 added) |
(02.1.12 Processing sketch XII added) |
||
(13 intermediate revisions by the same user not shown) | |||
Line 769: | Line 769: | ||
incomingVal = myPort.read(); | incomingVal = myPort.read(); | ||
} | } | ||
// set the fill color to the | // set the fill color to the incoming value: | ||
fill(incomingVal); | fill(incomingVal); | ||
// draw a rectangle | // draw a rectangle with the height of the | ||
// incomming value: | // incomming value: | ||
rect(0,height-incomingVal,width,height); | rect(0,height-incomingVal,width,height); | ||
Line 777: | Line 777: | ||
</source> | </source> | ||
<br /><br /> | <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> | |||
<br /><br /> | |||
=== IX translate() and rotate() (PROCESSING ONLY) === | |||
<source lang="java"> | |||
/* ORBITING RECTS (PROCESSING ONLY) | |||
* | |||
* Example of the translate() and rotate() | |||
* functions. Try to comment out certain lines | |||
* of the code to understand what exactly happens | |||
* during the runtime of the program. | |||
* | |||
* There is also a good tutorial which explains | |||
* 2D transformations: | |||
* http://processing.org/learning/transform2d/ | |||
* | |||
* Frederic Gmeiner, 2011 | |||
*/ | |||
// value used for the increasing rotation | |||
float rotationVal = 0; | |||
void setup(){ | |||
size(500,500); | |||
smooth(); | |||
noStroke(); | |||
// this tells processing to draw rectangles | |||
// from the center instead of the upper left | |||
// corner | |||
rectMode(CENTER); | |||
} | |||
void draw(){ | |||
background(100); | |||
// set the origin to the center of the sketch window | |||
// (so width/2, height/2 is now equal to 0,0 ) | |||
translate(width/2,height/2); | |||
// rotate the whole scene by the amount of | |||
// the increasing rotationVal | |||
rotate(rotationVal); | |||
// change the origin again by 100 pixels to the right. | |||
// we are doing this so that the rectangle won't stick | |||
// to the center of the sketch and rotates there, | |||
// but rather let the rect orbit around the | |||
// center with a distance of 100px. | |||
translate(100,0); | |||
// now draw the black rectangle to the | |||
// origin (0,0) | |||
fill(0); | |||
rect(0,0,30,30); | |||
// to get to the position of the smaller red | |||
// rectangle which is circling faster, we have | |||
// to rotate the scene again with a multiplication | |||
// of the prvious rotation: | |||
rotate(rotationVal*4); | |||
// shift the origin again by 40px to the right: | |||
translate(40,0); | |||
// draw the smaller red rectangle | |||
fill(255,0,0); | |||
rect(0,0,5,5); | |||
// increase the rotation value a bit: | |||
rotationVal += 0.01; | |||
} | |||
</source> | |||
<br /><br /> | |||
=== X String, Char & Typography (PROCESSING ONLY) === | |||
<source lang="java"> | |||
/* RANDOM LETTERS MOUSE WRITER | |||
* | |||
* Example of how to work with font files and | |||
* charecters (char). | |||
* In order to use a font face we have to | |||
* convert it using the Tools>create font... | |||
* menu. This will make the font available | |||
* in the data folder of this sketch. | |||
* Follow this tutorial for detailed | |||
* descriptions: | |||
* http://processing.org/learning/text/ | |||
* | |||
* Frederic Gmeiner, 2011 | |||
*/ | |||
// create a font object | |||
PFont mySerif; | |||
void setup() { | |||
size(400,400); | |||
smooth(); | |||
// load the font file "Baskerville-48.vlw" from | |||
// the data folder | |||
// !!! MAKE SURE THAT YOU HAVE THE FILE IN YOUR | |||
// DATA FOLDER OR THAT YOU CREATED A NEW ONE AND CHANGED | |||
// THE NAME ACCORDINGLY !!! | |||
mySerif = loadFont("Baskerville-48.vlw"); | |||
// set the text align to center | |||
textAlign(CENTER); | |||
background(0); | |||
fill(255,0,0); | |||
// specify the font and the size which should | |||
// be used when calling the text() function | |||
textFont(mySerif, 15); | |||
// write "random letters" to the position 50,15 | |||
text("random letters", 50, 15); | |||
} | |||
void draw() { | |||
//background(0); | |||
fill(200,200,200,50); | |||
// set the font and font size | |||
textFont(mySerif, 32); | |||
// instead of writing a String to the screen we only | |||
// draw a single randomly chosen character to the current | |||
// mouse position. | |||
// the char() function returns a character from the unicode | |||
// table by the index. | |||
text( char(int(random(150))), mouseX, mouseY); | |||
} | |||
</source> | |||
<br /><br /> | |||
=== Exercise IV === | |||
Modify the example X so that instead of writing random characters, the following String (char sequence) | |||
is written "HELLO WORLD" (but with only one letter at each draw() cycle). | |||
Helpful links: <br /> | |||
[http://processing.org/learning/text/ Strings and Drawing Text in Processing] <br /> | |||
<br /><br /> | |||
=== SOLUTION: Exercise IV === | |||
[[File:Processing_hello_world_mouse_writer.png|200px|thumb]] | |||
<source lang="java"> | |||
PFont mySerif; | |||
char[] letters = {'H','E','L','L','O',' ','W','O','R','L','D',' '}; | |||
int curPos = 0; | |||
void setup() { | |||
size(400,400); | |||
smooth(); | |||
mySerif = loadFont("Serif-48.vlw"); | |||
textAlign(CENTER); | |||
textFont(mySerif, 32); | |||
background(0); | |||
frameRate(5); | |||
} | |||
void draw() { | |||
fill(200,200,200,100); | |||
translate(mouseX,mouseY); | |||
text( char(letters[curPos]), 0, 0); | |||
curPos++; | |||
if(curPos == letters.length){ | |||
curPos = 0; | |||
} | |||
} | |||
</source> | |||
<br /> <br /> | |||
== 12.12.2011 Arduino & Processing sketches == | |||
=== V Capacitive Sensing (Arduino) === | |||
[[File:V_arduino_capacitive_sensing.png|200px|thumb]] | |||
<source lang="java"> | |||
/* CAPACITVE SENSING (ARDUINO) | |||
* | |||
* Example of how to include external libraries. | |||
* In this case we are using the CapSense Library | |||
* (http://www.arduino.cc/playground/Main/CapSense) | |||
* to turn a piece of aluminium foil into a capacitive | |||
* sensor. Check out the description page for more | |||
* details and options. | |||
* | |||
* Frederic Gmeiner, 2011 | |||
*/ | |||
#include <CapSense.h> | |||
CapSense cs_9_2 = CapSense(9,2); | |||
long capValue; | |||
void setup() | |||
{ | |||
Serial.begin(9600); | |||
} | |||
void loop() | |||
{ | |||
capValue = cs_9_2.capSense(30); | |||
Serial.println(capValue); | |||
delay(10); | |||
} | |||
</source> | |||
<br /> <br /> | |||
=== Va Smoothing Sensor Values === | |||
[[File:Arduino_array_smoothing.png|200px|thumb]] | |||
<source lang="java"> | |||
/* SMOOTHING SENSOR VALUES (ARDUINO) | |||
* | |||
* Sometimes the values from a sensor variate | |||
* around a certain value or have certain peaks | |||
* that we need to filter out in order to | |||
* work with the signal. For this we can | |||
* use an array in which we write the | |||
* sensor reaings first and calculate the | |||
* average of all values in the array. | |||
* | |||
* NOTE: This smoothing example is very simple. | |||
* There are much better ways of doing this, but | |||
* this is the most basic procedure to illustrate | |||
* the purpose of a smoothing algorithm. | |||
* | |||
* Frederic Gmeiner, 2011 | |||
*/ | |||
#include <CapSense.h> | |||
CapSense cs_9_2 = CapSense(9,2); | |||
// vars for smoothing array: | |||
const int numReadings = 10; | |||
long capValues[numReadings]; | |||
int index = 0; // the index of the current reading | |||
int total = 0; // the running total | |||
int average = 0; // the average | |||
void setup() | |||
{ | |||
Serial.begin(9600); | |||
} | |||
void loop() | |||
{ | |||
capValues[index] = cs_9_2.capSense(30); | |||
// reset and recalculate the total: | |||
total= 0; | |||
for(int i=0; i<numReadings; i++) total += capValues[i]; | |||
// advance to the next position in the array: | |||
index = index + 1; | |||
// if we're at the end of the array... | |||
// ...wrap around to the beginning: | |||
if (index >= numReadings) index = 0; | |||
// calculate the average: | |||
average = total / numReadings; | |||
Serial.println(average); // print the average | |||
delay(10); // arbitrary delay to limit data to serial port | |||
} | |||
</source> | |||
<br /> <br /> | |||
=== XI Interval Timer=== | |||
<source lang="java"> | |||
/* INTERVAL TIMER (PROCESSING) | |||
* | |||
* Often when animating things, we need | |||
* to trigger events in a certain interval. | |||
* For this the millis() funtion is very helpful. | |||
* It returns the time in milliseconds | |||
* since the program was started. | |||
* | |||
* Frederic Gmeiner, 2011 | |||
*/ | |||
// create a variable to store the last time | |||
// our event was triggered. (the long datatype | |||
// can store much larger numbers as an integer) | |||
long lastMillis= 0; | |||
// define the length of the interval | |||
int interval = 1000; | |||
void draw(){ | |||
// check whether the interval time has passed: | |||
if(millis() - lastMillis > interval){ | |||
// once the interval time has passed, | |||
// update the lastMillis so that we need to | |||
// wait again for 1000 milliseconds. | |||
lastMillis = millis(); | |||
// trigger the event: | |||
println(millis()/1000); | |||
} | |||
} | |||
</source> | |||
<br /> <br /> | |||
=== Exercise V: Counter === | |||
[[File:processing_counter_screenshot.png|200px|thumb]] | |||
Modify the example XI above so that it will display the seconds directly in the sketch window.<br /> | |||
Also change the color of the font according to whether or not the number which is displayed | |||
is dividable by 3. For this the modulo operator % is very helpful.<br /> | |||
[http://processing.org/reference/modulo.html modulo operator] | |||
== 02.01.2012 Processing sketches == | |||
=== XII Classes === | |||
<source lang="java"> | |||
/* GROWING FLOWERS (PROCESSING) | |||
* | |||
* In this example the concept of "object | |||
* orientated programming" (OOP) is used | |||
* to structure the code into objects by using | |||
* a class which describes a flower (a very | |||
* abstract flower though). | |||
* | |||
* IMPORTANT: Make sure you have the corresponding | |||
* class "Flower.pde" in your sketch folder !!! | |||
* | |||
* Frederic Gmeiner, 2012 | |||
*/ | |||
// declare two new objects of the type Flower and | |||
// call their constructor function Flower() with | |||
// individual values: | |||
Flower flower1 = new Flower("Tulip", 200, 400); | |||
Flower flower2 = new Flower("Rose", 0, 400); | |||
void setup() { | |||
size(400, 400); | |||
smooth(); | |||
} | |||
void draw() { | |||
background(150); | |||
// call the grow() function of flower1 | |||
// which increases the growths by 0.1 | |||
flower1.grow(0.1); | |||
// call the paint() function of flower1 | |||
// in order to draw the flower to the screen | |||
flower1.paint(); | |||
// set the xPos variable of flower2 to the | |||
// current x position of the mouse | |||
flower2.xPos = mouseX; | |||
// call the grow() function of flower2 with | |||
// half of the value of flower1 | |||
flower2.grow(0.05); | |||
// call the paint function of flower2 | |||
flower2.paint(); | |||
// to make the objects interact with each other | |||
// we have to check from within our main draw loop. | |||
// here we check compare the individial x positions | |||
// and change the fill color accordingly: | |||
if ( flower2.xPos > flower1.xPos){ | |||
fill(255, 0, 0); | |||
} | |||
else { | |||
fill(150); | |||
} | |||
} | |||
</source> | |||
<br /> <br /> | |||
<source lang="java"> | |||
/* FLOWER CLASS (PROCESSING) | |||
* | |||
* Definition of the Flower class which we use in | |||
* the GROWING FLOWERS sketch. NOTE: Class names always | |||
* start with an upper case letter. | |||
* | |||
* IMPORTANT: Make sure you have this class in the | |||
* file "Flower.pde" in your main sketch folder !!! | |||
* | |||
* Frederic Gmeiner, 2012 | |||
*/ | |||
class Flower { | |||
// global class variables | |||
float growth; | |||
String name; | |||
float xPos; | |||
float yPos; | |||
// this is the so called "constructor" method with which | |||
// the Flower class is initiated and fed with individual | |||
// values. | |||
Flower(String theName, int theXPos, int theYPos) { | |||
// assign the local function variables (theName, theXPos, | |||
// theYPos) to the global class variables (name, xPos, yPos). | |||
name = theName; | |||
xPos = theXPos; | |||
yPos = theYPos; | |||
} | |||
// function fow increasing the growth variable | |||
// (by means of a grow factor) | |||
void grow(float theGrowFactor){ | |||
growth += theGrowFactor; | |||
} | |||
// function for painting the visual appearance | |||
// of the flower onto the screen | |||
void paint(){ | |||
line(xPos,yPos, xPos, yPos - growth); | |||
ellipse(xPos, yPos - growth, growth, growth); | |||
} | |||
} | |||
</source> | |||
<br /> <br /> | |||
[[Category:Physical Computing]] | [[Category:Physical Computing]] | ||
[[Category:Frederic Gmeiner]] | [[Category:Frederic Gmeiner]] | ||
[[Category:WS11]] | [[Category:WS11]] |
edits