Is there a way to grab values from a basic stamp with python?
I just bought a Accelerometer and want to read the values with python.
Is there a way to grab values from a basic stamp with python?
I just bought a Accelerometer and want to read the values with python.
Good follow-up from — sending the Stamp’s debug output to a host process is a simple, effective approach and answers ’s request for specifics. The posted method works for quick experiments but benefits from a few robustness and usability improvements so the stream is reliable, easy to parse, and produces smooth cursor motion or clean CSV logs for Excel.
Key points and pitfalls:
except: with targeted exception handling (ValueError/IndexError/SerialException) and use a serial timeout to avoid indefinite blocking. Recommended small examples (formatting + robust parse):
PBASIC-style output (single CSV line):
' Example PBASIC debug format
DEBUG DEC x, ",", DEC y, CR Python parsing sketch (robust, with smoothing and CSV logging):
with serial.Serial(port, baudrate, timeout=0.5) as s:
line = s.readline().decode().strip()
parts = line.split(',')
try:
ax, ay = int(parts[0]), int(parts[1])
except (ValueError, IndexError):
continue
# apply calibration, deadzone, EMA smoothing, then map to screen coords These additions make the solution repeatable, easier to calibrate, and safer to run long-term—use the CSV log for Excel analysis and iterate on calibration and filter parameters for the smoothest cursor mapping.
Jump to Post— programmersbook 17Hi!
Can you be more specific?
Hi!
Can you be more specific?
I have values in the Basic Stamps debugger and only in the debugger. I want python to be able to read the values. I'm going to map the mouse movements on the computer with the values, and possibly port the values into excel to be graphed. I can do all of that with python except read the values from the stamp.
After putting down this project and revisiting it after several months, I solved it. No click events yet, but it wont be hard at all.
here is the BASIC code that is loaded into the microcontrollers memory:
' {$STAMP BS2}
' {$PBASIC 2.5}
x VAR Word
y VAR Word
DO
PULSIN 8, 1, x
PULSIN 7, 1, y
DEBUG LF, ? x , ? y
LOOP And this is the python code to map the accelorometer to the mouse movements.
Requires PySerial.
from ctypes import *
import serial
s = serial.Serial('COM5') # this will be different for everyone, I used a Serial to USB cable.
user = windll.user32
x = 0
y = 0
speed = 10
while 1:
try:
d = s.readline()
d = d.split()
if int(d[2]) > 2700:
print 'right'
if x < 1400:
x += speed
elif int(d[2]) < 2400:
print 'left'
if x > 0:
x -= speed
if int(d[5]) > 2600:
print 'up'
if y > 0:
y -= speed
elif int(d[5]) < 2400:
print 'down'
if y < 900:
y += speed
user.SetCursorPos(x,y)
except: pass We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.