Control Servo

/*
—- Transmitter Code —-
*/

#include <SPI.h>
#include “RF24.h”

int joystick[6];

RF24 radio(7,8);

const uint64_t pipe = 123456;

void setup(void)
{
radio.begin();
radio.openWritingPipe(pipe);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
}

void loop(void)
{
joystick[0] = analogRead(0);
joystick[1] = analogRead(1);
joystick[2] = analogRead(2);
joystick[3] = analogRead(3);
joystick[4] = digitalRead(2);
joystick[5] = digitalRead(3);
radio.write(joystick, sizeof(joystick) );
}

 

/*
—- Receiver Code —-
*/

#include <Servo.h>
#include <SPI.h>
#include “RF24.h”

int positionA = 90;
int positionB = 90;
int positionC = 90;
int positionD = 90;
int positionW = 90;
int positionX = 90;
int positionY = 90;
int positionZ = 90;
Servo myServoA;
Servo myServoB;
Servo myServoC;
Servo myServoD;
Servo myServoW;
Servo myServoX;
Servo myServoY;
Servo myServoZ;
long previousMillis = 0;
long interval = 15;

boolean lastButton2 = LOW;
boolean lastButton3 = LOW;
boolean ledOn2 = false;
boolean ledOn3 = false;
int buttonState2 = 0;
int buttonState3 = 0;

int joystick[6];

RF24 radio(7,8);
const uint64_t pipe = 123456;

void setup()
{
myServoA.attach(4);
myServoB.attach(5);
myServoC.attach(6);
myServoD.attach(9);
myServoW.attach(14);
myServoX.attach(15);
myServoY.attach(16);
myServoZ.attach(17);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(18, OUTPUT);
pinMode(19, OUTPUT);

radio.begin();
radio.openReadingPipe(1, pipe);
radio.startListening();
}

void loop()
{
if(radio.available())
{
bool done = false;
while (!done) {
done = radio.read(joystick, sizeof(joystick) );

unsigned long currentMillis = millis();
if(currentMillis – previousMillis > interval)
{
previousMillis = currentMillis;

if(ledOn2 == LOW)
{
if(joystick[0] < 412 && positionA < 180)
{
positionA+=3;
myServoA.write(positionA);
}
if(joystick[0] > 612 && positionA > 0)
{
positionA-=3;
myServoA.write(positionA);
}
if(joystick[1] < 412 && positionB < 180)
{
positionB+=3;
myServoB.write(positionB);
}
if(joystick[1] > 612 && positionB > 0)
{
positionB-=3;
myServoB.write(positionB);
}
}

if(ledOn2 == HIGH)
{
if(joystick[0] < 412 && positionC < 180)
{
positionC+=3;
myServoC.write(positionC);
}
if(joystick[0] > 612 && positionC > 0)
{
positionC-=3;
myServoC.write(positionC);
}
if(joystick[1] < 412 && positionD < 180)
{
positionD+=3;
myServoD.write(positionD);
}
if(joystick[1] > 612 && positionD > 0)
{
positionD-=3;
myServoD.write(positionD);
}
}

if(ledOn3 == LOW)
{
int joy2;
joy2 = map(joystick[2], 0, 1023, 0, 179 );
myServoW.write(joy2);
int joy3;
joy3 = map(joystick[3], 0, 1023, 0, 179);
myServoX.write(joy3);
}

if(ledOn3 == HIGH)
{
int joy2;
joy2 = map(joystick[2], 0, 1023, 0, 179 );
myServoY.write(joy2);
int joy3;
joy3 = map(joystick[3], 0, 1023, 0, 179);
myServoZ.write(joy3);
}

buttonState2 = joystick[4];
if (buttonState2 == HIGH && lastButton2 == LOW)
{
ledOn2 = !ledOn2;
lastButton2 = HIGH;
}
else
{
lastButton2 = buttonState2;
}
//digitalWrite(joystick[4], ledOn2);
digitalWrite(2, ledOn2);
digitalWrite(18, !ledOn2);

buttonState3 = joystick[5];
if (buttonState3 == HIGH && lastButton3 == LOW)
{
ledOn3 = !ledOn3;
lastButton3 = HIGH;
}
else
{
lastButton3 = buttonState3;
}
digitalWrite(3, ledOn3);
digitalWrite(19, !ledOn3);
}
}
}
}