serial and processing code
PROCESSING CODE
float[] data = new float[2]; // create 2 space data array. 1) Potentiometer and 2) photocell
int index=0;
float xpos;
int bck;
void setup() {
size(255,255);
beginSerial(); //begin serial communication thing
serialWrite(65); //talk to PIC . like saying hello
}
void loop() {
background(bck);
fill(0,0,244);
ellipse(xpos, 100, 50,50); //movable circle
}
void serialEvent() { //automatic serial listener
data[index] = serial; // save messages to data array
index=index+1; // increment serial counter
if (index==2) { // both messages received
xpos = float(data[0]); // rename the first message to xpos
bck = int(data[1]); //save photocell value
serialWrite(65); // responsd back to PIC
index = 0; // reset serial counter to zero
}
}
'----------------------------------------
PIC CODE
input portc.7
output portc.6
inputVar VAR byte
sensorValue VAR WORD ' Create variable to store result
photoValue VAR WORD
' POTENTIOMETER SETUP
' Define ADCIN parameters
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and right justify result
PAUSE 500 ' Wait .5 second
'-------------------------------------------------------
main:
ADCIN 0, sensorValue 'potentiometer variable
ADCIN 1, photoValue 'photocell
sensorValue = sensorValue/4 'divide the value because it's too big
'----------------------
' THE LISTENING CODE - puts message into inputVar
serin2 portc.7, 16468, [inputVar] 'listening for potentiometer value
if inputVar=65 then ' if received message is 65, then respond to processing
gosub blink 'blink LED
'usage: serout2 dataPin, mode, [data]
serout2 portc.6, 16468, [sensorValue,photoValue] 'talk to processing
ENDif
goto main
blink:
high portd.2
pause 250
low portd.2
pause 250
return
nodata:
return
float[] data = new float[2]; // create 2 space data array. 1) Potentiometer and 2) photocell
int index=0;
float xpos;
int bck;
void setup() {
size(255,255);
beginSerial(); //begin serial communication thing
serialWrite(65); //talk to PIC . like saying hello
}
void loop() {
background(bck);
fill(0,0,244);
ellipse(xpos, 100, 50,50); //movable circle
}
void serialEvent() { //automatic serial listener
data[index] = serial; // save messages to data array
index=index+1; // increment serial counter
if (index==2) { // both messages received
xpos = float(data[0]); // rename the first message to xpos
bck = int(data[1]); //save photocell value
serialWrite(65); // responsd back to PIC
index = 0; // reset serial counter to zero
}
}
'----------------------------------------
PIC CODE
input portc.7
output portc.6
inputVar VAR byte
sensorValue VAR WORD ' Create variable to store result
photoValue VAR WORD
' POTENTIOMETER SETUP
' Define ADCIN parameters
DEFINE ADC_BITS 10 ' Set number of bits in result
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and right justify result
PAUSE 500 ' Wait .5 second
'-------------------------------------------------------
main:
ADCIN 0, sensorValue 'potentiometer variable
ADCIN 1, photoValue 'photocell
sensorValue = sensorValue/4 'divide the value because it's too big
'----------------------
' THE LISTENING CODE - puts message into inputVar
serin2 portc.7, 16468, [inputVar] 'listening for potentiometer value
if inputVar=65 then ' if received message is 65, then respond to processing
gosub blink 'blink LED
'usage: serout2 dataPin, mode, [data]
serout2 portc.6, 16468, [sensorValue,photoValue] 'talk to processing
ENDif
goto main
blink:
high portd.2
pause 250
low portd.2
pause 250
return
nodata:
return

0 Comments:
Post a Comment
<< Home