Stuck in an if statement

Started by Ablivadice, June 19, 2017, 07:05:21 PM

Ablivadice

I'm trying to make an RFM69HCW send a message to another RFM69HCW when a button is pressed. When I press the button on my Arduino uno, it turns on an LED and then sends a message to the level shifter then to my RFM69HCW. Then it is suppose to turn off the LEDs when done but it is stuck on the send.
Any help would be greatly appreciated.
Here is my code:

// Include the RFM69 and SPI libraries:

#include <RFM69.h>
#include <SPI.h>

// Addresses for this node. CHANGE THESE FOR EACH NODE!

#define NETWORKID     1   // Must be the same for all nodes
#define MYNODEID      1   // My node ID
#define TONODEID      2   // Destination node ID

// RFM69 frequency, uncomment the frequency of your module:

#define FREQUENCY   RF69_433MHZ
//#define FREQUENCY     RF69_915MHZ

// AES encryption (or not):

#define ENCRYPT       true // Set to "true" to use encryption
#define ENCRYPTKEY    "0000000000000001" // Use the same 16-byte key on all nodes

// Use ACKnowledge when sending messages (or not):

#define USEACK        false // Request ACKs or not

// Packet sent/received indicator LED (optional):

int button = 7; // pin of button
int LED1 = 9; // LED positive pin
int LED2 = 8; // LED positive pin

int buttonV;

// Set up a "buffer" for characters that we'll send:

  static char sendbuffer[1];
  static int sendlength = 1;

// Create a library object for our RFM69HCW module:

RFM69 radio;

void setup()
{
  // Set up the button pin
  
  pinMode(button, INPUT);
  
  // Set up the indicator LED (optional):

  pinMode(LED1,OUTPUT);
  digitalWrite(LED1,LOW);
  pinMode(LED2,OUTPUT);
  digitalWrite(LED2,LOW);

  // Initialize the RFM69HCW:

  radio.initialize(FREQUENCY, MYNODEID, NETWORKID);
  radio.setHighPower(); // Always use this for RFM69HCW

  // Turn on encryption if desired:

  if (ENCRYPT)
    radio.encrypt(ENCRYPTKEY);
}

void loop()
{
  buttonV = digitalRead(button);
    if (buttonV == 1)
    {
       digitalWrite(LED1, HIGH);                      //Turn on LED1 while button press
       digitalWrite(LED2, HIGH);                      //Turn on LED2 while button press
       radio.send(TONODEID, sendbuffer, sendlength);  //Send char when button press
    }
    if (buttonV < 1)
    {
      digitalWrite(LED1, LOW);     //Turn off LED1 when button is not pressed
      digitalWrite(LED2, LOW);     //Turn off LED2 when button is not pressed
    }
}

TomWS

Quote from: Ablivadice on June 19, 2017, 07:05:21 PM
I'm trying to make an RFM69HCW send a message to another RFM69HCW when a button is pressed. When I press the button on my Arduino uno, it turns on an LED and then sends a message to the level shifter then to my RFM69HCW. Then it is suppose to turn off the LEDs when done but it is stuck on the send.
Any help would be greatly appreciated.
Firstly, your code will send continuously as long as the button is pressed.  Probably not a good thing since your loop time is probably less than the time to send and you're overrunning the transmit.
To fix, you need to send only when the button makes a change to go high as in:
void loop(void) {
  static int lastButton = 0;
  buttonV = digitalRead(button);
  if (buttonV != lastButton) {
     lastButton = buttonV;
     if (buttonV) {
         ... send and light led here (not sure why you have two LEDs, but that's not your issue)
      } else {
        ... turn off LEDs
      }
  }
}


Now the other issue you'll have is if the button has any 'bounce' you'll need to debounce it.  But you can DAGS that...

Tom

syrinxtech

#2
Ablivadice,

I'm assuming you don't have a flash chip....since you're using D8 for the second LED and that is normally used by the flash chip?  And, you're not including the SPIFlash.h header file.

Also, you didn't seem to put any particular payload into the 1-byte buffer.  Was there any particular message you wanted to send?

Third, and I could be wrong on this one....generally it's a good idea to call setHighPower() without a "true" or "false" parameter.  In looking at the code it takes this parameter and acts accordingly.  Not sure what it does if it isn't specified in the function call.

Ablivadice

Thank you all for the responses. I tried the change to the code and it did not fix the problem. I tried a different library, RadioHead and my code is now working.