Norduino Sparkfun Keyfob tutorial 1

This is my very first tutorial about using the Norduino.

What I want to show is a simple application which will read the status buttons of the fob and activate 2 LEDS: when I press left the green one will be activated and when I press right the yellow one will be activated. It’s so simple but it’s just for starters.

This is what you need:

First of all you need to assemble the Norduino as discussed on this page.
Secondly you have to program it but put extra care you need to setup the BUB Board at 3.3V otherwise you are going to destroy the poor Norduino!

Finally, you need to know that the Norduino pin layout is identical to the Jeenode and yes if you are asking why I forgot the silkscreen on the PCB, I have to say that the Chinese factory who printed them discared my layer, it’s something that I’m going to fix in the near future!
DSCF5011
So we are going to connect the green LED on PORT 4 between DIO and GND and the YELLOW LED on PORT 1 between DIO and GND

As you can see the BUB board sits on top of the serial header connection and the 2 leds are on the bottom.
Here is the magic code which will sync your led’s with the buttons.
/**
 * Emulates the nordic interface serial board from sparkfun http://www.sparkfun.com/products/9019
 * Use the nordic fob to send key strokes!
 * 2011-03-10 www.norduino.com http://opensource.org/licenses/mit-license.php
 * Uses the Mirf library
 */

//This inlcudes are for the Nordic interface
#include <Spi.h>
#include <mirf.h>
#include <nRF24L01.h>
//This includes are for the Norduino ports
// uses the same as the jeenode
#include <Ports.h>

Port one (1);
Port four (4);

//set the address as equal to the keyfob
// it's 5 bytes
byte tx_addr[5] = {0xE7, 0xE7, 0xE7, 0xE7, 0xE7};
byte rx_addr[5] = {0xE7, 0xE7, 0xE7, 0xE7, 0xE7};

//button status
byte buttons[4]={0,0,0,0};

void setup(){

  //set the ports first
  one.mode(OUTPUT);
  four.mode(OUTPUT);

  // setup the serial port for debugging
  Serial.begin(9600);

  /*
   * Setup pins / SPI.
   */

  Mirf.init();

  /*
   * Configure receiving address.
   */

  Mirf.setRADDR(rx_addr);

  //Air data rate 1Mbit, 0dBm, Setup LNA
  Mirf.configRegister(RF_SETUP, 0x07);
  //Disable auto-acknowledge
  Mirf.configRegister(EN_AA, 0x00);
  //The button presses are stored in a 4 byte payload
  Mirf.payload = 4;
  //The Nordic Keyfobs are setup on channel 2
  Mirf.channel=2;

  /*
   * Write channel and payload config then power up reciver.
   */

  Mirf.config();

  Serial.println("Listening...");
}

void loop(){
  /*
   * A buffer to store the data.
   */

  byte data[Mirf.payload];

  /*
   * If a packet has been recived.
   */
  if(Mirf.dataReady()){

    do{
      Serial.println("Got packet");

      /*
       * Get the paylod into the buffer.
       */

      Mirf.getData(data);

      /*
      * Decode the button press with a switch case
      */
      switch (data[0]) {
  	case 0x1D: Serial.println("DOWN"); break;
  	case 0x1E: Serial.println("UP"); break;
  	case 0x17:
          Serial.println("RIGHT");
          //swap the button status
          buttons[0]=buttons[0] ^ 1;
          one.digiWrite(buttons[0]);
          break;
  	case 0x1B:
          Serial.println("LEFT");
          //swap the button status
          buttons[1]=buttons[1] ^ 1;
	  four.digiWrite(buttons[1]);
          break;
  	case 0x0F: Serial.println("CENTER"); break;
  	default: break;
      }

    }while(!Mirf.rxFifoEmpty());
  }
}

The code reads quite well, the only difficult part was to set the configuration of the Nordic module to match the one used in the Sparkfun buttons.
As you can see the Jeenode original Port library is supported so you can re-use what was already written by JCW except for few classes that has to be made compatible with the Nordic interrupt system.

 

Posted in Norduino Hardware, Norduino Sw

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by sweet Captcha