Go Down

Topic: problem push button and led controlled wirelessely by Arduino with NRF24L01  (Read 423 times) previous topic - next topic

sreekanthmp

i make one program for controlled led with push button wirelessley.In ma program,  once i press and release the button (non locking) (joystick[2]) to ON the led6  for one second  than goes off than after led2 get ON until next press of button .
same as when i press and release  the button (joystick[4]) led 7 goes ON for one second than goes OFF than after led3 get on until next press of the button.

But my program  not working properly,led3 goes ON always not responding with button.
please correct me if i have done any mistakes.

transmitter code
Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#define CE_PIN   9
#define CSN_PIN 10
#define JOYSTICK_X A0
const uint32_t debounceTime = 100;

const uint64_t pipe = 0xE8E8F0F0E1LL;


RF24 radio(CE_PIN, CSN_PIN);

int joystick[10]; 
int upbut = 2;
int fwdbut = 3;
int downbut = 4;
int rvesbut = 5;



void setup()   
{
  Serial.begin ( 9600 );
  radio.begin();
  radio.openWritingPipe(pipe);
 
  pinMode(upbut,INPUT_PULLUP);
  digitalWrite(upbut,LOW);
  pinMode(rvesbut,INPUT_PULLUP);
  digitalWrite(rvesbut,LOW);
  pinMode(downbut,INPUT_PULLUP);
  digitalWrite(downbut,LOW);
  pinMode(fwdbut,INPUT_PULLUP);
  digitalWrite(fwdbut,LOW);
 
 
 
 }//--(end setup )---


void loop()   
{
 
  radio.stopListening();
  radio.write( &joystick, sizeof(joystick) );
 
  joystick[1] = digitalRead(upbut);      //digital input
  joystick[2] = digitalRead(rvesbut);   //digital input
  joystick[3] = digitalRead(downbut);    // digital input
  joystick[4] = digitalRead(fwdbut);    // digital input
 
 
 }//--(end main loop )---


receiver code
Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#define CE_PIN 9
#define CSN_PIN 10
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
int joystick[8];  // 6 element array holding Joystick  6 button state
int led2 = 2; // digital out put
int led3 = 3; // digital out put
int led4 = 4; // digital out put
int led5 = 5; // digital out put
int led6 = 6; // digital out put
int led7 = 7; // digital out put
boolean flag6 = true;
boolean flag7 = true;
unsigned long currentMillis;
unsigned long pin6Millis;
unsigned long pin7Millis;
unsigned long debounceMillis = 1000UL;     //100ms
unsigned long ledOnTime      = 1000UL;  //5 seconds
int fwdbut = LOW;
int rvesbut = LOW;
void setup()   /****** SETUP: RUNS ONCE ******/
{
  digitalWrite(2,LOW);
  pinMode(led2, OUTPUT);
  digitalWrite(3,LOW);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  digitalWrite(5,LOW);
  pinMode(led5, OUTPUT);
  digitalWrite(6,LOW);
  pinMode(led6, OUTPUT);
  digitalWrite(7,HIGH);
  pinMode(led7, OUTPUT);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();;
}       
void loop ()   /****** LOOP: RUNS CONSTANTLY ******/
{
   
 
  if ( radio.available() )
  {
      currentMillis = millis();
      radio.read( &joystick, sizeof(joystick) );
      int upbut = joystick[1];
      int rvesbut = joystick[2];
      int downbut = joystick[3];
      int fwdbut = joystick[4];
     
 if ((flag6 == true) && (joystick[2] == HIGH)){
       
        flag6 = false;
        digitalWrite(led6, HIGH);
        digitalWrite(led2, LOW);
        pin6Millis = currentMillis;
     }
 
    if (( flag6 == false) && (currentMillis - pin6Millis >= ledOnTime)){
        digitalWrite(led6, LOW);
        digitalWrite(led2, HIGH);
        flag6 = true;
      }
       if (( flag7 == true)&& (joystick[4] == HIGH)){
        flag7 == false;
        digitalWrite(led7, HIGH);
        digitalWrite(led3, LOW);
        pin7Millis = currentMillis;
     }
   if   (( flag7 == false) && (currentMillis - pin7Millis >= ledOnTime)){
       digitalWrite(led7, LOW);
       digitalWrite(led3, HIGH);
       flag7 ==true ;
   
     }
     
     
      if (joystick[3]==HIGH){
       
        digitalWrite(led4,HIGH );
        }
        else {
         digitalWrite(led4,LOW );
   }
         if (joystick[1]==HIGH){
       
        digitalWrite(led5,HIGH );
        }
       else {
         digitalWrite(led5,LOW );
   }
       
          }}
       

