what i am trying to do is use an object detector to make a servo move to a specific location and then once it does turn on a DC motor. The project is an arrow and a DC motor with a fan attached to a servo and the goal is for the arrow and motor to point at someone when they get close to the object detector and then turn a fan on with the DC motor. So far i have been able to make the servo move to the specific location based on what object detector is reading less than 40 (i have 2 object detectors) but i can't for the life of me figure out how to turn the DC motor on once the servo moves. I feel like it needs to be done with an IF statement but nothing i can think of is working.
</>
#include <Servo.h>
//this is the pin connected to my servo
int servoPin=9;
//since the servo is not included by deafult this is how i tell my arduino what the servo is.
Servo myServo;
//i need to start it in a position
int servoPosition=20;
long duration1; // long is just a larger variable. it stores more numbers. this will record the time it takes to travel
int distance1; //this is what i will use ro define the distance
int triggerPin1=A1;
int echoPin1=A0;
int Pos1=80;
long duration2; // long is just a larger variable. it stores more numbers. this will record the time it takes to travel
int distance2; //this is what i will use ro define the distance
int triggerPin2=A8;
int echoPin2=A9;
int Pos2=150;
//------------------------------Motor setup
//L29 pin1
int speedPin=2;
//L29 pin2
int directionPin1=3;
//L29 pin7
int directionPin2=4;
//this number will indicate speed and must be between 0-255
int motorSpeed=255;
int motorSpeed2=0;
void setup() {
Serial.begin (9600);
//this is how you assaign the servo to a pin. you take the item you defined as a servo to your arduino and tell it what pin that servo is connected to
myServo.attach(servoPin);
pinMode (triggerPin1,OUTPUT);
pinMode (echoPin1, INPUT);
pinMode (triggerPin2,OUTPUT);
pinMode (echoPin2, INPUT);
pinMode(speedPin,OUTPUT);
pinMode(directionPin1, OUTPUT);
pinMode(directionPin2, OUTPUT);
}
void loop() {
//how to start the motor on something. in this case starting it in the off position
digitalWrite(directionPin1, HIGH);
digitalWrite(directionPin2, LOW);
//we need to tell the speed pin what speed to go. so we assaign it a value 0-255
digitalWrite (speedPin, motorSpeed);
digitalWrite (triggerPin1, LOW);
delayMicroseconds(2); //this is how i set my output to low and make sure noting is being sent from it to start.
//this is how i send out a pulse
digitalWrite (triggerPin1, HIGH);
delayMicroseconds (10);
digitalWrite (triggerPin1, LOW);
//this is how i tell my echo pin to read the pulse thats sent out once it returns
duration1 = pulseIn(echoPin1,HIGH);
distance1=duration1*0.034/2;
Serial.print ("distance1: ");
Serial.println(distance1);
//--------------------------------------------------------------------------------------
digitalWrite (triggerPin2, LOW);
delayMicroseconds(2); //this is how i set my output to low and make sure noting is being sent from it to start.
//this is how i send out a pulse
digitalWrite (triggerPin2, HIGH);
delayMicroseconds (10);
digitalWrite (triggerPin2, LOW);
//this is how i tell my echo pin to read the pulse thats sent out once it returns
duration2 = pulseIn(echoPin2,HIGH);
distance2=duration2*0.034/2;
Serial.print ("distance2: ");
Serial.println(distance2);
//--------------------------------------------------------------------------------------
if(distance1>40 && distance2<40){myServo.write (Pos1);}
else if (distance1<40 && distance2>40){ myServo.write (Pos2);}
else{myServo.write (servoPosition);}
}
</>