Making a Servo motor stop with resistance measurement from current sensor.

Hi im trying to make a servo motor stop if my current sensor gives X amperage output. Im using a ACS712-5 current sensor with and a MG99R motor. But I cant seem to figure out how to make the input from the sensor a stop sign to the servo.

i want the servo to turn towards a 148 degree but stop if resistance occours.

#define acs712 A0
float vpp = 0.0048828125;
float senstivity = 0.666; //mv

#include <Servo.h>;
const int buttonPin = 8;
const int servoPin = 9;
Servo servo;
int counter = 0;

void setup() {
servo.attach (servoPin);
pinMode(buttonPin, INPUT);

pinMode(acs712, INPUT);
Serial.begin(9600);

}

void loop() {
int counts = analogRead(acs712) + 1;
float voltage = counts * vpp;
voltage -= 2.5;

float amperage = voltage / senstivity;

Serial.println("amps:" + String(amperage));

int buttonState;
if (buttonState == LOW)
{
counter ++;
delay(150);

}

if (amperage > 0.2);
else if(counter == 0)
servo.write (0); // zero degrees
else if(counter == 1)
servo.write(148);
else
counter = 0;
}

Please edit your post to add code tags, and tell us what goes wrong with your attempts. The code you posted won't even compile, so solve that problem first (study if statement construction tutorials).

See the "How to use the forum" post for instructions.

Once you send the write(148) command to the servo there's no practical way to stop it completing that move.

If you want to be able to stop between 0 and 148 one way would be to use the for loop technique from the Sweep example code to move the servo a step at a time. Then you can check between steps and break out of the for loop if needed.

Steve

Thank you i will fix my post sorry im about to read the post instructions. I will look in to Slipsticks solution as it scans for every degree :slight_smile: