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
Here's everything we did in the during the second half of our marathon session last night, cleaned up and commented.
switch.bs2
switch+LED.bs2
switch+LED+pot.bs2
switch+LED+pot+serial.bs2
newMaxtoBS2.pat
A demonstration of how to use softVNS motion tracking to rotate a stepper motor.
Basic Stamp program listing
Max/MSP/softVNS patch
snapshot of stepper motor setup
closeup of stepper motor & switch
http://www.gorobotics.net/articles/article.php?name=servohack
http://www.seattlerobotics.org/guide/servohack.html
http://www.acroname.com/robotics/info/ideas/continuous/continuous.html
http://www.dprg.org/projects/2003-05a/
external links:
• Stampworks Experiment #26: Stepper Motor Control (PDF)
• ULN2003 spec sheet (PDF)
• BEAM Reference Library article on Stepper Motors
• Jones on Stepping Motors
internal links:
• Basic Stamp program (revised version of StampWorks program)
• circuit diagram (revised version of StampWorks diagram)
• snapshot of small stepper setup
links to components @ All Electronics:
• ULN2003 Darlington Array
• a small 12V 7.5 deg/step unipolar stepper motor
• a larger 5V 7.5 deg/step unipolar stepper motor
• a larger 5V 1.8 deg/step unipolar stepper motor
NOTE: You will need a separate power supply for the motor.
Sorting out the wires:
Unipolar stepper motors have six leads. Internally, they have two windings, each with three of the six leads. One of these three leads is a center tap on its respective winding; this lead gets wired to V+. The other two leads are connected to each end of the winding, and when either is successively grounded, it creates an opposite magnetic field. By alternately energizing the two windings in each of two directions, the rotor can be stepped in a clockwise or counterclockwise direction.
To sort out the leads on a unipolar stepper motor with 6 wires: Use a multimeter to measure the resistance between pairs of leads. Each pair of leads will give one of three possible results: a fairly low resistance, twice this resistance, or a very high resistance (typically in megaohms).
Try to find a trio of leads with low resistance. Of these three leads, the one that gives identical results with the other two is the common. The other two together should give you twice the resistance of either of them with the common. Label the two groups of three leads each 1 and 2 (don't worry about which is which; see below), and label each of the commons as C.
Now, if you connect the common to V+, by momentarily connecting each of the other two leads to ground, you should be able to make the motor shaft jump slightly clockwise and counterclockwise. Call the clockwise lead A, and the counterclockwise lead B. Find the A and B leads for both groups 1 and 2.
Each wire is now labeled: 1A, 1B, 1C and 2A, 2B, 2C. Connect 1C and 2C to V+, and connect the other wires to your control pins in this order: 1A, 2A, 1B, 2B. If clockwise and counter-clockwise are flipped once you wire everything up, then switch 1 and 2 (ie, 2A, 1A, 2B, 1B).
Useful web site for MIDI hardware stuff:
DIY MIDI Controllers
(flat) image of BS2-MIDI setup
quick question:
I feel like I've got the reading of wiring diagrams down ok, or at least to the point where I know what's going on. However, I'm looking to try out some more complex stuff, and I'm wondering where one goes to get wiring diagrams for, say, a rotary switch, or whatever - something we haven't covered in class. I'm guessing I should probably go buy a electronics book, but if anyone has any suggestions, I'm more than welcome to them.
thanks.
'{$STAMP BS2}
'BASIC STAMP 2 FIRST PROJECT - COMMUNICATION WITH MAX/MSP
'WHAT SHOULD HAPPEN:
'When a serial message is received on pin 2, the light is turned on or off.
'When the switch is opened or closed, a serial message is sent out on pin 3.
'NOTE: after downloading this program, remove the programming cable from the
'Mac serial port, and plug in the custom serial cable to communicate with Max/MSP.
'Also, you will have to quit out of MacBS2 (or Virtual PC) before you run Max/MSP.
'DECLARE INS & OUTS
input 0 'switch
output 1 'relay
input 2 'BS2 serial receive, mac TXD (transmit)
output 3 'BS2 serial transmit, mac RXD (recieve)
'DECLARE VARIABLES
prevSwitchState var bit
serButtonByte var byte 'serial input from Mac - 1 for button down, 2 for button up
'INITIALIZE VARIABLES
prevSwitchState = 0 'start with switch off
'MAIN LOOP
TOP:
serIn 2,16468,50,CHECKSWITCH,[serButtonByte] 'branch to CHECKSWITCH if 50 ms pass
if serButtonByte = 2 then LIGHTON
if serButtonByte = 1 then LIGHTOFF
CHECKSWITCH:
if in0 <> prevSwitchState then ONOROFF 'branch if switch state changed
goto TOP
LIGHTON:
high 1 'turn on light
'debug "Light ON",cr
serOut 3,16468,["light_ON_"] 'report light on
goto TOP
LIGHTOFF:
low 1 'turn off light
'debug "Light OFF",cr
serOut 3,16468,["light_OFF"] 'report light off
goto TOP
ONOROFF:
if in0 = 1 then SWITCHON 'if 1 then switch on
if in0 = 0 then SWITCHOFF 'if 0 then switch off
goto TOP
SWITCHON:
prevSwitchState = 1
'debug "Switch ON",cr
serOut 3,16468,["switch_ON"] 'report switch on
goto TOP
SWITCHOFF:
prevSwitchState = 0
'debug "Switch OFF",cr
serOut 3,16468,["switchOFF"] 'report switch off
goto TOP