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
#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
#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 );
}
}}