I made a very basic mockup of our creature’s “bristle” mechanism with a paper towel tube, a CD cylinder cover, some wire and toy stuffing.
The code is telling the servo motor that if the fsr feed is 0 (actually it’s 0 – 15, because of some noise I was getting), the servo motor is in a neutral state of 80 degrees, leaving the bristle half-in, half-out.
At a pressure value between 15 and 700, the servo goes into a happy state and turns to 180 degrees, expanding the bristle all the way out.
If the pressure goes beyond 700, the servo goes into an angry state and goes down to 0, retracting the bristle.
I added a 3 second delay after the happy and the angry state is released to neutral, so that the creature has time to react and make the user aware of its reaction. The delay might have to be even longer.
I need to spend more time on perfecting the delay behavior. At the moment, it’s causing a delay not only between happy/angry states and the neutral (which we want) but also going from happy to angry (which we don’t want). I’ll try to use boolean vars to set up rules of when a delay should occur.
I set up three LEDs to correspond to our three states (not sure if you can see them lighting up in the video). For some reason, they are not getting enough power to light properly (only the red LEDs are bright enough for visibility—the green is almost imperceptible while the yellow doesn’t light at all).
ISSUES TO CONSIDER:
As Mike mentioned, we’ll need a sturdy wire to drive our bristle. The wires I have at home are too flexible and thin for the job. However, the servo motor has pretty small holes for attachments so a hanger or thicker wire will need a fancy rig to attach (they don’t fit).
With the attachments we have in our kit, the servo provides a fairly small range of up/down motion. If we want the bristles to expand and retract at a greater range, we’ll need to add an extension to our “servo arms”.
NEXT STEPTS:
- Perfect delays between happy/angry states and neutral state.
- Play around with a breathing cycle for the neutral state.
- Figure out the LED power issue (might have to ask Benedetta about this—I’m drawing a blank).
- add more FSR’s to create diversity in the “happy” bristle states.
CODE:
#include <Servo.h> // include the servo library
Servo servoMotor;
int servoPin = 8; // servo pin
int servoAngle = 80; // initial servo angle for “neutral” state
const int ledRed = 4; // red LED pin
const int ledGreen = 2; // green LED pin
const int ledYellow = 7; // yellow LED pin
void setup() {
Serial.begin(9600);
servoMotor.attach(servoPin);
}
void loop()
{
int analogValue = analogRead(A0); // read the analog input
Serial.println(analogValue);
/* I am putting a servoMotoer.write into each of the “state” conditions
so that a delay can be added after each state, giving our creature time
to play some sounds and for the users to process the change. I am still
working on this part to remove the delay between happy and angry states.*/
// HAPPY STATE condition
if (analogValue > 15 && analogValue < 700) {
servoAngle = 180;
servoMotor.write(servoAngle);
digitalWrite(ledRed,LOW);
digitalWrite(ledGreen,HIGH);
digitalWrite(ledYellow,LOW);
delay(4000); // four second delay
}
// ANGRY STATE condition
if (analogValue > 700) {
servoAngle = 0;
servoMotor.write(servoAngle);
digitalWrite(ledRed,HIGH);
digitalWrite(ledGreen,LOW);
digitalWrite(ledYellow,LOW);
delay(4000); // four second delay
}
// NEUTRAL STATE condition
if (analogValue == 0 || analogValue < 15) {
servoAngle = 80;
servoMotor.write(servoAngle);
digitalWrite(ledRed,LOW);
digitalWrite(ledGreen,LOW);
digitalWrite(ledYellow,HIGH);
}
delay(50); // this delay is just a safety precaution – not sure we need it.
}