I have tried to make a program using "struct" for 6 button state and one angle input signal to control using 6 LEDs and one Servo and its working fine(Program mentioned below.) But problem is that when i add two more input signal that time output become erratic and output is not getting as per program.Please help to modify the program. transmitter
#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;
RF24 radio(CE_PIN, CSN_PIN);
int upbut = 2;
int rightbut = 3;
int downbut = 4;
int leftbut = 5;
int nuetral = 6;
int starte = 7;
int stope = 8;
struct data {
uint8_t leds;
uint8_t angle;
};
struct data packet;
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
pinMode(upbut,INPUT_PULLUP);
digitalWrite(upbut,LOW);
pinMode(rightbut,INPUT_PULLUP);
digitalWrite(rightbut,LOW);
pinMode(downbut,INPUT_PULLUP);
digitalWrite(downbut,LOW);
pinMode(leftbut,INPUT_PULLUP);
digitalWrite(leftbut,LOW);
pinMode(nuetral,INPUT_PULLUP);
digitalWrite(nuetral,LOW);
pinMode(starte,INPUT_PULLUP);
digitalWrite(starte,LOW);
pinMode(stope,INPUT_PULLUP);
digitalWrite(stope,LOW);
//end pinMode and digitalWrite
}//--(end setup )---
void loop(){
int potValue = analogRead(A0);
packet.angle = map(potValue, 0, 1023, 0, 180);
packet.leds = 0;
packet.leds = digitalRead(upbut) ? 0x00 : 0x01;
packet.leds |= digitalRead(rightbut) ? 0x00 : 0x02;
packet.leds |= digitalRead(downbut) ? 0x00 : 0x04;
packet.leds |= digitalRead(leftbut) ? 0x00 : 0x08;
packet.leds |= digitalRead(nuetral) ? 0x00 : 0x10;
packet.leds |= digitalRead(starte) ? 0x00 : 0x20;
/*packet.leds |= digitalRead(starte) ? 0x00 : 0x30;*/
radio.write((uint8_t *)&packet, sizeof(struct data));
}
receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <Servo.h>
#define CE_PIN 6
#define CSN_PIN 7
const uint64_t pipe = 0xE8E8F0F0E1LL;
RF24 radio(CE_PIN, CSN_PIN);
const int servo1 = 9;
int servoVal;
Servo myservo1;
struct data {
uint8_t leds;
uint8_t angle;
};
struct data packet;
int led1 = 2; // digital out put
int led2 = 3; // digital out put
int led3 = 4; // digital out put
int led4 = 5; // digital out put
int led5 = 8; // digital out put
int led6 = A0; // digital out put
int led7 = A1; // digital out put
void setup() /****** SETUP: RUNS ONCE ******/
{
Serial.begin(9600);
myservo1.attach(servo1); // attaches the servo1 /analog output
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();;
} //--(end setup )---
void loop(){
if (radio.available()) {
radio.read((uint8_t *)&packet, sizeof(struct data));
myservo1.write(packet.angle);
if (packet.leds ==0x01){
digitalWrite(led1, packet.leds & 0x01);
digitalWrite(led2, packet.leds & 0x00);
digitalWrite(led6, packet.leds & 0x00);
}
else if (packet.leds ==0x02){
digitalWrite(led2, packet.leds & 0x02);
digitalWrite(led1, packet.leds & 0x00);
digitalWrite(led6, packet.leds & 0x00);
}
else if (packet.leds ==0x20){
digitalWrite(led6, packet.leds & 0x20);
digitalWrite(led1, packet.leds & 0x00);
digitalWrite(led2, packet.leds & 0x00);
}
digitalWrite(led3, packet.leds & 0x04);
digitalWrite(led4, packet.leds & 0x08);
digitalWrite(led5, packet.leds & 0x10);
/*digitalWrite(led7, packet.leds & 0x30);*/
}
}