Interrupt/Radio Code Help Please

Using : Board -Arduino Pro or Pro Mimi & Processor - ATmega328P (3.3V, 8 MHz)

Pin 3 has a grounded magnetic switch connected to it that is normally open with no magnetic field applied.

Pin 9 has an indicator LED connected.

When I add the radio code and upload it, it seems to only transmit once and that's it.

Please try to see the error(s).

first I tested my code interrupt with this code and it performs as it should.

const byte interruptPin = 3;

byte ledPin = 9;

void setup ()
{
  pinMode (interruptPin, INPUT_PULLUP);
  attachInterrupt (digitalPinToInterrupt(interruptPin), processChange, CHANGE );
  
  pinMode (ledPin, OUTPUT);
  digitalWrite (ledPin, LOW);

  if (digitalRead(interruptPin) == HIGH)
  {
    digitalWrite(ledPin,HIGH);
  }
  
}  //End setup()


void loop ()
{
  // do nothing
}


void processChange ()
{
  delay (20);
  if (digitalRead(interruptPin) == LOW)
  {
    digitalWrite(ledPin,LOW);
    }
    else
    {
      digitalWrite(ledPin,HIGH);
    }
}

Then I added the radio code as follows:


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

// Addresses for this node. CHANGE THESE FOR EACH NODE!
#define NETWORKID     144   // Must be the same for all nodes
#define MYNODEID      4   // My node ID
#define TONODEID      1   // 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 ENCRYPT       false // Set to "true" to use encryption
#define ENCRYPTKEY    "TOPSECRETPASSWRD" // Use the same 16-byte key on all nodes

// Use ACKnowledge when sending messages (or not):
#define USEACK        true // Request ACKs or not

//#define SERIAL        true
//#define RESET         true

// Create a library object for our RFM69HCW module:
RFM69 radio;

// -----------------------------------------------------------------------------
const byte interruptPin = 3;

char switchStateOPEN[] = "144-4-SO";
char switchStateCLOSED[] = "144-4-SC";

byte ledPin = 9;


void setup ()
{
  pinMode (interruptPin, INPUT_PULLUP);
  attachInterrupt (digitalPinToInterrupt(interruptPin), processChange, CHANGE );
  
  pinMode (ledPin, OUTPUT);
  digitalWrite (ledPin, LOW);
 if (digitalRead(interruptPin) == HIGH)
  {
    digitalWrite(ledPin,HIGH);
  }

  // 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);
  
}  //End setup()


void loop ()
{
    
  radio.sleep();  
  //LowPower.powerDown(SLEEP_2S, ADC_OFF, BOD_OFF);
  LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
  
}

// ----------------------------------
// Helper Methods
// ---------------------------------

void processChange ()
{
  delay (50);
  if (digitalRead(interruptPin) == LOW)
  {
  digitalWrite(ledPin,LOW);
  sendMessage (false);
  radio.sleep();
  }
  else
  {
  digitalWrite(ledPin,HIGH);
  sendMessage (true);
  radio.sleep();
  }
} // End Blink()

void sendMessage(bool switchState)
{ 
  // Send the packet!     
  if (switchState == true)
  {
    // switchStateOPEN
    if (radio.sendWithRetry(TONODEID, switchStateOPEN, strlen(switchStateOPEN)+1))
    {
      // ACK received!       
    }
    else
    {
      // No ACK received 
    }    
  }
  else
  {
    // switchStateCLOSED
    if (radio.sendWithRetry(TONODEID, switchStateCLOSED, strlen(switchStateCLOSED)+1))
    {
      // ACK received!            
    }
    else
    {
      // No ACK received       
    }
  }     
      
}  // End void sendMessage ()

You know when it says don't do delays or other interrupt-dependent stuff in interrupt context?

It really means just that

Could you please suggest code changes?

Can you explain why you're using interrupts?

It is intended to be battery powered and I did not want a change missed.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.