4 servo Ok, not the 5th one & LED

Hi everyone :slight_smile:
In my arm project i’m controlling 4 servos with 2 Thumb Joysticks.
2 Arduino Unos, 2 nrf24l01s, 2 Thumb Joysticks, 4 Sg90 servos work properly.

Transmitter Uno
1 nrf24l01(D9-10-11-12-13-GND-3.3V pins)
2 Thumb Joysticks (A1-A2-A3-A4 pins)
9V battery
Receiver Uno
1 nrf24l01(D9-10-11-12-13-GND-3.3V pins)
4 Sg90 servos (D1-2-3-4)
7.4 (2S) battery

I’m trying to make the 5th servo move 0 to 180 degrees by pushing Thumb Joystick’s button(if LOW 0 degree, if HIGH 180 to make hand of arm hold something)
and turn on & off a led by pushing the second Thumb Joystick’s button.
My codes are below. Uncolored work properly controls 4 servos. Red marked about 5th servo and does not work. I could not write anything about LEDs cause i was planning to modify 5th servos codes but it didnt work. Could you please correct or add to my codes for 5th servo and LED.

Transmitter

#include <SPI.h>
#include <RF24.h>
const uint64_t pipe = 0xE8E8F0F0E1LL;
#define CE_PIN 9
#define CSN_PIN 10
RF24 radio(CE_PIN, CSN_PIN);

[color=red]const int buttonPin = 7;
int buttonState;[/color]

int ileti[1];

int potpin_1 = A1;
int val_1; 
int potpin_2 = A2;
int val_2; 
int potpin_3 = A3;
int val_3; 
int potpin_4 = A4;
int val_4;

void setup(void)
{
radio.begin();
radio.openWritingPipe(pipe);
[color=red]buttonState = digitalRead(buttonPin);[/color]
}
void loop() 
{
val_1 = analogRead(potpin_1),val_1 = map(val_1, 0, 1023, 1, 60),ileti[0] = val_1,radio.write(ileti, 1);
val_2 = analogRead(potpin_2),val_2 = map(val_2, 0, 1023, 61, 120),ileti[0] = val_2,radio.write(ileti, 1);
val_3 = analogRead(potpin_3),val_3 = map(val_3, 0, 1023, 121, 180),ileti[0] = val_3,radio.write(ileti, 1);
val_4 = analogRead(potpin_4),val_4 = map(val_4, 0, 1023, 181, 240),ileti[0] = val_4,radio.write(ileti, 1);

[color=red]if (buttonState == LOW){
  ileti[0]=250;
  radio.write(ileti, 1);
  delay(30);
}
if (buttonState == HIGH){
  ileti[0]=251;
  radio.write(ileti, 1);
  delay(30);
}[/color]
}

Receiver

#include <SPI.h>
#include "RF24.h"
const uint64_t pipe = 0xE8E8F0F0E1LL;
#define CE_PIN 9
#define CSN_PIN 10
RF24 radio(CE_PIN, CSN_PIN);
#include <Servo.h>

Servo servo_1;
Servo servo_2;
Servo servo_3;
Servo servo_4;
Servo servo_5;
int ileti[1];
int data;
int pos;
[color=red]int ICbuttonState = 0;[/color]

void setup()
{
servo_1.attach(1);
servo_2.attach(2);
servo_3.attach(3);
servo_4.attach(4);
[color=red]servo_5.attach(5);[/color]
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
}
void loop()
{
if (radio.available())radio.read(ileti, 1);

if (ileti[0] >0 && ileti[0] <61)data = ileti[0], pos = map(data, 1, 60, 0, 180),servo_1.write(pos);
if (ileti[0] >60 && ileti[0] <121)data = ileti[0], pos = map(data, 61, 120, 0, 180),servo_2.write(pos);
if (ileti[0] >120 && ileti[0] <181)data = ileti[0], pos = map(data, 121, 180, 0, 180),servo_3.write(pos);
if (ileti[0] >180 && ileti[0] <241)data = ileti[0], pos = map(data, 181, 240, 0, 180),servo_4.write(pos);

[color=red]if (ileti[0]==250){
  ICbuttonState=0;
  servo_5.write(ICbuttonState);
  delay(30);
}
if (ileti[0]==251){
  ICbuttonState=90;
  servo_5.write(ICbuttonState);
  delay(30);
}[/color]
}

Thank you:)

