Allison and I made a squirrel nut cracker that cracks jokes as it cracks nuts. A few things I would like to remember: the funniness of things becomes more complicated when you spend hours working on the code of it (but maybe it would feel the same with the sadness of things, or the profoundness of things, or the importance of things), and that its hard to start with a toy or object without knowing the purpose of the program. We were talking about several toys to use from the beginning, and it became more of an exercise in toy enhancement, which was frustrating. If we had a concept in the beginning, I think it would have been more interesting to find the appropriate toy, rather than the other way around. I was very happy with the idea we settled on though.. I think it was nice to do something ridiculous.
The most challenging part was the serial communication. I was getting random readings sent from the arduino that seemed unaltered by the goings on of the switch. I finally got the advice to change what the arduino was sending from integers to characters, which eliminated the reading problem. The processing code seemed to be picking up a series of delayed readings at the wrong speed (or something, I don’t really understand), but I wanted a digital “1” or “0” read. So finally, asking arduino to send char’s at switch state changes and processing to look for them, solved the problem.
CODE for ARDUINO ::
#include <Boards.h>
#include <Firmata.h>
int switchPin = 2; // digital input pin for a switch
int LED = 13; // digital output pin for an LED
int switchState = 0; // the state of the switch
char sendData = 0;
int pulseWidth = 5;
void setup() {
Serial.begin(9600);
pinMode(switchPin, INPUT);//set the switch pin to be an input
pinMode(LED, OUTPUT); // set the s LED pin to be an output
}
void loop() {
// read the switch input:
switchState = digitalRead(switchPin);
sendData = char(switchState);
Serial.println(sendData, DEC);
if (switchState == 1) {
// if the switch is closed:
for (int i = 0; i<100; i++){
digitalWrite(LED, HIGH); // turn on the LED
delayMicroseconds(i);
digitalWrite(LED, LOW);
delayMicroseconds(i);
}
} else {
// if the switch is open:
digitalWrite(LED, LOW);
delayMicroseconds(pulseWidth);
digitalWrite(LED, HIGH);
delayMicroseconds(pulseWidth);
}
}
CODE for PROCESSING ::
import ddf.minim.*;
import processing.serial.*;
import cc.arduino.*;
Serial port;
char val = '0';
char oldVal = '0';
Minim minim;
AudioPlayer groove;
void setup()
{
minim = new Minim(this);
groove = minim.loadFile("BustANut.aif", 2048);
println(Serial.list());
port = new Serial(this, Serial.list()[0], 9600);
size(10, 10);
minim = new Minim(this);
}
void draw()
{
println(val);
if (port.available() > 0) {
val = port.lastChar();
println(val);
}
if (val == '1' && oldVal == '0') {
groove.play();
oldVal = '1';
}
else if (val == '0' && oldVal == '1') {
groove.pause();
oldVal = '0';
}
}
void stop() {
groove.close();
minim.stop();
super.stop();
}
I would love to get to the bottom of the benefits and challenges of the different styles of serial communication, particularly char vs. int vs. ASCII character vs. DEC data types. I know this has to do with bytes and bits and data transfer speeds, and its because I still don’t have a solid grasp on this that I am having a hard time with it.
[wpvideo joZ6aNAD]
The squirrel’s switchstate was on when the mouth was shut (nut was cracked) and it said “Eat some nuts.” or “How those nuts taste?” or told a joke like “A guy walks into the psychiatrist’s office with his privates wrapped in saran wrap, and the shrink says, ‘I can clearly see your nuts.'”
bubum ch