I need help with this code, it's for a robotic hand. I am using 5 flex sensors, 5 servo motors, 2 arduino nano and 2 nRF24L01.
I would appreciate it if you would help me to know what is wrong in the code (transmitter and receiver)
//Receiver Code (Hand) - Mert Arduino and Tech
#include <Servo.h> //the library which helps us to control the servo motor
#include <SPI.h> //the communication interface with the modem
#include "RF24.h" //the library which helps us to control the radio modem
//define the servo name
Servo myServo1;
Servo myServo3;
Servo myServo4;
Servo myServo2;
Servo myServo5;
RF24 radio(9,10); /*This object represents a modem connected to the Arduino.
Arguments 9 and 10 are a digital pin numbers to which signals
CE and CSN are connected.*/
const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem,that will receive data from the Arduino.
int msg[5];
void setup(){
//define the servo input pins
myServo1.attach(15); //A1
myServo2.attach(16); //A2
myServo3.attach(17); //A3
myServo4.attach(18); //A4
myServo5.attach(19); //A5
radio.begin(); //it activates the modem.
radio.openReadingPipe(1, pipe); //determines the address of our modem which receive data.
radio.startListening(); //enable receiving data via modem
}
void loop(){
if(radio.available()){
bool done = false;
while (!done){
done = radio.read(msg, sizeof(msg));
myServo1.write(msg[2]); //A1
myServo2.write(msg[4]); //A2
myServo3.write(msg[3]); //A3
myServo4.write(msg[1]); //A4
myServo5.write(msg[0]); //A5
}
}
}
//Transmitter Code (Glove) - Mert Arduino and Tech
#include <SPI.h> //the communication interface with the modem
#include "RF24.h" //the library which helps us to control the radio modem
int msg[5]; //Total number of data to be sent (data package)
//define the flex sensor input pins
int flex_5 = A5;
int flex_4 = A4;
int flex_3 = A3;
int flex_2 = A2;
int flex_1 = A1;
//define variables for flex sensor values
int flex_5_val;
int flex_4_val;
int flex_3_val;
int flex_2_val;
int flex_1_val;
RF24 radio(9,10); //9 and 10 are a digital pin numbers to which signals CE and CSN are connected.
const uint64_t pipe = 0xE8E8F0F0E1LL; //the address of the modem, that will receive data from Arduino.
void setup(void){
Serial.begin(9600);
radio.begin(); //it activates the modem.
radio.openWritingPipe(pipe); //sets the address of the receiver to which the program will send data.
}
void loop(void){
flex_5_val = analogRead(flex_5);
flex_5_val = map(flex_5_val, 630, 730, 80, 20);
flex_4_val = analogRead(flex_4);
flex_4_val = map(flex_4_val, 520, 710, 70, 175);
flex_3_val = analogRead(flex_3);
flex_3_val = map(flex_3_val, 510, 680, 140, 10);
flex_2_val = analogRead(flex_2);
flex_2_val = map(flex_2_val, 580, 715, 90, 175);
flex_1_val = analogRead(flex_1);
flex_1_val = map(flex_1_val, 550, 700, 90, 175);
msg[0] = flex_5_val;
msg[1] = flex_4_val;
msg[2] = flex_3_val;
msg[3] = flex_2_val;
msg[4] = flex_1_val;
radio.write(msg, sizeof(msg));
}
Thanks!