Processing is an open source programme which is used for people who ultimately want to create either images, animations or interactions. Everyone from students to professionals use this in everyday life...which I am now.
Here are some codes that you would need to put in the sketchbook - to do this you go onto file --> examples --> sketchbook.
/*
* pa_8Switches
*
* Reads the values which represent the state of eight switches
* from the serial port and draws a graphical representation.
*
* This file is part of the Arduino meets Processing Project.
* For more information visit http://www.arduino.cc.
*
* copyleft 2005 by Melvin Ochsmann for Malm? University
*
*/
PImage number2;
PImage number3;
PImage number1;
PImage buffer;
// importing the processing serial class
import processing.serial.*;
// the display item draws background and grid
DisplayItems di;
// definition of window size and framerate
int xWidth = 512;
int yHeight = 512;
int fr = 12;
// attributes of the display
boolean bck = true;
boolean grid = true;
boolean g_vert = false;
boolean g_horiz = true;
boolean g_values = true;
boolean output = true;
// variables for serial connection, portname and baudrate have to be set
Serial port;
String portname = Serial.list()[0];
int baudrate = 9600;
int value = 0;
// variables to draw graphics
int i;
String fontname2 = "Helvetica-Bold-96.vlw"; // if you would like to change fonts, make sure the fontfile (which can be created with Processing) is in the data directory
int fontsize2 = 72; //
PFont font2;
float valBuf[] = new float[8];
int xpos, ypos;
// lets user control DisplayItems properties and value output in console
void keyPressed(){
if (key == 'b' || key == 'B') bck=!bck; // background black/white
if (key == 'g' || key == 'G') grid=!grid; // grid ON/OFF
if (key == 'v' || key == 'V') g_values=!g_values; // grid values ON/IFF
if (key == 'o' || key == 'O') output=!output; //turns value output ON/OFF
}
void setup(){
// set size and framerate
size(xWidth, yHeight); frameRate(fr);
// establish serial port connection
port = new Serial(this, portname, baudrate);
println(port);
number2 = loadImage("two.jpg");
number3 = loadImage("three.jpg");
number1 = loadImage("one.jpg");
buffer = number1;
// create DisplayItems object
di = new DisplayItems();
// load second font for graphical representation and clear value buffer
font2 = loadFont(fontname2);
for(i = 0; i < valBuf.length; i++ ){
valBuf[i] = (height/2);
}
}
void drawEightSwitchesState(){
textFont(font2, fontsize2);
if (output) print("8Switches State: ");
// takes value, interprets it as a byte
// and reads each bit
for (i=0; i < 8 ; i++){
if(output) print(value & 1);
// if a bit is 1 increase the corresponding value in value buffer array by 1
if ( (value & 1) == 1){
if(valBuf[i] > fontsize2 ) valBuf[i] -=1;
// if a bit is 0 decreade corresponding value
}else{
if(valBuf[i] < height) valBuf[i] += 1;
}
if(output) print(".");
// draw number for each value at its current height
fill( ( (i%3==0) ? 255 : 0 ), ((i%3==1) ? 255 : 0 ) ,((i%3==2) ? 255 : 0 ) );
text( ( ""+(i+1) ), (i*(width/9)) + (width/12), valBuf[i]);
value = value >> 1;
}// end for loop
if(output) println("");
}
void draw(){
// listen to serial port and set value
while(port.available() > 0){
value = port.read();
println(value);
}
// draw background, then EightSwitchesState and finally rest of DisplayItems
di.drawBack();
//drawEightSwitchesState();
di.drawItems();
if (value == 50){
buffer=number2;
}
else
if (value == 48){
buffer =number3;
}
else
buffer = number1;
image(buffer,50,50);
}
PImage teddy;
float xpos;
float ypos;
float drag = 30;
void setup() {
size(200, 200);
teddy = loadImage("teddy.gif");
xpos = width/2;
ypos = height/2;
}
void draw() {
background(102);
float difx = mouseX - xpos-teddy.width/2;
if (abs(difx) > 1) {
xpos = xpos + difx/drag;
xpos = constrain(xpos, 0, width-teddy.width);
}
float dify = mouseY - ypos-teddy.height/2;
if (abs(dify) > 1) {
ypos = ypos + dify/drag;
ypos = constrain(ypos, 0, height-teddy.height);
}
// Display the sprite at the position xpos, ypos
image(teddy, xpos, ypos);
}
No comments:
Post a Comment