Processing
This was probably one of the hardest lab assignments I had to do. But definitely the most interesting. I was thrilled when I finally got to move the little square with my potentiameter!
I used Dan ’s processing code:
// Example by Dan O’Sullivan apologies to Tom Igoe
import processing.serial.*;
Serial myPort; // The serial port
int slide;
int light;
int button;
String accumulation = “”;
void setup() {
// List all the available serial ports:
println(Serial.list());
size(255,255);
// I know that the first port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[0].
// Open whatever port is the one you’re using.
myPort = new Serial(this, Serial.list()[0], 9600);
//prime the pump in case your microcontroller is stuck in serin
myPort.write(65);
}
void draw() {
background(0);
fill(0,light,0);
if (button == 1){
ellipse(slide,100,10,10);
}
else{
rect(slide,100,10,10);
}
}
void serialEvent(Serial p) {
int input = myPort.read();
// if the last thing in was a carriage return, it means that a whole
reading is ready
if (input== 13) {
String[] asText = accumulation.split(”,”); //separate out the
reading based on the comma (44)
int[] asNumbers = int(asText); //turn the text reading in to
numbers, beware if there is a 13 still attached at the end
println(”accumulation ” + accumulation );
if (asNumbers.length >=3){
light = asNumbers[0]/3; //scale it a bit based on the empirical
readings of this sensor
slide = asNumbers[1]/4; //easy scaling because it is a
potentiometer which delivers solid 0-1020
button = asNumbers[2];
println(”light ” + light + ” slide” + slide + ” button” + button);
}
//now that you go a whole reading, clear your accumulation
accumulation = “”;
//now send something to satisfy the serin
//in this case I actualy am sending information back but even if you
are not just send anything back
//my servo wants numbers 65- 250 and the mouse gives me numbers
between 0 and width
int servoRange = 250-65;
float percentageAcross = mouseX/float(width);
int servoOut = int(servoRange*percentageAcross) + 65;
myPort.write(servoOut);
}
else{
accumulation= accumulation + char(input); //if you did not hear
13, accumulate
}
}
Which basically has a green square on a black background. And by programming the PIC chip with this code:
DEFINE OSC 4
INCLUDE “modedefs.bas”
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 10
TRISA = %11111111
adcon1 = %10000010
adcVar0 VAR WORD ‘ ir Create variable to store result
adcVar1 VAR WORD ‘ ir Create variable to store result
switchVar var byte
inputVar Var BYTE
pause 500
main:
ADCIN 0, adcVar0
ADCIN 1, adcVar1
serout2 portc.6, 16468, [DEC adcVar1,44,DEC adcVar0,44,DEC PORTB.6,13]
serIN2 portc.7, 16468, [inputVar]
debug inputVar
PULSOUT portd.0 ,inputVar
PAUSE 10
GoTo main
I managed to get the little green square to go back and forth. I was trying to get the colors to fade in and out with a photcell but just couldn’t figure it out. I will be trying to work on this exercise more after the midterms.
