For midterm project, my original idea is to make a mystery box that blows bubbles at you upon opening the lid. However, due to various technical problems and time constrains, that idea was not realized. On the other hand, it is very satisfying to see the three motors at work and how bubbles are produced. The system consists of two servo motors that acts as a mechanical arm that dips the slotted spoon in the soap water and then hold the spoon on top of the fan. Also, a dc motor that works as a fan that blows the bubbles.
I spent a lot of time building the circuit and debugging my code. I did not know that I need a diode in order for the dc motor to work. Even though my entire circuit worked fine for a couple of hours, my dc motor stopped working at some point. I suspect that the absence of a diode also damaged my transistor. As a result, I had to replace the motor and transistor and rewire my my circuit connected to the dc motor according to following graph:
Another difficulties I encountered was to set the delays for the movements of my servo motors. Since delay() function would stop the whole system and it would not respond to user input during delay period, I have to come up with an alternative. So I used millis() function instead:
[code]
#include <Servo.h>
Servo myservoBot;
Servo myservoTop;
int photocellPin = A1;
int photocellReading;
const int gearPin = 2;
int servoAngle = 0;
unsigned long previousMillis = 0;
void setup() {
myservoBot.attach(9);
myservoTop.attach(10);
pinMode(gearPin,OUTPUT);
Serial.begin(9600);
}
void loop() {
photocellReading = analogRead(photocellPin);
unsigned long currentTime = millis();
int currentTimeMod = currentTime%12000;
if(currentTimeMod == 10000){
myservoBot.write(0);
digitalWrite(gearPin,HIGH);
}else if(currentTimeMod == 8000){
myservoTop.write(40);
}else if(currentTimeMod == 4000){
myservoTop.write(0);
}else if(currentTimeMod == 3000){
myservoBot.write(40);
digitalWrite(gearPin,LOW);
}
//
// Serial.print(“Analog reading = “);
// Serial.println(photocellReading); // the raw analog reading
}
[/code]
I was going to set up a button/photo censor that would be able to stop or start the sequence of motion of the machine. This could be used to mimic any user input (i.e. opening box). Nevertheless, this feature can be added in the future when I improve on this project.