需要使2个伺服无线移动

时间:2018-12-18 11:37:14

标签: arduino

我正在使用2个arduino uno和433MHz Rf模块,我试图通过更改电位器值来分别移动两个伺服电机。

但是我的代码将伺服系统一起移动了。 我想当我更改第一个电位器值时进行第一次伺服移动,而当我更改第二个电位器时进行第二次伺服移动。

这是我的发射机代码:

//Transmitter Code
#include <RCSwitch.h>

RCSwitch myswitch = RCSwitch();

int pot = A0;
int pot1 = A1;

void setup() {
    Serial.begin(9600);

    myswitch.enableTransmit(10);

}

void loop() {

    int pott = analogRead(pot);
    int pott1 = analogRead(pot1);

    int servo= map(pott,0,1024,1,180);
    int servo1= map(pott1,0,1024,1,180);

    myswitch.send(servo, 10);
    delay(200);
    myswitch.send(servo1, 10);
    delay(200);

}

............................................... ........................

这是我的接收方代码

//Receiver Code:
#include <RCSwitch.h>
#include <Servo.h>

int pos = 0;

Servo myservo;
Servo myservo1;

RCSwitch myswitch = RCSwitch();


void setup() {
    myservo.attach(10);
    myservo1.attach(11);

    Serial.begin(9600);

    myswitch.enableReceive(0);

}

void loop() {
    if (myswitch.available()){
        int angle = myswitch.getReceivedValue();
        int angle1 = myswitch.getReceivedValue();

        myservo.write(angle);
        delay(200);
        myservo1.write(angle1);
        delay(200);
    }
}

1 个答案:

答案 0 :(得分:0)

发送两个值,因为它们可以区分, 就像在发射器中一样,

myswitch.send(servo, 10);
delay(200);
myswitch.send(servo1 + 1000, 10);

然后更换接收器

if (myswitch.available()){
    int value = myswitch.getReceivedValue();

    if(value > 1000) 
        myservo1.write(value - 1000);
    else 
        myservo.write(value);

    delay(200);
}
相关问题