ICMSerial

ITP HELP | RecentChanges | Preferences

Processing (Text Style)

import processing.serial.*;


int xpos;
int shapeColor;    // Fill color
Serial port;       // The serial port
int shape;

void setup() {
  size(256, 256);  // Stage size
  noStroke();      // No border on the next thing drawn

  // Print a list of the serial ports, for debugging purposes to find out what your ports are called:
  println(Serial.list());
  //port = new Serial(this, Serial.list()[0], 9600);  //you can pull the name out of the list
    port = new Serial(this, "COM14", 9600);  //or you can just specify it
    port.write(65);    // Send a capital A in case the microcontroller is waiting to hear from you
    println("OKAY LET'S GO");
}

void draw() {
  background(255);
  fill(shapeColor,0,0);
  // Draw the shape
  if (shape == 0){
    ellipse(xpos, 100, 20, 20);
  }else{
    rect(xpos, 100, 20, 20);
  }

}

void serialEvent(Serial port) {
   
   String input = port.readStringUntil(10);  //make sure you return (Ascii 13) at the end of your transmission
   if (input != null){
     println("Raw Input: " + input);
     String[] parts = input.split(",");  //this will only work if you put commas (Ascii 44) between things in your transmission
    
     xpos =  int(parts[0]);
     xpos = scaleIt(255, xpos,130,515);  
     shapeColor =  int(parts[1]);
     shapeColor = scaleIt(255,shapeColor,200,670);  
     shape = int(parts[2]);
     println("Parsed and Scaled  xpos:" + xpos + " shapeColor:" + shapeColor + " shape:" + shape);
     port.write(65);    // Send a capital A (ascii 65) or any other arbitrary character in case the microcontroller is waiting to hear from you
     //int mouseDistanceAcross = int(255* float(mouseX)/width);  //it is possible you want to communicate more than just "okay ready for more"
    // port.write(mouseDistanceAcross); //in this example you might use the mouse distance across to control the panning of a servo motor
   }
}
/*This function takes the idosyncratic numbers that come in from you sensors
and scales them into a range that processing can use, for instance 0-255 for colors;
*/
int  scaleIt(int _outputRange, int _currentInput, int _lowestInput, int _highestInput){
  int inputRange = _highestInput -_lowestInput;
  _currentInput = min(_currentInput,_highestInput);  //protect against high rouge inputs by taking the minimum between the current and the highest allowable
  _currentInput = max(_currentInput,_lowestInput);  //protect against low rouge inputs by taking the maximum between the current and the lowest allowable
  int currentPlaceInInputRange = _currentInput - _lowestInput;
  float percentageOfCurrentInputInTotalInputRange = float(currentPlaceInInputRange)/inputRange; //have to turn one of these into a float to get a float back
  return  int(percentageOfCurrentInputInTotalInputRange * _outputRange);  //now apply the percetage for the input range to the range of number you want to use.

}


Arduino (Text Style)



int heat = 65;
int light = 65;
int switcher = 0;


void setup()
{
  beginSerial(9600);
  pinMode(11, INPUT);      // sets the digital pin 7 as input
}

void loop()
{
if (Serial.available() > 0){  //only send if you have hear back
 Serial.read();

    heat  = analogRead(2);
    light  = analogRead(3);
    switcher = digitalRead(12);

    Serial.print(heat,DEC);  //DEC turns the ints into Strings
    Serial.print(44, BYTE); //send a comma

    Serial.print(light,DEC);
    Serial.print(44, BYTE);
    
    Serial.print(switcher,DEC);
    Serial.print(44, BYTE);
    
    
   // Serial.print(13, BYTE);  //send a return
    Serial.print(10, BYTE);  //send a return
  }
  //delay(500);  //you might use this instead of if available
}

PIC BASIC (Text Style)


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 adcVar0,44,DEC adcVar1,44,DEC PORTB.6,13]
    serIN2 portc.7, 16468, [inputVar]
    debug inputVar
    PULSOUT portd.0 ,inputVar
    PAUSE 10
GoTo main  

Processing (Binary Style)

/* Serial call-and-response
by Tom Igoe (with adjustments by Dano)

Sends a byte out the serial port, and reads 3 bytes in.
Sets foreground color, xpos, and ypos of a circle onstage
using the values returned from the serial port

Updated October 12, 2004
*/

import processing.serial.*; 

Serial myPort;  // The serial port 

int  redNess = 255;               // fill color
int[] serialStuff= new int[3];;  //where we keep all the incoming stuff
int serialCount = 0;        // a count of how many bytes we receive
int xpos= width/2;;                   // Starting position of the ball
int shape =65;

void setup() {
  size(255, 255);  // stage size
  noStroke();               // no border on the next thing drawn
   myPort = new Serial(this, Serial.list()[0], 9600); 
  serialWrite(65);    // send a capital A to start the MC sending
}

void draw() {
  background(0);
  fill(redNess,0,0);
  // Draw the shape
  if (shape==65){  //switch value is 65 or 66 (I added 65 to the normal values 0 and 1)
    ellipse(xpos, 100, 50, 50);
  }else{
    rect(xpos,100,50,50);
  }
}

void serialEvent()  {  //this function gets called when something happens involving the serial port

  //serial is a special variable provided by processing to give you the oldest byte waiting to be read
  // add the latest byte from the serial port into the correct slot in your byte array
  serialStuff[serialCount] = myPort.read();
  serialCount++;

  // if we have 3 bytes, parse the string:
  if (serialCount> 2 ) {
    redNess = 2* serialStuff[0];
    xpos = serialStuff[1];
    shape = serialStuff[2];

    // after you got them all, go back to putting the bytes  in the first slot in the array again
    serialCount = 0;
    // get a number between 0 to 255 for the mouseX
    //if your stage is bigger than 255 //int mouseXIn0to255 = 255*mouseX/width
    //send out the mouseX to postion the servo motor
    myPort.write(mouseX);
  }
}

PIC BASIC (Binary Style)


DEFINE OSC 4

DEFINE ADC_BITS 8
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEuS 20

lightVar     var     byte
potVaR      VAR     BYTE
switchVar     var     byte
inByte      var     byte

input portb.7 

main:
       switchVar = portb.7 + 65        //make it readable
       adcin 0, potvar
       adcin 2, lightvar
       serout2 portc.6, 16468, [lightVar,potVar,switchVar]
       serin2 portc.7, 16468, [inbyte]
       Pulsout portd.0, inByte
       PAUSE 10

goto main

BX BASIC (Binary Style)

BX  COMPLICATED BASIC:  
SENDING THREE THINGS AND RECEIVING ONE

dim invar as byte
dim outvar as byte
dim variableResistor as integer
dim pulsewidth as integer
dim pos as single
	
	'set up a buffer
dim inputBuffer(1 To 10) as byte
dim outputBuffer(1 To 10) as byte

Sub main()
		call openQueue(inputBuffer,10)
	call openQueue(outputBuffer,10)
	'set up the port
   	call defineCom3(13, 11, bx1000_1000)	
	call openCom(3,9600,inputBuffer,outputBuffer) '9600 baud
	debug.print "start"

do 'deal with one variable resistor and send it 
	variableResistor = getADC(15)
	'debug.print cStr(variableResistor) 'determine that I get numbers 490-610
	variableResistor = variableResistor -490
	if variableResistor < 1 then 
		variableResistor = 1
	end if
	if variableResistor > 255 then 
		variableResistor = 255
	end if	
	call putQueue(OutputBuffer,variableResistor,1)

'deal with another variable resistor and send it 

	variableResistor = getADC(17)
	'debug.print cStr(variableResistor) 'determine that I get numbers 180 -580
	variableResistor = variableResistor -180
	if variableResistor < 1 then 
		variableResistor = 1
	end if
	variableResistor = cInt(cSng(variableResistor) * 255.0/400.0)
	if variableResistor > 255 then 
		variableResistor = 255
	end if	
	call putQueue(OutputBuffer,variableResistor,1)

'deal with the switch and send it 

	outvar = getPin(8) + 65  
	'so you don't send 0 (the nasty null char) or 1 but 65 or 66, "A" or "B"
	call putQueue(OutputBuffer,outvar,1)
	
	debug.print "listen"
	'get something back and use that to turn the motor
	call getQueue(inputBuffer,invar,1) 'program stops until it hears something
	debug.print "got"	
pos = cSng(invar)* 0.000006
	call pulseout(10,pos,1)
	call delay(0.02)

loop 
End Sub


ITP HELP | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited October 10, 2007 12:04 pm (diff)
Search:
To EDIT, You have to enter an ADMINISTRATOR password (guess) in Preferences. And refresh