简体   繁体   中英

Send angles of a motor servo to an other motor servo in the same program

The goal of our project is to follow a person with a emitter IR, we have also one receiver and 2 motor servo. The first one is use to save the angle of the person and the second to follow the person. It is necessary to know that we have an other program for the emitter. However, the save of the angle is still false and our second motor servo does not work. We want the 2nd motor to go to the last angle saved when our receiver has results.

#include <Servo.h> 
#include <IRLibRecvPCI.h>
IRrecvPCI myReceiver(2);//pin number for the receiver
Servo monServo;
Servo monServo1;

void setup() {
  Serial.begin(9600);
  delay(2000); while (!Serial); 
    myReceiver.enableIRIn(); // Start the receiver
  Serial.println(F("Ready to receive IR signals"));
    monServo.attach(9);
    monServo1.attach(7);// relier le servomoteur au port 9 
  monServo.write(0);  // positionner le servomoteur à l'angle absolu 0°
}
int angle = 0;
int increment = 1;
 
void loop() {
 
       monServo.write(angle);
   angle = angle + increment;
   if (angle == 0); increment = 1;
   if (angle == 180); increment = -1;

   if (myReceiver.getResults()) {
   monServo1.write(angle);

   Serial.print("detection");
   myReceiver.enableIRIn();
   Serial.println(monServo.read());
;

   
      }
   
     } 
 if (angle == 0); increment = 1;
   if (angle == 180); increment = -1;

Is equivalent to increment = -1;

You have two empty if statements here. Remove the first semicolon in each line if you want to change the increment value conditionally.

You should also add some delay as your servo won't be able to move 1° in a few milliseconds.

There is also one superfluous semicolon at the end of your code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM