I downloaded Scratch to play with it after a presentation on E-Textiles at UPenn back in April. I found it kind of interesting, and I was particularly interested in the “Scratch Sensor Board“, which provides a way to send physical signals into Scratch scripts and have Scratch respond to them. It’s a pretty simple protocol, and I filed away “build a Scratch sensor board” as one of those projects that I might do … someday. Well, nothing like being stuck at home during a major weather event to crank up the boredom level to the point that ya gotta do something. So, courtesy of Hurricane Irene, here’s a DIY Scratch Sensor written as an Arduino Sketch.
This sketch uses information that you can download here. You may need to use the Scratch Board Watcher to get things cooking 100%. For example, this is how I learned that the sense of the Button input is inverted.
/* This code emulates a Scratch Sensor board */ // --- sends serial bytes corresponding to a signal with value given by "data" // coming from the channel designated by "channel" // data is in the range 0 .. 1023 inclusive and generally gets mapped to the range 0 .. 100 by Scratch void sendScratchData(int channel, int data); int buttonPin = 2; // Change this if you want to use some other pin for the Button input. Just don't use pins 0 or 1. int ledPin = 13; // LED connected to digital pin 13. I used this just to monitor the request/response traffic void setup() { pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); // Set baud rate expected by Scratch sensor: Serial.begin(38400); } void loop() { int inByte; // if we get a valid byte, send the scratch data // Technically, we should check that the value is a 0x01 byte, // but, meh ... if (Serial.available() > 0) { // toggle the LED so that we can see the request/response activity // this is optional, really digitalWrite(ledPin, ~digitalRead(ledPin)); // get incoming byte: // we really should test the value, but meh++ inByte = Serial.read(); sendScratchData(0,analogRead(0)); // Resistance "D" is mapped to Analog 0 sendScratchData(1,analogRead(1)); // Resistance "C" is mapped to Analog 1 sendScratchData(2,analogRead(2)); // Resistance "B" is mapped to Analog 2 sendScratchData(3,digitalRead(2) * 128); // Button is mapped to Digital 2 sendScratchData(4,analogRead(3)); // Resistance "A" is mapped to Analog 3 sendScratchData(5,analogRead(4)); // light is mapped to Analog 4 (signal is inverted) sendScratchData(6,analogRead(5)); // sound is mapped to Analog 5 sendScratchData(7,0); // slider did not map slider, but any analog input could be moved here. sendScratchData(15,4); // version info The current version is 4 } } void sendScratchData(int channel, int data){ byte highByte; byte lowByte; data = data & 0x3FF; highByte = 0x80 | (channel << 3) | (data /128); lowByte = data & 0x7F; // --- send the data Serial.write(highByte); delayMicroseconds(400); Serial.write(lowByte); delayMicroseconds(1000); }
Hell yeah, Mike! This looks pretty awesome!
Very cool. Thinking I’ll stop by The Hive next week.