So I am working on a circuit that activates a servo motor if enough current is inputted. But my servo is not moving. What am I doing wrong?
Here is the code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
const float minimum_current = 0;
void setup()
{
myservo.attach(8); // attaches the servo on pin 9 to the servo object
}
void loop()
{
int current = analogRead(0);
if(current >= minimum_current) {
// Pressure is in range. Move the servo.
for(pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
}
If you have a problem, then isolate that problem and try to fix it. Remove everything that introduces more complexity.
Remove everything from the Arduino Uno board. Connect the servo to 5V, GND and pin 8. Verify that you have the right pins for the servo motor. Make a small test sketch that only uses the servo motor.
I cannot follow your circuit, it is not clear. Post an annotated schematic showing exactly how you have wired it. Be sure to show power, ground, and power(s) sources. What is that thing your finger is on, post a link to technical information on that.
And why is there a resistor ( or something else? ) between the UNO and the servo? Usually servos are connected directly as @Koepel already pointed out.
The way your sketch is written the servo will immediately move to the 90° position and then stay there. No matter what current is read, it's always >= 0 (analogRead(0) returns a value of 0-1023, and minimum_current is set to 0), so every instance of loop() it will move to 90° (and take a mere 0.45 seconds to do so, meaning it'll basically just stay there).
This provided the servo is wired correctly, of course. The current sensor is not doing anything, really - even if not connected this will be the result.
With the battery in series with the ground connection your servo will probably feedback into the Arduino and fry it. How are you powering it, I see nothing on the schematic that shows it other then the 9V battery.