Wireless communication

For now, we are using the nRF24L01 chip for wireless communication. The code below is an example. It can send values both ways between two Arduinos. Swap the address for one of the nodes.

If you press the switch (default connected to pin 6) on one of the Arduinos, it will send a package to the other Arduino.



byte c = 1;
int payload = 2;
  
byte message[2];

// Pins for user input and output
int pinTransmit = 10;
int pinReceive = 5;
int pinSwitch = 6;

void setup(){
  Serial.begin(9600);
  
  pinMode(pinTransmit, OUTPUT);
  pinMode(pinReceive, OUTPUT);
  pinMode(pinSwitch, INPUT);
  digitalWrite(pinTransmit, LOW);
  digitalWrite(pinReceive, LOW);
  
  // Swap those adresses for the second node (duh)
  CR_setup( "coord", payload );
  CR_set_destination( "clie1" );
  
  Serial.println("Started!"); 
}

void loop(){
  if( digitalRead( pinSwitch ) == LOW && !CR_busy() ) {
    digitalWrite(pinTransmit, HIGH);
    
    // Send a static number (node ID) and the increasing value c
    message[0] = 21;
    message[1] = c;
    
    CR_send( message );
    
    c++;
    if( c > 255 ) c = 1;
    
    delay( 20 );
    digitalWrite(pinTransmit, LOW);
    
    while( digitalRead( pinSwitch ) == LOW ) {
      delay( 50 );
    }
  }
  
  if( CR_dataReady() ){
    digitalWrite(pinReceive, HIGH);
    
    byte data[payload];
    CR_getData( data );
    
    Serial.println( (int) data[0] );
    Serial.println( (int) data[1] );
    
    delay(20);
    digitalWrite(pinReceive, LOW);
  }
}

Series of function to control the radio. This makes it easier to switch to another communication module, e.g. XBee.

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>


void CR_setup( char addres[], int payload ) {
  Mirf.cePin = 8;
  Mirf.csnPin = 7;
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  
  Mirf.setRADDR( ( byte * ) addres );
  Mirf.payload = sizeof( byte[payload] );
  
  Mirf.config();
}

void CR_set_destination( char address[] ) {
  Mirf.setTADDR( ( byte * ) address );
}

void CR_send( byte message[] ) {
  Mirf.send( message );
}

boolean CR_busy() {
  return Mirf.isSending();
}

boolean CR_dataReady() {
  return ( !Mirf.isSending() && Mirf.dataReady() );
}

void CR_getData( byte* data ) {
  Mirf.getData( data );
}

 Because we don't have more than two nRF24L01  transmitters/receivers we are now using xbee's for the prototype with more than 1 keyboard. The code and a picture of the arduino setup is attached below: 


ċ
TransmitterReceiverXbee.rar
(4k)
Herjan Treurniet,
Jun 21, 2011, 6:15 AM
ą
node.PNG
(1362k)
Pieter Pelt,
Jun 21, 2011, 5:56 AM
Comments