septillion

Code: [Select]
  pinMode(upbut,INPUT_PULLUP);
  digitalWrite(upbut,LOW);

Very weird way of setting a pin to INPUT (without pull). Do you really want that?

Next, you recycled the variable joystick without using it as joystick. Having int to old button states is pretty overkill.

And you make it 10 items big. but don't use 0, 5, 6, 7, 8 and 9 of it...

And on the receiving side you only read 8 of the 10 items. Te rest will not disappear.

Code: [Select]
int led2 = 2; // digital out put
int led3 = 3; // digital out put
int led4 = 4; // digital out put
int led5 = 5; // digital out put
int led6 = 6; // digital out put
int led7 = 7; // digital out put

Variable names are there to HELP you. If you don't want help by creating descriptive and easy to use variable names, then don't. But I would know what I would like...

And you're code is all over the place. Press Ctrl+T to let the IDE help you.

But my suggestion, get back to the basics and extend little by little.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Robin2

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

sreekanthmp

But my suggestion, get back to the basics and extend little by little.
[/quote]

i have made some change.now using only one condition;Once i press and release the button (non locking) (joystick[0]) to ON the led3  for one second  than goes off than after led2 get ON until next press of button .

Reci:
Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#define CE_PIN 9
#define CSN_PIN 10
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
int joystick[1];  // 4 element array holding Joystick  4 button state
int led2 = 2; // digital out put
int led3 = 3; // digital out put
int led4 = 4; // digital out put
int led5 = 5; // digital out put
int led6 = 6; // digital out put
int led7 = 7; // digital out put
boolean flag6 = true;
boolean flag7 = true;
unsigned long currentMillis;
unsigned long pin6Millis;
unsigned long pin7Millis;
unsigned long debounceMillis = 1000UL;     //100ms
unsigned long ledOnTime      = 1000UL;  //5 seconds

void setup()   /****** SETUP: RUNS ONCE ******/
{
  digitalWrite(2, LOW);
  pinMode(led2, OUTPUT);
  digitalWrite(3, LOW);
  pinMode(led3, OUTPUT);
 
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();;
}
void loop ()   /****** LOOP: RUNS CONSTANTLY ******/
{


  if ( radio.available() )
  {
    currentMillis = millis();
    radio.read( &joystick, sizeof(joystick) );
   
    int rvesbut = joystick[0];
   
    if ((flag6 == true) && (joystick[0] == HIGH))
    {

      flag6 = false;
      digitalWrite(led3, HIGH);
      digitalWrite(led2, LOW);
      pin6Millis = currentMillis;
    }

    if (( flag6 == false) && (currentMillis - pin6Millis >= ledOnTime))
    {
      digitalWrite(led3, LOW);
      digitalWrite(led2, HIGH);
      flag6 = true;
    }


   
  }
}



Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#define CE_PIN   9
#define CSN_PIN 10
#define JOYSTICK_X A0
const uint32_t debounceTime = 100;

const uint64_t pipe = 0xE8E8F0F0E1LL;


RF24 radio(CE_PIN, CSN_PIN);

int joystick[1]; 

int rvesbut = 5;



void setup()   
{
  Serial.begin ( 9600 );
  radio.begin();
  radio.openWritingPipe(pipe);
 
 
 
  pinMode(rvesbut,INPUT);
 
 
 
 
 
 
 }//--(end setup )---


void loop()   
{
 
  radio.stopListening();
  radio.write( &joystick, sizeof(joystick) );
 
 
  joystick[0] = digitalRead(rvesbut);   //digital input
 
 
 }//--(end main loop )---



now its working.But problem is that suppose i have introduced one more input button and one more out put led that time problem will arise

sreekanthmp

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

...R
i checked the wireless communication,found ok.

Code: [Select]

if (joystick[3]==HIGH){
       
        digitalWrite(led4,HIGH );
        }
        else {
         digitalWrite(led4,LOW );
   }
         if (joystick[1]==HIGH){
       
        digitalWrite(led5,HIGH );
        }
       else {
         digitalWrite(led5,LOW );
   }

in existing program   this part  working smoothly.Its means communication between transmitter and receiver happening.

Robin2

in existing program   this part  working smoothly.Its means communication between transmitter and receiver happening.
I had assumed, based on your Title, that you were having a problem with the wireless communication.

If the data is being received correctly then what exactly is the problem?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

sreekanthmp

Some conditions not working.
 once i press and release the button (non locking) (joystick[2]) to ON the led6  for one second  than goes off than after led2 get ON until next press of button .
same as when i press and release  the button (joystick[4]) led 7 goes ON for one second than goes OFF than after led3 get on until next press of the button.

But my program  not working properly,led3 goes ON always not responding with button.

Robin2

Some conditions not working.
 once i press and release the button (non locking) (joystick[2]) to ON the led6  for one second  than goes off than after led2 get ON until next press of button .
same as when i press and release  the button (joystick[4]) led 7 goes ON for one second than goes OFF than after led3 get on until next press of the button.

But my program  not working properly,led3 goes ON always not responding with button.
I cannot tell from this description whether the problem is in the Arduino that is sending the data or the one that is receiving the data. You need to explain things more clearly.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

sreekanthmp

Now software side problem is solved . All have working as per my programmed conditions.

TX
Code: [Select]
#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#define CE_PIN   9
#define CSN_PIN 10
#define JOYSTICK_X A1
const uint64_t pipe = 0xE8E8F0F0E1LL;

RF24 radio(CE_PIN, CSN_PIN);
int joystick[8];

unsigned long debounceMillis =500UL;     //100ms
unsigned long ledOnTime      = 5000UL;  //5 seconds

const byte startSwitch = 2; //pushed = LOW
const byte testSwitch  = 3; //pushed = LOW
const byte testSwitch1  = 4; //pushed = LOW
const byte testSwitch2  = 5; //pushed = LOW
const byte testSwitch3  = 6; //pushed = LOW
//**********************************************************************

void setup()
{
  Serial.begin(9600);
 
 
  radio.begin();
  radio.stopListening();
  radio.openWritingPipe(pipe);
  pinMode(startSwitch, INPUT); //pushed = LOW
  pinMode(testSwitch,  INPUT); //pushed = LOW
  pinMode(testSwitch1,  INPUT); //pushed = LOW
  pinMode(testSwitch2,  INPUT);
  pinMode(testSwitch3,  INPUT);
  pinMode (JOYSTICK_X,  INPUT);
 

} //  >>>>>>>>>>>>>> E N D  O F  s e t u p ( ) <<<<<<<<<<<<<<<<<
void loop()
 


{
 
  radio.write( &joystick, sizeof(joystick) );
  joystick[0] = digitalRead(startSwitch);      //digital input
  joystick[1] = digitalRead(testSwitch);   //digital input
  joystick[2] = digitalRead(testSwitch1);
  joystick[3] = digitalRead(testSwitch2);
  joystick[5] = digitalRead(testSwitch3);
  joystick[4] = analogRead(JOYSTICK_X);
}


Rx
Code: [Select]
#include <Servo.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#define CE_PIN 9
#define CSN_PIN 10
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
const int servo1 = A0;
int servoVal;
Servo myservo1;
int JOYSTICK_X;
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
int joystick[7];  // 4 element array holding Joystick  4 button state
int led2 = 2; // digital out put
int led3 = 3; // digital out put
int led4 = 4; // digital out put
int led5 = 5; // digital out put
int led6 = 6; // digital out put
int led7 = 7; // digital out put
int led8 = A1; // digital out put
boolean flag4 = true;
boolean flag5 = true;
unsigned long currentMillis;
unsigned long pin4Millis;
unsigned long pin5Millis;
unsigned long debounceMillis = 10UL;     //100ms
unsigned long ledOnTime      = 200UL;  //5 seconds

void setup()   /****** SETUP: RUNS ONCE ******/
{
   myservo1.attach(servo1);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();;
}
void loop ()   /****** LOOP: RUNS CONSTANTLY ******/
{


  if ( radio.available() )
  {
    currentMillis = millis();
    radio.read( &joystick, sizeof(joystick) );
    JOYSTICK_X = map(joystick[4],0,1023,179,0);
    int startSwitch = joystick[0];
    int testSwitch = joystick[1];
    int testSwitch1 = joystick[2];
     int testSwitch2 = joystick[3];
     int testSwitch3 = joystick[5];
    if ((flag4 == true) && (joystick[0] == HIGH))
    {
      flag4 = false;
      digitalWrite(led4, HIGH);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
       digitalWrite(led6, LOW);
      pin4Millis = currentMillis;
    }

    if (( flag4 == false) && (currentMillis - pin4Millis >= ledOnTime))
    {
      digitalWrite(led4, LOW);
      digitalWrite(led2, HIGH);
      digitalWrite(led3, LOW);
      digitalWrite(led6, LOW);
      flag4 = true;
    }
if(flag5 == true && joystick[1]== HIGH )
  {   
    flag5 = false;
    digitalWrite(5,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
     digitalWrite(6, LOW);
    pin5Millis = currentMillis;
  }
     else  if (flag5 == false && currentMillis - pin5Millis >= ledOnTime )
  {
   
    digitalWrite(5,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(6, LOW);
    flag5 = true;
 
  }
   if (joystick[2]==HIGH){
        digitalWrite(led7, HIGH);
      }
      else{
        digitalWrite(led7, LOW);
      }
   if (joystick[3]==HIGH){
        digitalWrite(led6, HIGH);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      }
     if (joystick[5]==HIGH){
        digitalWrite(led8, HIGH);
      }
     
      else{
        digitalWrite(led8, LOW);
      }
      servoVal = JOYSTICK_X;
      myservo1.write(servoVal);
 
  }}

   





Now iam facing problems in power supply. Actualy i have used RX out put signal for activating 5v relays and one servo motor (9gm).When i using power from wall socket and USB port all are working fine.
But whenever i using power from 9v(600mah) battery for RX as well as TX Circuit, its not working. 

sreekanthmp

Now software side problem is solved . All have working as per my programmed conditions.

TX
Code: [Select]
#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#define CE_PIN   9
#define CSN_PIN 10
#define JOYSTICK_X A1
const uint64_t pipe = 0xE8E8F0F0E1LL;

RF24 radio(CE_PIN, CSN_PIN);
int joystick[8];

unsigned long debounceMillis =500UL;     //100ms
unsigned long ledOnTime      = 5000UL;  //5 seconds

const byte startSwitch = 2; //pushed = LOW
const byte testSwitch  = 3; //pushed = LOW
const byte testSwitch1  = 4; //pushed = LOW
const byte testSwitch2  = 5; //pushed = LOW
const byte testSwitch3  = 6; //pushed = LOW
//**********************************************************************

void setup()
{
  Serial.begin(9600);
  
  
  radio.begin();
  radio.stopListening();
  radio.openWritingPipe(pipe);
  pinMode(startSwitch, INPUT); //pushed = LOW
  pinMode(testSwitch,  INPUT); //pushed = LOW
  pinMode(testSwitch1,  INPUT); //pushed = LOW
  pinMode(testSwitch2,  INPUT);
  pinMode(testSwitch3,  INPUT);
  pinMode (JOYSTICK_X,  INPUT);
 

} //  >>>>>>>>>>>>>> E N D  O F  s e t u p ( ) <<<<<<<<<<<<<<<<<
void loop()
  


{
  
  radio.write( &joystick, sizeof(joystick) );
  joystick[0] = digitalRead(startSwitch);      //digital input
  joystick[1] = digitalRead(testSwitch);   //digital input
  joystick[2] = digitalRead(testSwitch1);
  joystick[3] = digitalRead(testSwitch2);
  joystick[5] = digitalRead(testSwitch3);
  joystick[4] = analogRead(JOYSTICK_X);
}


Rx
Code: [Select]
#include <Servo.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#define CE_PIN 9
#define CSN_PIN 10
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
const int servo1 = A0;
int servoVal;
Servo myservo1;
int JOYSTICK_X;
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
int joystick[7];  // 4 element array holding Joystick  4 button state
int led2 = 2; // digital out put
int led3 = 3; // digital out put
int led4 = 4; // digital out put
int led5 = 5; // digital out put
int led6 = 6; // digital out put
int led7 = 7; // digital out put
int led8 = A1; // digital out put
boolean flag4 = true;
boolean flag5 = true;
unsigned long currentMillis;
unsigned long pin4Millis;
unsigned long pin5Millis;
unsigned long debounceMillis = 10UL;     //100ms
unsigned long ledOnTime      = 200UL;  //5 seconds

void setup()   /****** SETUP: RUNS ONCE ******/
{
   myservo1.attach(servo1);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();;
}
void loop ()   /****** LOOP: RUNS CONSTANTLY ******/
{


  if ( radio.available() )
  {
    currentMillis = millis();
    radio.read( &joystick, sizeof(joystick) );
    JOYSTICK_X = map(joystick[4],0,1023,179,0);
    int startSwitch = joystick[0];
    int testSwitch = joystick[1];
    int testSwitch1 = joystick[2];
     int testSwitch2 = joystick[3];
     int testSwitch3 = joystick[5];
    if ((flag4 == true) && (joystick[0] == HIGH))
    {
      flag4 = false;
      digitalWrite(led4, HIGH);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
       digitalWrite(led6, LOW);
      pin4Millis = currentMillis;
    }

    if (( flag4 == false) && (currentMillis - pin4Millis >= ledOnTime))
    {
      digitalWrite(led4, LOW);
      digitalWrite(led2, HIGH);
      digitalWrite(led3, LOW);
      digitalWrite(led6, LOW);
      flag4 = true;
    }
if(flag5 == true && joystick[1]== HIGH )
  {    
    flag5 = false;
    digitalWrite(5,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
     digitalWrite(6, LOW);
    pin5Millis = currentMillis;
  }
     else  if (flag5 == false && currentMillis - pin5Millis >= ledOnTime )
  {
    
    digitalWrite(5,LOW);
    digitalWrite(3,HIGH);
    digitalWrite(2,LOW);
    digitalWrite(6, LOW);
    flag5 = true;
  
  }
   if (joystick[2]==HIGH){
        digitalWrite(led7, HIGH);
      }
      else{
        digitalWrite(led7, LOW);
      }
   if (joystick[3]==HIGH){
        digitalWrite(led6, HIGH);
      digitalWrite(led2, LOW);
      digitalWrite(led3, LOW);
      }
     if (joystick[5]==HIGH){
        digitalWrite(led8, HIGH);
      }
    
      else{
        digitalWrite(led8, LOW);
      }
      servoVal = JOYSTICK_X;
      myservo1.write(servoVal);
  
  }}

    





Now iam facing problems in power supply. Actualy i have used RX out put signal for activating 5v relays and one servo motor (9gm).When i using power from wall socket or USB port all are working fine.
But whenever i using power from 9v(600mah) battery for RX as well as TX Circuit, its not working.  

Robin2

Now iam facing problems in power supply. Actualy i have used RX out put signal for activating 5v relays and one servo motor (9gm).When i using power from wall socket or USB port all are working fine.
But whenever i using power from 9v(600mah) battery for RX as well as TX Circuit, its not working. 
You need to make a clear pencil drawing showing how everything is connected and post a photo of the drawing. See this  Simple Image Guide

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

sreekanthmp

You need to make a clear pencil drawing showing how everything is connected and post a photo of the drawing. See this  Simple Image Guide

...R
image attached.

Robin2

Please make your image visible in your Post. I gave you a link to the instructions.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.


Robin2

Thanks. Your image is just for one Arduino. I have the impression from your Reply #9 that the problem i when you try to power the two Arduinos from the same power supply. Am I wrong?

Exactly what 9v battery are you using? The small PP3 style 9v batteries are useless as they can't provide enough current. Try a pack of 6 x AA alkaline cells or (better) 8 x AA NiMh cells.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up