Hai all, im new to the arduino scene, and also programming. I've been working on a small pet feeder as a starter project using uno r3. All good on the desk connected to pc. On the computer, i set the timer to 5seconds, it worked fine. I changed the timer to 5 hours and deployed it, connected to usb power (phone charger 5v 2A) , it did not dispense the food based in the specified time, sometimes every 3-4 minutes.. Sometimes 8 minutes.. Seems irregular, random interval..
I have added a separate power pack (4x AA batteries) for the servo, thinking the board probably resets every time the servo turns, but the problem persist. Im using a continuous 360 servo.
I have also resorted to use a separate 9V battery to power the board only, but the result is the same.
Code below. I update it several time including added println function to find out where it stuck, all ok when connected to pc, but when upload to board, the println seems to cause issues when running.
// This pet feeder timing dispense food at 0 hours (startup), next is 5 hours,
// then 5 hours then 14 hours, total 24 hours. Suggestion to turn on the machine
// at 8am, next feed is 1pm, and 5pm after that, then wait. Next feeding is 8am next
// morning.
// Trial time is 0 second start up, 5 second feed 1, 5 seconds feed 2 and
// wait for 14 seconds
#include <Servo.h>
Servo feederServo;
const int servoPin = 9;
unsigned long currentMillis = 0;
unsigned long previousMillis = 0;
const unsigned long servoRunTime = 1000; // 1 second rotation
const unsigned long fiveSeconds = 5000; // 5 second rotation
const unsigned long fourteenSeconds = 14000; // 14 second rotation
const unsigned long fiveHours = 18000000; // 5 hours in milliseconds
const unsigned long nineHours = 32400000; // 9 hours in milliseconds
int state = 0; // To track the sequence of operations
void setup() {
Serial.begin(9600); // Start serial communication
feederServo.attach(servoPin);
feederServo.write(90); // Start at neutral position
previousMillis = millis(); // Initialize previousMillis at startup
}
void loop() {
currentMillis = millis();
switch (state) {
case 0: // Rotate the servo for 1 second at startup
if (currentMillis - previousMillis < servoRunTime) {
feederServo.write(60); // Rotate servo (adjust angle for your servo)
//Serial.println("Dispensing 8am"); // Print "Dispensing" when servo rotates
} else {
feederServo.write(90); // Stop rotation
previousMillis = currentMillis;
//Serial.println("Next dispense 1pm"); // Print "Waiting" after servo rotation
state = 1; // Move to next state
}
break;
case 1: // Pause for 5 hours
if (currentMillis - previousMillis >= fiveHours) {
previousMillis = currentMillis;
state = 2; // Move to next state
}
break;
case 2: // Rotate servo for 1 second
if (currentMillis - previousMillis < servoRunTime) {
feederServo.write(60); // Rotate servo
//Serial.println("Dispensing 1pm"); // Print "Dispensing" when servo rotates
} else {
feederServo.write(90); // Stop rotation
previousMillis = currentMillis;
//Serial.println("Next dispense 6pm"); // Print "Waiting" after servo rotation
state = 3; // Move to next state
}
break;
case 3: // Pause for 5 hours
if (currentMillis - previousMillis >= fiveHours) {
previousMillis = currentMillis;
state = 4; // Move to next state
}
break;
case 4: // Rotate servo for 1 second
if (currentMillis - previousMillis < servoRunTime) {
feederServo.write(60); // Rotate servo
//Serial.println("Dispensing 6pm"); // Print "Dispensing" when servo rotates
} else {
feederServo.write(90); // Stop rotation
previousMillis = currentMillis;
//Serial.println("Next dispense tomorrow 8am"); // Print "Waiting" after servo rotation
state = 5; // Move to next state
}
break;
case 5: // Pause for 14 hours / seconds
if (currentMillis - previousMillis >= fourteenHours) {
previousMillis = currentMillis;
state = 0; // Restart the cycle
}
break;
}
}
I have also created a simulation here, works ok with short time intervals.
It's not entirely clear from your description exactly how you are powering the servo. If you're powering it from the Uno's 5V pin, there's your problem.
Original code copy paste from chatgpt. Original code from the default servo sketch wihtin IDE using delay function. That too had issues (same issue) so i switched to using timer above. I thought the board was faulty, i used a 2nd board, only to have same result. Probably thr code has bugs, but it should be seen when running connected to the PC.
1. At startup , what is the positon of Servo in degrees? 2. After 1-sec , what is the positon of Servo in degrees? 3. After 5-sec, what is the positon of Servo in degrees? 4. After 14-sec, what is the positon of Servo in degrees? 5. After 5-hrs, what is the positon of Servo in degrees? 6. After 9-hrs, what is the positon of Servo in degrees? 7. Please post the picture of your Servo and a web link to its model.
The servo is a 360 deg continuous servo, from my reading, its not to be controlled by angle, but speed and time.. So the speed at each case is 60, and time is 1 second.
The servo spins a propeller to dispense pet food. Food quantity specified by how long the servo spins.
What does 60 mean? It is understood that the argument of write(60) is a mechanical angle for the shaft position when it is less that 200; else, it is the on-time (in microseconds) of the PWM signal.
You picture clearly indicates the SG90 is a 180 degree Servo and NOT 360 degree Servo.
I had 7 questions in post #7; but, I have received only the answer of Q7. Would be glad to receive the answers of the remaining questions so that I can help you.
It can be common to "modify" standard 180degree Servos for continuous rotation, perhaps without changing the labels.
AFAIK, the idea that the "angle" used in the servo libraries has any effect on the speed of rotation of a continuous servo is ... spurious, and dependent on the electronics of the particular servo.
(the usual "continuous rotation" modification is to replace the position feedback pot with a fixed resistor divider. Sending an "angle" less than the divider set point makes the motor go one direction, and it moves the other way if the angle is on the other side of the set point. But whether the driver will move the motor slower or faster depending on "how far" from the setpoint the angle is, is questionable.)
Here is a short video of the start up. Moving ay speed 60 clockwise. Few minutes later another spin (not in the video)
I am sorry i have not been able to articulate myself clearly, partly because this is totally new to me, and probably unable to give the correct response, but please bear with me, i hope your questions can guide me to have a better or the correct answer to help me with this project.
I bought what I believe a 360 deg servo, and was told by the seller it is a 360 continuous servo. I am unable to tell the difference other than seeing how it move. I thought it should serve the purpose. If its not, then what do i have here?
The 0 mean its moving full speed clockwise, 90 means stop and 180 means full speed anticlockwise. So I set it at 60 not too fast clockwise i think ita suitable speed to dispense the pet food.
I don't think I have any problem with the servo if its a 180 or 360, its just the timing Interval is not triggering as it should be. Or is it really because of the servo?
I believe the wokwi servo is a 180 not a 360 that i have (i looked fir a 360 servo, couldn't find one - let me know any related keyword so i could modify them). Also the reason im so confused. Aside from the angle (not a big concern for me) the timing works fine when simulated and also when connected to the pc, but when i deployed it, it goes nuts..
Question 1-6 about the position of the servo, there is no position required for the servo to be at what angle, just how long it spins and at what speed. Because its a 360deg (or could be a modified version if 180deg - was not aware about this, don't even know how to tell except when it rotate).
Initially i followed the sample sketch with this servo, but it didn't not work at the tutorial show, i thought 360-deg and 180-deg works the same, apparent they dont. 180deg works by setting the angle, 360 work by setting the spin time.
Correct me if im wrong. Im telling you this because its what i have learnt so far (and its not that far obviously)