Hi! I'm somewhat new to Arduino but I am learning pretty quickly but I'm confused about something.
I have a robot arm with a ping sensor on the end of it and I have it programmed to stop when i put my hand in front of it, yet when I do it only stops once it is complete with turning. How can I make it stop immediately? I am using continuous rotation servos. Also I have not added a claw yet so don't ask why it isn't in the code. Also the if and else statements are at the bottom.
WARNING!!! Newbie code ahead!
#define trigPin 2
#define echoPin 3
#define ledPin 13
#define piezo 10
#include <Servo.h>
Servo rotateServo;
Servo armServo1;
Servo armServo2;
void setup()
{
//armServo1.attach(7);
//armServo2.attach(8);
//rotateServo.attach(6);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(piezo, OUTPUT);
startup();
}
void startup()
{
armServo1.attach(7);
armServo2.attach(8);
armServo1.write(88);
armServo2.write(85);
delay(700);
armServo1.write(90);
armServo2.write(90);
armServo1.detach();
armServo2.detach();
}
void rotateLeft()
{
rotateServo.attach(6);
rotateServo.write(95);
delay(1500);
rotateServo.detach();
}
void rotateRight()
{
rotateServo.attach(6);
rotateServo.write(88);
delay(1500);
rotateServo.detach();
}
void idle()
{
armServo1.detach();
armServo2.detach();
rotateServo.detach();
}
void loop()
{
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 74;
int inches = (distance);
Serial.println(inches);
delay(100);
if(distance >= 3)
{
rotateLeft();
delay(900);
rotateRight();
delay(900);
}
else
{
tone(10, 20, 200);
idle();
}}