
Here are the BS2 and the Processing codes I used to execute my "McVoting Booth." The voting booth allowed the voter to choose between Bush or Kerry. There was a button for each candidate. The Bush button always worked (to add votes to the Bush side), but the Kerry button worked some of the time, sometimes added votes for Bush instead, and sometimes did nothing at all.
THE BS2 CODE:
********************************************************************************************
SerInPin PIN 0 'serial IN pin (RXD - at the other end: TXD)
SerOutPin PIN 1 'serial OUT pin (TXD - at the other end: RXD)
SerInData VAR BYTE 'number to match to incoming data
SerWait CON 50 'ms to wait for input before timing out
N9600 CON 16468 'BS2 baudmode values: 16468/9600, 16416/19200, 16390/38400
'See SEROUT in the Basic Stamp manual for details.
test3:
PAUSE 100
IF IN2 THEN
SEROUT SEROUTPin, N9600,[DEC 1]
DEBUG "1"
ENDIF
IF IN4 THEN
SEROUT SerOutPin, N9600,[DEC 2]
DEBUG "2"
ENDIF
GOTO test3
********************************************************************************************
PROCESSING CODE:
********************************************************************************************
// McVoting Booth
// by Kellee Santiago
// A phony voting booth that reads off of two buttons.
// votes for Bush all of the time, the other button votes for Kerry sometimes,
// Bush at others, and sometimes does nothing.
// Created 20 Oct 2004
int bushVotes = 0; //declares the variable number of bushVotes and starts it at 0
int kerryVotes = 0; //declares the variable number of kerryVotes and starts it at 0
BFont theFont; //declared the variable of the font as "theFont"
int val; //reads the value off the serial port
void setup() {
beginSerial();//starts reading the serial port
size(400,200);//screensize
theFont = loadFont("Bauhaus.vlw.gz");//declared font - make sure to copy it into project folder!
fill(255,0,0);
stroke(255);
textFont(theFont, 48);
}
void loop() {
background(255);
BImage elephant;//declare image of rep. elephant
elephant = loadImage("tease.republican.gif");//loads image - have copy in project folder!
BImage donkey;
donkey = loadImage("tease.democrat.gif");
image(elephant, 120, 50); //sets the place of the image
image(donkey, 200, 50);
smooth();//smooths out font
fill(255,0,0);
text("bush", 40,75);
text(bushVotes,65,125);
fill(0,0,255);
text("kerry", 260,75);
text(kerryVotes,285,125);
}
void serialEvent() {
val = serial;
println(val);
if(val==49) {
bushVotes++; //reads value off serial port. Processing reads in ASCII, so when the
} //serial port sends out a 1, processing sees a 49.
else
if(val==50) {
if(random(10) < 3) {
kerryVotes++;
if(random(10) > 6){
bushVotes++;
//this is how i set up a basic random generator. When processing gets a 50 (ASCII for 2)
//it randomly generates a number from 1-10. If it's less than 3, Kerry gets a vote. If it's greater
//than 6, Bush gets a vote. Nothing happens if it's 3-6.
}
}
}
}
********************************************************************************************
I'm trying to get Processing to read a binary input, here is the reference code:
// Binary Input
// by Mathias Dahlstrom
// Example of a binary input from a BX-24 chip using serial communcation.
// Running this examples requires you have a BX-24 microcontroller
// and peripheral hardware. More information can be found on the tutorial
// pages of Tom Igoe: http://stage.itp.nyu.edu/%7Etigoe/pcomp/examples.shtml
// Make sure to have the correct port selected in the "sketch" menu. Because
// this program uses the serial port, it will not work within a web browser.
// Created 12 February 2003
// State of the circle drawing
boolean circleExpanding = true;
// Size of the circle
float circleSize = 0;
//The setup defines screen size and color setup.
void setup()
{
size(200, 200);
beginSerial(19200);
noStroke();
fill(204);
ellipseMode(CENTER_RADIUS);
}
// The loop checks for what state the circle should be
// drawn into and performs the drawing.
void loop()
{
background(0);
if(circleExpanding) {
ellipse(width/2, height/2, circleSize, circleSize);
circleSize += 0.5;
}else{
ellipse(width/2, height/2, circleSize, circleSize);
circleSize -= 0.5;
}
if(circleSize > width/2) {
circleSize = width/2;
}
if(circleSize < 10) {
circleSize = 10;
}
}
// Function is called when ever a new byte from the
// BX-24 is avaliable for reading.
// It controls what input the user is generating and
// sets the corresponding drawing mode.
void serialEvent()
{
// Checks the ASCII code sent from the basicX chip.
// '48' is the code for '0' and '49' is the code for '1'
if(serial == 0) {
circleExpanding = false;
}
if(serial == 1) {
circleExpanding = true;
}
println(serial);
}
*******************************
Here is my code for BS2:
SerInPin PIN 0 'serial IN pin (RXD - at the other end: TXD)
SerOutPin PIN 1 'serial OUT pin (TXD - at the other end: RXD)
SerInData VAR BYTE 'number to match to incoming data
SerWait CON 50 'ms to wait for input before timing out
N9600 CON 16468 'BS2 baudmode values: 16468/9600, 16416/19200, 16390/38400
'See SEROUT in the Basic Stamp manual for details.
DEBUG CR,"Program starting ...",CR
test:
IF IN2 THEN
HIGH 11
SEROUT SerOutPin,N9600,[DEC 1,CR]
DEBUG "1"
ELSE
LOW 11
SEROUT SerOutPin,N9600,[DEC 0,CR]
DEBUG "0"
ENDIF
GOTO test
************************************
But everytime I run the Processing program, it just makes a circle that expands. Blah!
Parallax Basic Stamp microcontrollers, sensors, components, modules, robotics
Acroname sensors, motor, robotics, Brainstem microcontroller
Mondotronics robots & robotics components
Jameco huge selection of electronics components
Digi-Key gargantuan selection of electronics components
All Electronics large selection of surplus electronics; cheap, local.
EarthLCD LCD panels, touchscreens
Electronic Goldmine surplus components, cheap
Images SI Inc sensors, actuators, components
Nuts & Volts
Servo Magazine
Practical Electronics
Circuit Cellar
Robotics World
Security Distribution Magazine
Security Magazine
Security World
Supplies:
To program a Basic Stamp, you can use the 9-pin RS232 serial port that's built in to the carrier board. However, to communicate between the Stamp and a multimedia tool (such as Director, Max/MSP or Processing), you'll need to build a serial cable that ends in bare wires, so that you can connect it directly to two Stamp I/O pins, which you'll use for serial input and output.
Here is a diagram of the cable you'll need to build:

Following are detailed instructions for one method of making this cable.
Use a 9 pin female D-sub connector [Radio Shack 276-1538].
The solder terminals are numbered from left to right, top row first.

You will want to find some way to hold the connector in place while
you solder it (such as a vice, a clamp, a weight, or a piece of tape).
Use .032 diameter light-duty rosin-core solder [Radio Shack 64-005].
Fill terminals 2, 3 and 5 with solder. (Heat the terminal with the soldering
iron and then hold the solder to the terminal until it flows into it).

I recommend using a green wire for ground [5],
a black wire for BS2 serial in [3], and a red wire
for BS2 serial in [2]. Strip 1/4 inch of insulation
on each wire. Coat the bare wire with solder.

Reheat the solder in each terminal.

When it is liquified, push the appropriate wire
into the terminal and hold it until it hardens.

I recommend soldering the necessary 22K resistor
inline with the BS2 serial in cable.

That's it. Now you're ready to test communications
between the Stamp and your authoring tool.
Here is a diagram of the circuit you will need to build to use the BS2 command RCTIME to read any variable resistor (such as a potentiometer, photoresistor, bend sensor, force sensing resistor, etc).

The following Basic Stamp file demonstrates the use of RCTIME to read a variable resistor.
Bend Sensor
Accelerometer
Force Sensing Resistor
PIR Motion Detector
Bend Sensor
PIR Motion Detector Module
Force Sensing Resistor
Dual Axis Accelerometer
Ultrasound Rangefinder
Thermistors
Tilt Sensor
Digital & Analog Compass
Things to try if something isn't working:
Parallax Basic Stamp Editor 2.1 for Windows
MacBS2 Editor for OS X
Basic Stamp Tokenizer MacBS2 should install this automatically, but it doesn't
SerialXtra for Director