The codes in color tags dont work

In the Transmitter code, buttonState is never given a value.

buttonState = digitalRead(buttonPin)

buttonPin's digital result has given. Is not it correct?

@osondi, please re-write your code so there is just one instruction on each line and then I will look at it.

It looks like you are sending bytes one at a time. It would be much better to send an array containing all 5 values.

...R
Simple nRF24L01+ Tutorial

I am curious how you are powering the servos.
Can you post pics and/or a drawing of the wiring?

Oops, I missed that the setup() function gives a value to buttonState. But why give a value to buttonState in the setup() function (executed once) and then test it in the loop() function (executed repeatedly) ?

vinceherman:
I am curious how you are powering the servos.
Can you post pics and/or a drawing of the wiring?

2s 7.4 battery atached to breadboard also servos + and - and with jumper cables i power arduino from breadboard. Is it wrong. I ll send photo.

osondi:
2s 7.4 battery atached to breadboard also …

Have you overlooked Reply #5?

…R

Robin2:
Have you overlooked Reply #5?

...R

Yes Robin2. Thank you. Iam in looking after my two babies till mom arrives. I ll draw in fritzing and explane lines as i can. I modified codes. I ll add all from the first one and mines.

Robin2:
@osondi, please re-write your code so there is just one instruction on each line and then I will look at it.

It looks like you are sending bytes one at a time. It would be much better to send an array containing all 5 values.

…R
Simple nRF24L01+ Tutorial

I added fritzing drawings and tried to explain codes as i can.

Explained Transmitter Code

#include <SPI.h>  // Including libraries
#include <RF24.h> // Including libraries
const uint64_t pipe = 0xE8E8F0F0E1LL; //Channel addres
#define CE_PIN 9
#define CSN_PIN 10
RF24 radio(CE_PIN, CSN_PIN);

const int buttonPin = 7;  // Thumb Joystick's button pin
int buttonState ; // Variable for Thumb Joystick's button pin

int ileti[1]; // Variable to send radio signal

int potpin_1 = A1;  // 1.Thumb Joystick's X axis attached to Analog 1 pin
int val_1;          // Variable to read and convert A1 value
int potpin_2 = A2;  // 1.Thumb Joystick's Y axis attached to Analog 2 pin
int val_2;          // Variable to read and convert A2 value
int potpin_3 = A3;  // 2.Thumb Joystick's X axis attached to Analog 3 pin
int val_3;          // Variable to read and convert A3 value
int potpin_4 = A4;  // 2.Thumb Joystick's Y axis attached to Analog 4 pin
int val_4;          // Variable to read and convert A4 value

