DESCRIPTION
In this lab, I worked with a DC Motor to work with rotational movement as an output to some other input. I did this by attaching a DC Motor to a small and light toy and have it react to the values from the photoreceptor. When dark, the motor whirrs and the music plays, as if there's a "party" in the toy box. (Very similar to the Toy Story premise of the toys having a life of their own while no one is watching.) The music is also initiated by receiving a "dark" value from the photoreceptor.
Video: http://instagram.com/p/fzB-uJxyd0/
MATERIALS
1 - Arduino Uno
1 - Breadboard
1 - USB Cable
1 - 220 Ohm Resistor
1 - diode
1 - transistor
1 - Photoreceptor
1 - Piezo Speaker
1 - Battery Pack
1 - DC Motor
6 - Wires
Shoe box and toys
CODE
/*
* one pot fades one motor
* modified version of AnalogInput
* by DojoDave <http://www.0j0.org>
* http://www.arduino.cc/en/Tutorial/AnalogInput
* Modified again by dave
*/
int potPin = 0; // select the input pin for the potentiometer
int motorPin = 9; // select the pin for the Motor
int speakerOut = 7;
byte names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = {1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
byte melody[] = "2d2a1f2c2d2a2d2c2f2d2a2c2d2a1f2c2d2a2a2g2p8p8p8p";
// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
// 10 20 30
int count = 0;
int count2 = 0;
int count3 = 0;
int MAX_COUNT = 24;
int motorVal = 0;
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(speakerOut, OUTPUT);
Serial.begin(9600);
Serial.println("ready");
}
void loop() {
digitalWrite(speakerOut, LOW);
for (count = 0; count < MAX_COUNT; count++) {
for (count3 = 0; count3 <= (melody[count*2] - 48) * 30; count3++) {
val = analogRead(potPin); // read the value from the sensor, between 0 - 1024
motorVal = 3 - val;
analogWrite(motorPin, motorVal*35); // analogWrite can be between 0-255
Serial.println(val); // writing the value to the PC via serial connection
if (val<1) {
for (count2=0;count2<8;count2++) {
if (names[count2] == melody[count*2 + 1]) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tones[count2]);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tones[count2]);
}
if (melody[count*2 + 1] == 'p') {
// make a pause of a certain size
digitalWrite(speakerOut, 0);
delayMicroseconds(500);
}
}
}
else {
delayMicroseconds(500);
}
}
}
}
- Login to post comments