BREADBOARD:
HOW IT WORKS:
This video has a 5 second delay not a 5 minute delay for the purposes of testing and shooting this video***
CODE:
I started writing this code being able to detect when the fridge is open. When the fridge is open the light inside the fridge is on. I used the photoresistor to detect when the light is on and would wait 5 minutes before blinking its LED and buzzing for a couple seconds. After it would begin moving the motor to close the fridge.I ran into issues with the mechanism knowing when to turn off and reset. I had a few variables that would hold its value after the fridge being closed. Meaning it would remember it already waited 5 minutes. In order to fix this issue, I had to reset the variable LEDcount back to zero after every time the motor moved. Also when the photoresistor was receiving zero light value the same variable would reset to 0 so no residual counts would be held after a cycle has gone by.
#include <Stepper.h>
// Conncetions
const int pResistor = A0; // Photoresistor at Arduino analog pin A0
int LED = 2;
int BUZZER = 3;
// Variables
int lightVal;
float checkTime = 0;
bool ledState = 0;
int ledCount = 0;
int buzzCount = 0;
// Stepper motor variables
const float STEPS_PER_REV = 32;
const float GEAR_REDUCTION = 64;
const float STEPS_PER_OUTPUT_REV = STEPS_PER_REV * GEAR_REDUCTION;
int StepsReq;
Stepper steppermotor(STEPS_PER_REV, 8, 10, 9, 11);
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
checkTime = millis();
}
void loop() {
lightVal = analogRead(pResistor);
Serial.println(lightVal);
if (lightVal >= 200) { // if the fridge light is on then...
if (millis() - checkTime >= 100) { // blink led
ledState = ! ledState;
digitalWrite(LED, ledState);
checkTime = millis();
ledCount++;
}
if (ledCount >= 3000) { // if the LED has been on for 5 minutes (3000 counts = 300000ms = 5 min) then...
buzzCount = 0;
while (buzzCount < 500) {
tone(BUZZER, 100, 100); // play tone
delay(1); // delay in between reads for stability
buzzCount++;
}
delay(2000); // delay before closing fride
StepsReq = STEPS_PER_OUTPUT_REV / 2; // Make the motor turn CW 180 degrees
steppermotor.setSpeed(700);
steppermotor.step(StepsReq);
delay(4000); // delay before resetting closing arm
StepsReq = - STEPS_PER_OUTPUT_REV / 2; // the minus makes it turn the other way
steppermotor.setSpeed(700);
steppermotor.step(StepsReq);
delay(4000); // delay before it resets and checks if the fridge remains open
digitalWrite (LED, 0); // turn the led off so the light turns back on in the next loop (DOESNT WORK!!!)
}
}
else {
ledCount = 0; // resetting variables incase fridge is closed before it counts up
digitalWrite (LED, 0);
}
}
ISSUES:
I was unable to get a material to attach to the end of my stepper motor to actually physically move the fridge closed. I, unfortunately, did not have the materials or tools to build the proper arm without it coming off or falling apart. Due to this, my project was unsuccessful in its final result. However,
I learned a lot in building and troubleshooting details within my code and physically wiring. I believe if I were to 3D print the proper arm, this project would be feasible.
No comments:
Post a Comment