Go Down

Topic: NRF24l01 LED Remote Modification not working (Read 743 times) previous topic - next topic

cattledog

Quote
I did the = sign instead and it worked, sort of. Now the transmitter only sends 200, even if I switch the switch on! I have no clue what's wrong!
What do your Serial prints statements say about the state of the switches? Is your logic correct for the INPUT_PULLUP on the switches, and what position is HIGH or LOW?

You may want to write a simple and separate sketch to test and confirm the switches and the logic.

Watson221

The serial monitor says that they're 0.

Low = Open

High = Closed

I believe my logic is correct.

I wrote some new code to combat the errors i was getting using the old code

With this code I get 300 no matter the position of the switches

Code: [Select]

#include <SPI.h>
#include "RF24.h"

boolean powerState  = LOW;

int photocell = 0;

int upDetect = 8;
int downDetect = 9;

int manualUp = 10;
int manualDown = 3;

int upswitchReading;
int downswitchReading;
int photocellReading;
int manualUpReading;
int manualDownReading;
int DATA;

RF24 radio(2, 4);
byte addresses[][6] = {"1Node", "2Node"};

void setup(void) {
  pinMode (upDetect, INPUT_PULLUP);
  pinMode (downDetect, INPUT_PULLUP);

  pinMode (manualDown, INPUT_PULLUP);
  pinMode (manualUp, INPUT_PULLUP);
  pinMode (6, OUTPUT);
  pinMode (5, OUTPUT);
  digitalWrite (5, HIGH);

  radio.begin();
  radio.setPALevel(RF24_PA_LOW);
  radio.openWritingPipe(addresses[1]);

  Serial.begin(9600);
  delay(100);
}

void loop(void) {
  Serial.println("Up Switch Reading = ");
  upswitchReading = digitalRead(upDetect);
  Serial.println(upswitchReading, DEC);
  delay(1000);

  Serial.println("Down Switch Reading = ");
  downswitchReading = digitalRead(downDetect);
  Serial.println(downswitchReading, DEC);
  delay(1000);

  Serial.println("Manual Down Switch = ");
  manualDownReading = digitalRead(manualDown);
  Serial.println(manualDownReading, DEC);
  delay(1000);

  Serial.println("Manual UP Switch = ");
  manualUpReading = digitalRead(manualUp);
  Serial.println(manualUpReading, DEC);
  delay(1000);

  Serial.println("  Photocell reading = ");
  photocellReading = analogRead(photocell);
  Serial.println(photocellReading);
  delay(1000);


  if (photocellReading < 400) {
    powerState = HIGH;
  }
  else if (photocellReading > 400) {
    powerState = LOW;
  }

  if (manualDownReading == 0) {
    powerState = HIGH;
  }

  else if (manualUpReading == 0) {
    powerState = LOW;
  }

  digitalWrite(6, powerState);

  if (downswitchReading = 1) {
    DATA = 100;
  }

  else if (downswitchReading = 0 ){
    DATA = 200;
  }

  if (upswitchReading = 1) {
    DATA = 300;
  }
  else if (upswitchReading == 0) {
    DATA = 200;
  }

  Serial.println("DATA = ");
  Serial.println(DATA);

  radio.write( &DATA, sizeof(DATA) );


}






cattledog

Code: [Select]
if (downswitchReading = 1) {
    DATA = 100;
  }

  else if (downswitchReading = 0 ){
    DATA = 200;
  }

  if (upswitchReading = 1) {
    DATA = 300;
  }
  else if (upswitchReading == 0) {
    DATA = 200;
  }

  Serial.println("DATA = ");
  Serial.println(DATA);

  radio.write( &DATA, sizeof(DATA) );

}


== inside the if() conditionals

Watson221

That half way fixed the issue the Up switch portion works, but the down switch portion doesn't.

Watson221

https://www.youtube.com/watch?v=iJJVj9kgS-A

I followed this guy's tutorial and I managed to get it up and running!!!
I've been working on this for around a week straight!!
I ended up using a different library, that appears to be a little simpler and easier to understand.
Thank you guys so much for all your help!
I don't fully understand why the old code didn't work, but I'll have to leave that for another day, since im on a deadline.

Library-
https://github.com/maniacbug/RF24

Transmitter Code-
Code: [Select]

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

#include <SPI.h>

int msg[1];


RF24 radio(2, 4);



const uint64_t pipes[2] = {

  0xF0F0F0F000LL, 0xF0F0F0F0FFLL
};


int buttonPin1 = 8;

int buttonPin2 = 9;

void setup(void) {

  radio.begin();

  radio.setDataRate(RF24_250KBPS);

  radio.setChannel(100);

  radio.setRetries(15, 15);

  radio.openWritingPipe(pipes[1]);

  radio.openReadingPipe(1, pipes[0]);
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();
  Serial.begin(9600);
}

void loop(void) {



  if (digitalRead(buttonPin1) == HIGH) {

    msg[0] = 111;

    radio.stopListening();

    radio.write(msg, 1);

    radio.startListening();

  }

  if (digitalRead(buttonPin2) == HIGH) {

    msg[0] = 112;

    radio.stopListening();

    radio.write(msg, 1);

    radio.startListening();

  }

}


Receiver Code-

Code: [Select]
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>

int msg[1];

RF24 radio(2, 4);

int LEDpin1 = 10;

int LEDpin2 = 6;


const uint64_t pipes[2] = {

  0xF0F0F0F000LL, 0xF0F0F0F0FFLL
};

void setup(void) {

  radio.begin();

  radio.setDataRate(RF24_250KBPS);

  radio.setChannel(100);

  radio.setRetries(15, 15);
  radio.openWritingPipe(pipes[0]);
  radio.openReadingPipe(1, pipes[1]);
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();

  pinMode(LEDpin1, OUTPUT);

  pinMode(LEDpin2, OUTPUT);
  Serial.begin(9600);
}

void loop(void) {

  if (radio.available()) {

    bool done = false;

    while (!done) {

      done = radio.read(msg, 1);


      if (msg[0] == 111) {

        delay(10);

        digitalWrite(LEDpin1, HIGH);

      }

      else {

        digitalWrite(LEDpin1, LOW);

      }

      delay(10);

      if (msg[0] == 112) {

        delay(10);

        digitalWrite(LEDpin2, HIGH);

      }

      else {

        digitalWrite(LEDpin2, LOW);

      }

      delay(10);

    }

  }

}

cattledog

Due to the sequence of your if() conditionals, The DATA values from the upswitchReadings are overwriting those from the down switches,

You have 4 possible switch conditions
0/0
0/1
1/0
1/1

You can use compound if statements to set the DATA value for each of the 4 possible combinations

For example

Code: [Select]
if(upswitchReading ==1 && downswitchReading ==0)
{
 //set some value for DATA
}

Watson221

Ooooohh ok that makes so much sense! I tried it out and it actually worked, only after i moved the antennas closer to each other though.

Go Up