L A B 5 C O D E :: S E R I A L O U T P U T
S E R I A L O U T P U T
'**************************************************************** ' LAB : Serial Output '* '* '* Notes : serial out = portc.6 '* serial in = portc.7 '* digital in = portb.0 '**************************************************************** DEFINE OSC 4 DEFINE ADC_BITS 10 DEFINE ADC_CLOCK 3 DEFINE ADC_SAMPLEUS 15 TRISA = %11111111 ' SET PORTA to all input ADCON1 = %10000010 TRISC = %10000000 'SET FOR SERIAL IN ON PORTC.7 AND OUT ON C.6 'Constants inv9600 con 16468 ' baudmode = 9600-8-n-1 inverted 'Variables txPin VAR portc.6 ylLED var portd.1 thisByte VAR byte ' a byte to send out data digIn VAR portb.0 ADCVAR VAR WORD OUTPUT ylLed INPUT digIN ' Initialize by blinking HIGH ylLEd PAUSE 300 LOW ylLED PAUSE 300 HIGH ylLEd PAUSE 300 LOW ylLED PAUSE 300 HIGH ylLEd PAUSE 300 LOW ylLED PAUSE 300 HIGH ylLED main: ''' QUESTION 1 ' read the switch, convert it to a readable ASCII value: thisByte = digIN ' send it out the serial port: serout2 txPin, inv9600, ["byte info = ", DEC thisByte, 13, 10] PAUSE 500 '''''''''''''''''''''''''''''''''''''''''''''''''' ' QUESTION 2 ADCIN 0, ADCVAR thisByte = ADCVAR/4 'send it out the serial port serout2 txpin, inv9600, ["ANALOG BYTE info = ", DEC thisBYTE, 13, 10] PAUSE 500 '''''''''''''''''''''''''''''''''''''''''''''''''' ' QUESTION 3 if thisBYTE < 50 THEN serout2 txpin, inv9600, ["A LITTLE COLD", 13, 10] ENDIF IF thisBYTE <100 & thisBYTE>50 THEN serout2 txpin, inv9600, ["CARING", 13, 10] ENDIF IF thisBYTE < 150 & thisBYTE >=100 THEN serout2 txpin, inv9600, ["LOVING", 13, 10] ENDIF IF thisBYTE < 200 & thisBYTE >=150 THEN serout2 txpin, inv9600, ["Completely in love", 13, 10] ENDIF IF thisBYTE < 256 & thisBYTE >=200 THEN serout2 txpin, inv9600, ["INFAUTATED", 13, 10] ENDIF goto main
' This example waits for a byte on the incoming serial connection, ' and checks to see that the byte equals 65. ' It then sends the values of three sensors on pins RA0 - RA2. ' this example uses two arrays to hold the ADC values, ' and the byte values that they're converted to for sending. ' serial RX is on pin RC7 ' serial TX is on pin RC6 ' constant to set the baud rate: inv9600 con 16468 ' define variables: adcVar var word(3) byteVar var byte(3) channel var byte inByte var byte main: ' read sensors, convert to bytes: For channel = 0 To 2 adcin channel, adcVar(channel) byteVar(channel) = adcVar(channel) / 4 Next ' read serial data in: serin2 portc.7, inv9600, [inByte] ' if you got the message from director, send out data: If inByte = 65 Then serout2 portc.6, inv9600, [byteVar(0), byteVar(1), byteVar(2)] End If GoTo main ............................ Processing Code /* Serial call-and-response by Tom Igoe Sends a byte out the serial port, and reads 3 bytes in. Sets foregound color, xpos, and ypos of a circle onstage using the values returned from the serial port Updated 3 Nov. 2003 */ int bgcolor; // background color int fgcolor; // fill color String serialString = "";// 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 ball void setup() { beginSerial(); // Default start serial at 9600 baud 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; serialWrite(65); // send a capital A to start the MC sending } void loop() { background(bgcolor); fill(fgcolor); // Draw the shape ellipse(xpos, ypos, 20, 20); } void serialEvent() { processString((char)serial); } void processString(char inByte) { // add the latest byte from the serial port to the string to be parsed: serialString += inByte; // update the string length: serialCount = serialString.length(); // if we have 4 bytes, parse the string: if (serialCount > 2 ) { fgcolor = (int)serialString.charAt(0); xpos = (float)serialString.charAt(1); ypos = (float)serialString.charAt(2); // clear the string when we're done, and ask for more: serialString = ""; // send a capital A to request new sensor readings: serialWrite(65); } }P R O C E S S I N G C O D E
I used the code found on Tom Igoe's site to test processing with a breadboard with three pots. The code controls the color and movements of a ball on the screen. The code used is below.