sending counting from an ISR

195 views
Skip to first unread message

handy mosey

unread,
May 21, 2014, 7:01:50 AM5/21/14
to nrf24-...@googlegroups.com
Hi..
I want to send my pulse counting form a magnetic pickup coil sensor.
Here's my code:

In nrf24_server:

// nrf24_server.pde

#include <SPI.h>
#include <RH_NRF24.h>

// Singleton instance of the radio driver
//RH_NRF24 nrf24;
 RH_NRF24 nrf24(10, 7); // use this to be electrically compatible with Mirf
// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
// RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini
volatile unsigned long count;
void setup()
{
  Serial.begin(9600);
 
  noInterrupts();
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1  = 0;
  OCR1A = 4;
  bitSet(TCCR1B,WGM12);
  bitSet(TIMSK1,OCIE1A);
  bitSet(TCCR1B,CS12);
  bitSet(TCCR1B,CS11);
  interrupts();
 
  while (!Serial)
    ; // wait for serial port to connect. Needed for Leonardo only
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");   
}
ISR(TIMER1_COMPA_vect)    
{
  count++;
}
void loop()
{
  if (nrf24.available())
  {
    // Should be a message for us now  
    uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
    uint8_t len = sizeof(buf);
    if (nrf24.recv(buf, &len))
    {
//      NRF24::printBuffer("request: ", buf, len);
      Serial.print("got request: ");
      Serial.println((char*)buf);
     
      // Send a reply
      int data = count;
      nrf24.send((uint8_t*)&data, sizeof(data));
      nrf24.waitPacketSent();
      Serial.println("Sent a reply");
    }
    else
    {
      Serial.println("recv failed");
    }
  }
}

in nrf24_client:
// nrf24_client.pde

#include <SPI.h>
#include <RH_NRF24.h>

// Singleton instance of the radio driver
//RH_NRF24 nrf24;
 RH_NRF24 nrf24(10, 7); // use this to be electrically compatible with Mirf
// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
// RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini

void setup()
{
  Serial.begin(9600);
  while (!Serial)
    ; // wait for serial port to connect. Needed for Leonardo only
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(1))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");   
}


void loop()
{
  Serial.println("Sending to nrf24_server");
  // Send a message to nrf24_server
  uint8_t data[] = "Hello World!";
  nrf24.send(data, sizeof(data));
 
  nrf24.waitPacketSent();
  // Now wait for a reply
  uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
  uint8_t len = sizeof(buf);

  if (nrf24.waitAvailableTimeout(500))
  {
    // Should be a reply message for us now  
    if (nrf24.recv(buf, &len))
    {
      Serial.print("pulsa air: ");
      unsigned long pulsa = *((unsigned long*)buf);
      Serial.println(pulsa);
    }
    else
    {
      Serial.println("recv failed");
    }
  }
  else
  {
    Serial.println("No reply, is nrf24_server running?");
  }
  delay(400);
}

when I compile the server there is an error compiling:

Arduino: nightly (Windows 8), Board: "Arduino Pro or Pro Mini, ATmega328 (5V, 16 MHz)"

RadioHead\RH_ASK.cpp.o: In function `__vector_11':
C:\Program Files (x86)\arduino-nightly\libraries\RadioHead/RH_ASK.cpp:492: multiple definition of `__vector_11'
nrf24_server.cpp.o:C:\Program Files (x86)\arduino-nightly/nrf24_server.ino:37: first defined here

in which line should I change. Thank You

Regards,
Handy M



Mike McCauley

unread,
May 22, 2014, 2:28:13 AM5/22/14
to nrf24-...@googlegroups.com
Hello,

RH_ASK already uses Timer1.
You will have to use a different timer in your code.

Cheers.
--
Mike McCauley VK4AMM mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com
Phone +61 7 5598-7474 Fax +61 7 5598-7070

handy mosey

unread,
May 22, 2014, 6:49:59 AM5/22/14
to nrf24-...@googlegroups.com
Thank you for reply.
I should change to Timer0 or Timer2.
 Regards,
Handy M



--
You received this message because you are subscribed to the Google Groups "NRF24-Arduino" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nrf24-arduin...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages