controlling LEDS via RX/TX where the switches are on analogues

Gday,

For my school engineering project I am making an Arduino telecommunications system.
I have coded the switches on the TX circuit board and it sends out data as I saw in the serial monitor.
however for my RX circuit I need the switches to control some LEDs, the switches are setup on the analogue 10 bit scale, and when the switches are low/off they are 0 and when high/on they are 1023.
I want the switches to control the leds with this sytem. please help I am begging…
code is below

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

/-----( Import needed libraries )-----/
#include <SPI.h> // Comes with Arduino IDE
#include “RF24.h” // Download and Install (See above)
/-----( Declare Constants and Pin Numbers )-----/

/-----( Declare objects )-----/
// (Create an instance of a radio, specifying the CE and CS pins. )
RF24 radio (7, 8); // “myRadio” is the identifier you will use in following methods
int led1 = 2;
int led2 = 4;
int led3 = 5;
/-----( Declare Variables )-----/
byte addresses[6] = {“1Node”}; // Create address for 1 pipe.

struct dataStruct
{
int switch1 = A1;
int switch2 = A2;
int switch3 = A3;
} dataReceived;
void setup()
{

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);

;

// Use the serial Monitor (Symbol on far right). Set speed to 115200 (Bottom Right)
Serial.begin(115200);
delay(1000);

radio.begin(); // Start up the physical nRF24L01 Radio
radio.setChannel(100); // Above most Wifi Channels
// Set the PA Level low to prevent power supply related issues since this is a
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
radio.setPALevel(RF24_PA_MAX);
// myRadio.setPALevel(RF24_PA_MAX); // Uncomment for more power

radio.openReadingPipe(1, addresses[0]); // Use the first entry in array ‘addresses’ (Only 1 right now)
radio.startListening();

}//–(end setup )—

void loop() /****** LOOP: RUNS CONSTANTLY ******/
{

if ( radio.available()) // Check for incoming data from transmitter
{
while (radio.available()) // While there is data ready
{
radio.read( &dataReceived, sizeof(dataReceived) ); // Get the data payload (You must have defined that already!)

map(dataReceived.switch1, 0, 1023, HIGH, LOW); // this “maps” the very large “val” to a smaller range between 1 - 180.
if (switch1 == HIGH) {
// turn LED on:
digitalWrite(led1, HIGH);
}
else {
// turn LED off:
digitalWrite(led1, LOW);
map(dataReceived.switch2, 0, 1023, HIGH, LOW); // this “maps” the very large “val” to a smaller range between 1 - 180.
if (switch2 == HIGH) {
// turn LED on:
digitalWrite(led2, HIGH);
}
else {
// turn LED off:
digitalWrite(led2, LOW);
map(dataReceived.switch3, 0, 1023, HIGH, LOW); // this “maps” the very large “val” to a smaller range between 1 - 180.
if (switch3 == HIGH) {
// turn LED on:
digitalWrite(led3, HIGH);
}
else {
// turn LED off:
digitalWrite(led3, LOW);
}
{
// Print the data to Serial
Serial.print("switch1: ");
Serial.println(dataReceived.switch1);
Serial.print("switch2: ");
Serial.println(dataReceived.switch2);
Serial.print("Switch3: ");
Serial.print(dataReceived.switch3);
}

We need somewhere to start from so you need to describe in detail what your code does now not just what you want it to do.

E.g. do your Serial.print()s show the expected values for the switches? And why are you using analog values for switches...that's just silly.

BTW stupid comments like the one below don't help with understanding

 map(dataReceived.switch2, 0, 1023, HIGH, LOW);  // this "maps" the very large "val" to a smaller range between 1 - 180.

Pasting code from somewhere is fine but when you change the code change the comment too. Also you said earlier that 0 = LOW and 1023 = HIGH. Make your mind up.

Steve

Pretend we haven't got the same inside knowledge of this project that you have. Remarks like this:

I have coded the switches on the TX circuit board and it sends out data as I saw in the serial monitor.

.... really don't mean a lot without some context. It would help the forum help you if you could expand on this:

I am making an Arduino telecommunications system.....

.... to say what you're hoping to achieve:

I am making an Arduino telecommunications system that will do "this", "that" and "the next thing"

Please post the code for both the TX and the RX portion of the project.

Why are you using analog values for the switches? If the only reason is that the switches are connected to analog inputs on the arduino, you can use digitalRead instructions on those same inputs, or at least convert the analog values to digital in the TX.

Sending the switch values as integers is very inefficient, that uses six bytes, you can send the state of three switches in a single byte.

My knowledge of cding and IDE is very small if not non existent, i understand the concepts sometimes but just dont know what to write.
the point of my code i am going to try explain here
TX—
SO on the Transmitter, there are three switches they are controlled by the code below which basically when i have the switch on the signal sends out 0 and when it is off sends out 1023, i saw this in the serial monitor and i assume that is what is being sent out.
RX—
I need the rx code to receive the signals coming in from the tx, i believe i set it up correctly on the same channel and same libaries as the tx. I have 3 LEDS as you can see they are listed below as led1, led2 and led3. i want the led to turn on if the corresponding switch is turned on, this is the part where i need help as i am not sure of the code below is correct.
to help you get your head around the sytem-if switch 1 is in the off position the 10 bit scale is at 1023 being LOW and then when the switch is turned on the bit scale is at o being HIGH, the led will then turn on due to this.

the sytem i am making is much alrger than this however i need help with the switch part.
the sytem is a pit control to driver communication sytem, in the driver car i have leds which will light up on the dashboard and this will be able to notify the driver of whatever the predetermined meaning of each coloured led is. the car is also fitted with proximity senors to notify the driver of approaching vehicles from behind in their blind spot.
The pitcontrol has an lcd display and has the cars laptime displayed on it aswell as the switches which communicate with the driver.

TX CODE
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

// Include the SPI and RF libraries
#include <SPI.h>
#include “RF24.h”

/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 /
RF24 radio(7,8);
/
*********************************************************/

byte addresses[6] = {“1Node”}; // Create address for 1 pipe.

// Assign Push Button pin
int switch1 = A1;
int switch2 = A2;
int switch3 = A3;

struct dataStruct {

int switch1;
int switch2;
int switch3;

} DATA;

void setup() {

// Start the Serial connection for Debugging
Serial.begin(115200);
Serial.println(“Starting”);

//Start the Radio
radio.begin();

// Set the PA Level low to prevent power supply related issues since this is a
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
radio.setPALevel(RF24_PA_MAX);

// Set Wifi Channel - Above most Wifi Channels
radio.setChannel(100);

//Set Address to send to
radio.openWritingPipe( addresses[0]);

// Start the radio listening for data
radio.startListening();
}

void loop() {

// Stop the radio listening for data
radio.stopListening();

// Read data from pins and save to Struct
DATA.switch1 = analogRead(switch1);
DATA.switch2 = analogRead(switch2);
DATA.switch3 = analogRead(switch3);

// Transmit DATA Struct over wireless
radio.write(&DATA, sizeof(DATA));

// Write DATA to serial for debugging
Serial.print("switch1: ");
Serial.println(DATA.switch1);
Serial.print("switch2: ");
Serial.println(DATA.switch2);
Serial.print("switch3: ");
Serial.println(DATA.switch3);

// delay(100);

}

CODE FOR RX
/-----( Import needed libraries )-----/
#include <SPI.h> // Comes with Arduino IDE
#include “RF24.h” // Download and Install (See above)
/-----( Declare Constants and Pin Numbers )-----/

/-----( Declare objects )-----/
// (Create an instance of a radio, specifying the CE and CS pins. )
RF24 radio (7, 8); // “myRadio” is the identifier you will use in following methods
int led1 = 2;
int led2 = 4;
int led3 = 5;
/-----( Declare Variables )-----/
byte addresses[6] = {“1Node”};

struct dataStruct
{
int switch1 = A1;
int switch2 = A2;
int switch3 = A3;
} dataReceived;
void setup()
{

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);

;

// Use the serial Monitor (Symbol on far right). Set speed to 115200 (Bottom Right)
Serial.begin(115200);
delay(1000);

radio.begin(); // Start up the physical nRF24L01 Radio
radio.setChannel(100); // Above most Wifi Channels
// Set the PA Level low to prevent power supply related issues since this is a
// getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
radio.setPALevel(RF24_PA_MAX);
// myRadio.setPALevel(RF24_PA_MAX); // Uncomment for more power

radio.openReadingPipe(1, addresses[0]); // Use the first entry in array ‘addresses’ (Only 1 right now)
radio.startListening();

}//–(end setup )—

void loop() /****** LOOP: RUNS CONSTANTLY ******/
{

if ( radio.available()) // Check for incoming data from transmitter
{
while (radio.available()) // While there is data ready
{
radio.read( &dataReceived, sizeof(dataReceived) ); // Get the data payload (You must have defined that already!)

map(dataReceived.switch1, 0, 1023, HIGH, LOW); // this hopefully will set the bit scale to a defined name, basically 1023=low and 0=high
if (switch1 == HIGH) {
// turn LED on:
digitalWrite(led1, HIGH);
}
else {
// turn LED off:
digitalWrite(led1, LOW);
map(dataReceived.switch2, 0, 1023, HIGH, LOW);
if (switch2 == HIGH) {
// turn LED on:
digitalWrite(led2, HIGH);
}
else {
// turn LED off:
digitalWrite(led2, LOW);
map(dataReceived.switch3, 0, 1023, HIGH, LOW);
if (switch3 == HIGH) {
// turn LED on:
digitalWrite(led3, HIGH);
}
else {
// turn LED off:
digitalWrite(led3, LOW);

// Print the data to Serial
Serial.print("switch1: ");
Serial.println(dataReceived.switch1);
Serial.print("switch2: ");
Serial.println(dataReceived.switch2);
Serial.print("Switch3: ");
Serial.print(dataReceived.switch3);

}}}

david_2018:
Please post the code for both the TX and the RX portion of the project.

Why are you using analog values for the switches? If the only reason is that the switches are connected to analog inputs on the arduino, you can use digitalRead instructions on those same inputs, or at least convert the analog values to digital in the TX.

Sending the switch values as integers is very inefficient, that uses six bytes, you can send the state of three switches in a single byte.

I am using analogue as I ran out of digitals ports and would rather not get a mega

murphmd:
I am using analogue as I ran out of digitals ports pins and would rather not get a mega

But as david_2018 said, you can use them as digital pins anyway; they are digital pins.

    map(dataReceived.switch1, 0, 1023, HIGH, LOW);  // this hopefully will set the bit scale to a defined name, basically 1023=low and 0=high

This line does nothing. The map() function returns a value, but you are not assigning the value to a variable.

if (switch1 == HIGH) {

switch1 was not declared, you cannot refer to dataReceived.switch1 as switch1, it is not the same thing.

byte switch1 = map(dataReceived.switch1, 0, 1023, HIGH, LOW);  // this hopefully will set the bit scale to a defined name, basically 1023=low and 0=high
if (switch1 == HIGH) {

Should accomplish what you are trying to do, but you can accomplish the same thing with:

if (dataReceived.switch1 > 512) {

OK, first things first.

You need to go and read the forum instructions so that you can go back and modify your original posts (not re-post them) - using the “More → Modify” option below the right hand corner of your post - to mark up your code as such using the “</>” icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE has a “copy for forum” link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don’t forget to use the “Auto-Format” (Ctrl-T) option first to make it easy to read. If you do not post it as “code” it can as you now see, be quite garbled and is always more difficult to read.

It is inappropriate to attach it as a “.ino” file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only between complete functional blocks.