More Lab
October 12, 2005
Today after the class I went directly to finish my lab assignments for the PComp. Although I have spent 3 hours to figure out the code, in the end I was happy, because I have realised that it is very similar to carrying variables through actionscript and javascript, or php in this sense. Messing with codes have an advantage of seeing different things than when you copy and paste.
So here is the code that I used, the setup is I have an analog input which is connected through a serial to the computer and getting the values inside of Processing to output some movements on the object I have previously created, basically an ellipse and background color of the processing canvas. IT is nice because it can be improved greatly and it is not that complex after you find your way how to pass the variables and the values.
inv9600 con 16468
thisByte var byte
inByte var byte
define ADCBITS 10
DEFINE ADCCLOCK 3
DEFINE ADC_SAMPLEUS 50
ADCvar var wordADCON1 =%10000010
input portb.0
pause 500
main:
ADCIN 0, ADCvar
thisByte = ADCvar/4
serin2 portc.7, inv9600, [inByte]
if inbyte = 65 then
serout2 portc.6, inv9600, [thisByte]
endif
goto mainimport processing.serial.*;
int bgcolor=0; // background color
int fgcolor = 10; // fill color
Serial port; // the serial port
int[] serialInArray = new int[2]; // where we’ll put what we receive
int serialCount = 0; // a count of how many bytes we receive
float xpos, ypos; // Starting position of the ballvoid setup() {
size(256, 256); // stage size
noStroke(); // no border on the next thing drawn// Set the starting position of the ball (middle of the stage)
xpos = width/2;
ypos = height/2;
port = new Serial(this, Serial.list()[0], 9600);
port.write(65); // send a capital A to start the microcontroller sending
}
void draw() {
background(fgcolor);
fill(xpos,ypos,fgcolor);
// Draw the shape
ellipse(xpos, ypos, 25, 25);
// get any new serial data:
while (port.available() > 0) {
processByte((char)port.read());}
delay(1);
port.write(65);
}void processByte(char inByte) {
// add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
xpos = serialInArray[0];
fgcolor = serialInArray[0];// send a capital A to request new sensor readings:
delay(1);
port.write(65);
// reset serialCount:
serialCount = 0;}