void setup(void)
{
radio.begin();
radio.openWritingPipe(pipe); // Opening 1 of the 6 transmission channels
buttonState = digitalRead(buttonPin); // Equal buttonPin's digital value to buttonState
}
void loop() 
{
val_1 = analogRead(potpin_1),val_1 = map(val_1, 0, 1023, 1, 60),ileti[0] = val_1,radio.write(ileti, 1);
// Equal val_1 to analog reading of potpin_1, convert from 0-1023 to 1-60, equal to ileti[0], write to radio signal and send.
val_2 = analogRead(potpin_2),val_2 = map(val_2, 0, 1023, 61, 120),ileti[0] = val_2,radio.write(ileti, 1);
// Equal val_2 to analog reading of potpin_2, convert from 0-1023 to 61-120, equal to ileti[0], write to radio signal and send.
val_3 = analogRead(potpin_3),val_3 = map(val_3, 0, 1023, 121, 180),ileti[0] = val_3,radio.write(ileti, 1);
// Equal val_3 to analog reading of potpin_3, convert from 0-1023 to 121-180, equal to ileti[0], write to radio signal and send.
val_4 = analogRead(potpin_4),val_4 = map(val_4, 0, 1023, 181, 240),ileti[0] = val_4,radio.write(ileti, 1);
// Equal val_4 to analog reading of potpin_4, convert from 0-1023 to 181-240, equal to ileti[0], write to radio signal and send.

if (buttonState == LOW){    // if digital reading of buttonState is LOW which is associated with buttonPin
  ileti[0]=250;             // set ileti[0] as 250
  radio.write(ileti, 1);    // write to radio signal and send.
  delay(30);                // wait 30ms to avoid confusing of Thumb Joystick's button
if (buttonState == HIGH){   // if digital reading of buttonState is HIGH
  ileti[0]=251;             // set ileti[0] as 251
  radio.write(ileti, 1);    // write to radio signal and send.
  delay(30);                // wait 30ms to avoid confusing of Thumb Joystick's button
}
}

Explained Receiver Code

#include <SPI.h>    // Including libraries
#include "RF24.h"   // Including libraries
#include <Servo.h>  // Including libraries
const uint64_t pipe = 0xE8E8F0F0E1LL; //Channel addres
#define CE_PIN 9
#define CSN_PIN 10
RF24 radio(CE_PIN, CSN_PIN);


Servo servo_1;      // Giving names to servos
Servo servo_2;      // Giving names to servos
Servo servo_3;      // Giving names to servos
Servo servo_4;      // Giving names to servos
Servo servo_5;      // Giving names to servos

int ileti[1];       // Variable to receive radio signal
int data;           // Variable for servo motors
int pos;            // Variable to write value to servo motors (1-2-3-4)
int ICbuttonState;  // Variable to write value to 5th servo motor

void setup(){
servo_1.attach(1); // servo_1 attached to Digital Pin 1
servo_2.attach(2); // servo_1 attached to Digital Pin 2
servo_3.attach(3); // servo_1 attached to Digital Pin 3
servo_4.attach(4); // servo_1 attached to Digital Pin 4
servo_5.attach(5); // servo_1 attached to Digital Pin 5
radio.begin();
radio.openReadingPipe(1,pipe);// Opening 1 of the 6 reception channels
radio.startListening(); // Start listening to the broadcast
}
void loop()
{
if (radio.available())radio.read(ileti, 1); //  Read the value of ileti array's first member
                                            //  when communication established

if (ileti[0] >0 && ileti[0] <61)data = ileti[0], pos = map(data, 1, 60, 0, 180),servo_1.write(pos);
/* if ileti[0] is among 0&61, equal data to ileti[0], convert data's value to 0-180 from 1-61 and equal to pos, write pos to servo_1*/
if (ileti[0] >60 && ileti[0] <121)data = ileti[0], pos = map(data, 61, 120, 0, 180),servo_2.write(pos);
/* if ileti[0] is among 60&121, equal data to ileti[0], convert data's value to 0-180 from 60-121 and equal to pos, write pos to servo_2*/
if (ileti[0] >120 && ileti[0] <181)data = ileti[0], pos = map(data, 121, 180, 0, 180),servo_3.write(pos);
/* if ileti[0] is among 120&181, equal data to ileti[0], convert data's value to 0-180 from 120-181 and equal to pos, write pos to servo_3*/
if (ileti[0] >180 && ileti[0] <241)data = ileti[0], pos = map(data, 181, 240, 0, 180),servo_4.write(pos);
/* if ileti[0] is among 180&241, equal data to ileti[0], convert data's value to 0-180 from 180-241 and equal to pos, write pos to servo_4*/

if (ileti[0]==250){               // if ileti[0] is 250
  ICbuttonState=10;               // ICbuttonState is 10
  servo_5.write(ICbuttonState);   // write ICbuttonState to servo_5 to OPEN clamp at the end of arm
  delay(30);                      // wait 30ms as its sad button of thumb joystick may confuse
}
if (ileti[0]==251){               // if ileti[0] is 251
  ICbuttonState=170;              // ICbuttonState is 170
  servo_5.write(ICbuttonState);   // write ICbuttonState to servo_5 to CLOSE clamp at the end of arm
  delay(30);                      // wait 30ms as its sad button of thumb joystick may confuse
}
}

This is the video of my basic armhttps://youtu.be/J1qHCfyXkf8

Images from Reply #11 so we don’t have to download them. See this Image Guide

4d61230d7efe6276f3ae54d50e2f6c60ae7aceb4.png

8bbd49c93dc43893fc341bbf9cdbb8d67dcebb37.png

…R

Your program is almost impossible to read because it is not indented (use the AutoFormat tool) and you have multiple statements on one line in several places.

It has been 5 days since your last Reply but you do not seem to have spent any of that time studying the link I gave you.

This is not going to work

radio.write(ileti, 1);

because you have defined the variable ileti as an int array and an int is 2 bytes.

And, as I mentioned in Reply #5, why are you only sending one value in each message?

Finally you cannot expect to power anything from those PP3 style 9v batteries - they cannot provide enough current.

...R