I've made a program that uses the Hall Effect Sensor to read magnetic fluctuations from a magnet attached underneath and feeds the values to two SG90 servo motors with minimal delay. The program works how I want it to but the servo motor would time out or stall in the middle of the execution for a few seconds and then continue after. This keeps happening and often I notice that moments before it stalls, the motor goes on a mini seizure. I don't know why it is happening or how to stop it.
this is the code is used
#include <Servo.h>
Servo hallservo, finger;
const int SENSOR = A0;
const int POS = A2;
int roboIndexAngle, angle;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(SENSOR, INPUT);
hallservo.attach(9);
hallservo.write(0);
finger.attach(8);
finger.write(0);
}
void loop() {
// put your main code here, to run repeatedly:
int hall = analogRead(SENSOR);
int Angle = SenseAngle();
int pos = fingerFunc();
int diff = Angle - pos;
if(diff > 50 || diff < -50){
hallservo.write(pos);
}
else{
hallservo.write(Angle);
}
Serial.println("Angle: " + String(Angle) + " Robo Angle: " + String(pos) + " diff: " + diff);
Serial.print("\n");
finger.write(Angle);
delay(55);
}
int SenseAngle() {
int sensValue = analogRead(SENSOR);
int angle = sensValue - 508;
angle = map(angle, 0, 318, 0, 180);
angle += 100;
return angle;
}
int fingerFunc() {
int roboIndexAngle = analogRead(POS);
roboIndexAngle = map(roboIndexAngle, 65, 622, 0, 180);
return roboIndexAngle;
}
So I hooked up the program to a better PC, as initially I was working on my laptop, and the program seemed to work without stalls now. I'm assuming the processor speed of my laptop wasn't sufficient or maybe its power supply was at fault. Regardless, thank you for your replies
You're not wrong my friend, but so far that's the only possible cause I could think of
Yes, I did print out the values. there were fluctuations at times, the sensor data is an analog input so I don't expect it to be a steady value (maybe that explains the seizures). but even the Serial monitor would freeze. id have to close it and turn it back on to get it to work again.
I tested my motors individually with simpler codes, they worked fine; even tried the code on new motors, same outcome.
these led me to believe that it's not just the motors that are stalling but the whole Arduino board. later I noticed the LEDs on the Arduino board flash and blink as if it's getting reset.
This is what all I noticed, cannot confirm why it's happening
The Arduino is not a power supply. For small servos like the SG90, choose a separate power supply capable of providing at least 1 Ampere per servo. For larger ones like the MG996R, at least 2.5 Amperes per servo.
So the 4xAA battery you mentioned, would it be sufficient to power multiple SG90 servos by your opinion? As, all in all I have up to 5 to 10 servos I'm working with
Honestly speaking, there are only a few days left until this project submission and I don't think I can wait for a delivery to arrive. Would regular 4xAA batteries be enough as Jremington mentioned?
I have used 4 AA batteries powering two MG996 servos in some projects, they work just fine.
However, three might be the maximum for 4 batteries; any more, you just get another 4. You will need common GNDs between battery packs and the Arduino.
Well, there are other battery options - 4 C or D size would be a better choice, unless you're keen to take a chance. It's not just the end-of-travel stall current, it's the fact that any mechanical binding, or even internal misalignments in the servo itself, can result in quite high currents. The spec sheet will identify a stall current max for your servo; a good worst-case plan would sum the stall currents of all your servos, add a margin, and provide that supply, but you haven't the time. Might be worth a footnote in your documentation, if this is for a school project, because identifying a deficiency is better than letting your instructor ding you for an oversight.