For our in-progress demo we figured out the main computational elements of our music box design. A flexor is attached to the hinge of the box so that when the box is opened, the flexor angle changes and initiates the DC motor to rotate. For the next part of our project, we will focus more on the mechanical design to be incorporated with the motor and sound effects.


// Guideline for using flex sensor https://learn.sparkfun.com/tutorials/flex-sensor-hookup-guide
//Guideline for DC Motor Setup https://learn.adafruit.com/adafruit-arduino-lesson-13-dc-motors/arduino-code
const int FLEX_PIN = A0; // Pin connected to voltage divider output
int motorPin = 3; // Pin connected to DC motor

const float VCC = 4.98; // Measured voltage of Ardunio 5V line
const float R_DIV = 47500.0; // Measured resistance of 3.3k resistor

const float STRAIGHT_RESISTANCE = 37300.0; // resistance when straight
const float BEND_RESISTANCE = 90000.0; // resistance at 90 deg

void setup()
{
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
pinMode(FLEX_PIN, INPUT);
}

void loop()
{
// Read the ADC, and calculate voltage and resistance from it
int flexADC = analogRead(FLEX_PIN);
float flexV = flexADC * VCC / 1023.0;
float flexR = R_DIV * (VCC / flexV - 1.0);
Serial.println("Resistance: " + String(flexR) + " ohms");

// Use the calculated resistance to estimate the sensor's
// bend angle:
float angle = map(flexR, STRAIGHT_RESISTANCE, BEND_RESISTANCE,
0, 90.0);
Serial.println("Bend: " + String(angle) + " degrees");
Serial.println();

//check for condition to initiate DC motor based on flexor sensor angle
if (angle > 100){
analogWrite(motorPin, 0);
} else {
analogWrite(motorPin, 80);
}
delay(500);
}