processing & pcom
pic basic code
DEFINE OSC 4INCLUDE "modedefs.bas"DEFINE ADC_BITS 10DEFINE ADC_CLOCK 3DEFINE ADC_SAMPLEUS 10TRISA = %11111111adcon1 = %10000010
adcVar0 VAR WORD ' ir Create variable to store resultadcVar1 VAR WORD ' ir Create variable to store resultswitchVar var byteinputVar Var BYTE
pause 500main: ADCIN 0, adcVar0 ADCIN 1, adcVar1 serout2 portc.6, 16468, [DEC adcVar0,44,DEC adcVar1,44,DEC PORTB.6,13] serIN2 portc.7, 16468, [inputVar] debug inputVar PULSOUT portd.0 ,inputVar PAUSE 10GoTo main
processing code
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 }
}

0 Comments:
Post a Comment
<< Home