Sock Puppet

For this project I used a bend sensor to read the state of a sock puppet’s “mouth” and turn a motor accordingly to produce an acoustic noise to act as it’s voice.


/*
* Flex Sensor Puppet
*
* based on:
* AnalogInput by DojoDave
*
*
* http://www.arduino.cc/en/Tutorial/AnalogInput
*/

#define NUM_FLEX_READINGS 15

int flexPin = 2; // the analog input pin for the flex sensor
int gLEDPin = 3; // the pin for the green LED
int yLEDPin = 5; // the pin for the yellow LED

int motorPin = 6; // the pin for the transistor that activates the motor

int flexReadings[NUM_FLEX_READINGS];
int currentReadingIndex = 0;
int flexReadingsSum = 0;

int flexVal = 0; // variable to store the value coming from the flex sensor
int flexMin = 1019; // minimum flex sensor value
int flexMax = 1022; // maximum flex sensor value

void setup(){
pinMode(gLEDPin, OUTPUT); // declare the gLEDPin as an OUTPUT
pinMode(yLEDPin, OUTPUT); // declare the yLEDPin as an OUTPUT
pinMode(motorPin, OUTPUT); // declare the motorPin as an OUTPUT

for(int k=0; k< NUM_FLEX_READINGS; k++){
flexReadings[k] = 0; // init the flexReadings to 0;
}

digitalWrite(motorPin, LOW); // set the motor to off

//Serial.begin(9600); // begin serial communication via port 9600;
}

void loop(){
flexReadingsSum -= flexReadings[currentReadingIndex];
flexReadings[currentReadingIndex] = analogRead(flexPin); // read the value from the flex sensor
flexReadingsSum += flexReadings[currentReadingIndex];
currentReadingIndex++;

currentReadingIndex %= NUM_FLEX_READINGS;

flexVal = flexReadingsSum / NUM_FLEX_READINGS;

//Serial.println(flexVal); // print out the raw value of the flex sensor

flexVal = map(flexVal, flexMin, flexMax, 0, 255); // remap the flex value
flexVal = constrain(flexVal, 0, 255);

digitalWrite(gLEDPin, flexVal); // turn the gLEDPin on to represent the value
digitalWrite(yLEDPin, 255 - flexVal); // turn the yLEDPin on to represent the inverse

 

digitalWrite(motorPin, 255-flexVal); // turn on the motor proportional to the flex
}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s