Go Down

Topic: Arduino controlling nRF24L01P and SD memory (Read 2373 times) previous topic - next topic

EnricF

Hi all,

I am triying to send a picture (jpeg) from a SD memory by nRF24L01+ transceiver (from Nordic) connected through Arduino Pro Mini with Atmega328, 3.3V, 8 MHz. On the other side, the same transceiver will receive the image an store it into an SD memory. The final aim is to send photos and store it remotely. But now I am working in a simply approach (no camera). My problem is that I am not able to made them work together (SD an transcevier) through SPI connection.

I hope someone can help me. This is a part of the code I am using and continuously improving but I never make it work (with references to the code's owners). All the code is attach to this post (only for the transmitting part, because once I can transmit, the reception will "flow" alone):

Code: [Select]
/*********************************************************************
**  Device:  nRF24L01+                                              **
**  File:   EF_nRF24L01_TX.c                                        **
**                                                                  **
**                                                                  **
**  Copyright (C) 2011 ElecFraks.                                   **
**  This example code is in the public domain.                      **
**                                                                  **
**  Description:                                                    **
**  This file is a sample code for your reference.                  **
**  It's the v1.1 nRF24L01+ by arduino                              **
**  Created by ElecFreaks. Robi.W,24 July 2011                      **
**                                                                  **
**  http://www.elecfreaks.com                                       **
**                                                                  **
**   SPI-compatible                                                 **
**   CE - to digital pin 8                                          **
**   CSN - to digital pin 9  (SS pin)                               **
**   SCK - to digital pin 10 (SCK pin)                              **
**   MOSI - to digital pin 11 (MOSI pin)                            **
**   MISO - to digital pin 12 (MISO pin)                            **
**   IRQ - to digital pin 13 (MISO pin)                             **
*********************************************************************/


/*
  SD card basic file example

This example shows how to create and destroy an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4

created   Nov 2010
by David A. Mellis
updated 2 Dec 2010
by Tom Igoe


Thank you,
Enric.



PaulS

Quote
This is a part of the code I am using and continuously improving but I never make it work

You have to read data from the SD card. Can you do that?
You need to send data, using the nRF thing. Can you do that?
You need to receive data, using the nRF thing. Can you do that?
You need to save the data on the SD card. Can you do that?

PeterH

I haven't looked at the code because you haven't given me any clue what bits work and what bits don't, so it would be pointless. But the header caught my attention; it might be a good idea to review what's connected to what pin and make sure the software and hardware are both configured sensibly and consistently.
I only provide help via the forum - please do not contact me for private consultancy.

winner10920

Perhaps you don't have them wired correctly? I've done the excact same thing and it works no problems, where's the problem? Compiling? Actual results?
I imagine sending a whole jpeg will be complex since the nrf24l01 can only send 32 bytes at a time, and from what I know with the library I use for the sd, it only allows a 8.3 filename so the file must be 01234567.jpg to work

EnricF

Before all, thanks for your replies.



You have to read data from the SD card. Can you do that?
You need to send data, using the nRF thing. Can you do that?
You need to receive data, using the nRF thing. Can you do that?
You need to save the data on the SD card. Can you do that?


Yes, I am able to do everithing alone (by one side the RF comunication with a fixed payload and by other side read&write data on SD) but I was never able to do both things with one Arduino.


Perhaps you don't have them wired correctly? I've done the excact same thing and it works no problems, where's the problem? Compiling? Actual results?
I imagine sending a whole jpeg will be complex since the nrf24l01 can only send 32 bytes at a time, and from what I know with the library I use for the sd, it only allows a 8.3 filename so the file must be 01234567.jpg to work


Maybe I am connecting them wrong, which is your pin connection? I thing that for nRFL24L01P, the clock pin is called SCK (pin 10) or it is the IRQ (pin 13)? Therefore, SD also uses a clock pin (pin 10? or pin13 has I have in the code?). Furthermore, both share MISO and MOSI pins and both have a different CS (chip select) pins (one uses pin 9 and the other uses pin 4), also the transceivere requires two extra pins. It is ok?


I haven't looked at the code because you haven't given me any clue what bits work and what bits don't, so it would be pointless. But the header caught my attention; it might be a good idea to review what's connected to what pin and make sure the software and hardware are both configured sensibly and consistently.


I will review my pin connection. I will attach a picture with the pins names from the devices because sometimes they have a small differences (one letter more, less or a totally different name) on the pin name assigned and this confused me.

PaulS

Quote
Yes, I am able to do everithing alone (by one side the RF comunication with a fixed payload and by other side read&write data on SD) but I was never able to do both things with one Arduino.

Post all of your code for one side. Start with the sender. Explain what happens when that code is executed, and what does not happen.

winner10920

It sounds right, the only shared pins are 5v.gnd, miso,mosi, and sck
the sd cs, and the nrf ce,csn are separate and selectable in programming
I've done it with the arduino mega so I imagine it should work fine on the uno assuming you have the miso,mosi,sck on them right pins

EnricF

#7
Dec 02, 2011, 01:46 pm Last Edit: Dec 02, 2011, 01:55 pm by EnricF Reason: 1

Post all of your code for one side. Start with the sender. Explain what happens when that code is executed, and what does not happen.


This is the code I'm using for the sender:

Code: [Select]


#include <SD.h>

File myFile;

#include "NRF24L01.h"

//***************************************************
#define TX_ADR_WIDTH    5   // 5 unsigned chars TX(RX) address width
#define TX_PLOAD_WIDTH  32  // 32 unsigned chars TX payload

unsigned char TX_ADDRESS[TX_ADR_WIDTH]  =
{
 0x34,0x43,0x10,0x10,0x01
}; // Define a static TX address

unsigned char rx_buf[TX_PLOAD_WIDTH] = {0}; // initialize value
unsigned char tx_buf[TX_PLOAD_WIDTH] = {0};
//***************************************************
void setup()
{
 pinMode(CE,  OUTPUT);
 pinMode(SCK, OUTPUT);
 pinMode(CSN, OUTPUT);
 pinMode(MOSI,  OUTPUT);
 pinMode(MISO, INPUT);
 pinMode(IRQ, INPUT);
   
 pinMode(CSN2, OUTPUT);
  pinMode(10, OUTPUT);               //SD code specifies pin 10 must be an OUTPUT
 
 //  attachInterrupt(1, _ISR, LOW);// interrupt enable
 Serial.begin(9600);
 
 //If this function is commented, then RF works.
 //If used, then RF doesn't works.
 SD_Init();                       // Set SD memory
 
 
 init_io();                        // Initialize IO port
 unsigned char status=SPI_Read(STATUS);
 Serial.print("status = ");    
 Serial.println(status,HEX);     // There is read the mode's status register, the default value should be 'E'
 Serial.println("*******************TX_Mode Start****************************");
 
 TX_Mode();                       // set TX mode
       
}


void loop()
{
 int k = 0;
 for(;;)
 {
 
  for(int i=0; i<32; i++)
      {
       tx_buf[i] = k++;
       Serial.println(tx_buf[i], HEX);
     
      }
       
       
   unsigned char status = SPI_Read(STATUS);                   // read register STATUS's value
   if(status&TX_DS)                                           // if receive data ready (TX_DS) interrupt
   {
     SPI_RW_Reg(FLUSH_TX,0);                                  
     SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);       // write playload to TX_FIFO
   }
   if(status&MAX_RT)                                         // if receive data ready (MAX_RT) interrupt, this is retransmit than  SETUP_RETR                          
   {
     SPI_RW_Reg(FLUSH_TX,0);
     SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);      // disable standy-mode
   }
   SPI_RW_Reg(WRITE_REG+STATUS,status);                     // clear RX_DR or TX_DS or MAX_RT interrupt flag
   delay(500);
     
 
 }
}

//**************************************************
// Function: init_io();
// Description:
// flash led one time,chip enable(ready to TX or RX Mode),
// Spi disable,Spi clock line init high
//**************************************************
void init_io(void)
{
 digitalWrite(IRQ, 0);
 digitalWrite(CE, 0); // chip enable
 digitalWrite(CSN, 1);                 // Spi disable
 
 //For SD init????
 digitalWrite(CSN2, 1);                 // Spi disable
}

/**************************************************
* Function: SPI_RW();
*
* Description:
* Writes one unsigned char to nRF24L01, and return the unsigned char read
* from nRF24L01 during write, according to SPI protocol
**************************************************/
unsigned char SPI_RW(unsigned char Byte)
{
 unsigned char i;
 for(i=0;i<8;i++)                      // output 8-bit
 {
   if(Byte&0x80)
   {
     digitalWrite(MOSI, 1);
   }
   else
   {
     digitalWrite(MOSI, 0);
   }
   digitalWrite(SCK, 1);
   Byte <<= 1;                         // shift next bit into MSB..
   if(digitalRead(MISO) == 1)
   {
     Byte |= 1;                      // capture current MISO bit
   }
   digitalWrite(SCK, 0);
 }
 return(Byte);                  // return read unsigned char
}
/**************************************************/

/**************************************************
* Function: SPI_RW_Reg();
*
* Description:
* Writes value 'value' to register 'reg'
/**************************************************/
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value)
{
 unsigned char status;

 digitalWrite(CSN, 0);                   // CSN low, init SPI transaction
 status = SPI_RW(reg);                   // select register
 SPI_RW(value);                          // ..and write value to it..
 digitalWrite(CSN, 1);                   // CSN high again

 return(status);                   // return nRF24L01 status unsigned char
}
/**************************************************/

/**************************************************
* Function: SPI_Read();
*
* Description:
* Read one unsigned char from nRF24L01 register, 'reg'
/**************************************************/
unsigned char SPI_Read(unsigned char reg)
{
 unsigned char reg_val;

 digitalWrite(CSN, 0);           // CSN low, initialize SPI communication...
 SPI_RW(reg);                   // Select register to read from..
 reg_val = SPI_RW(0);           // ..then read register value
 digitalWrite(CSN, 1);          // CSN high, terminate SPI communication
 
 return(reg_val);               // return register value
}
/**************************************************/

/**************************************************
* Function: SPI_Read_Buf();
*
* Description:
* Reads 'unsigned chars' #of unsigned chars from register 'reg'
* Typically used to read RX payload, Rx/Tx address
/**************************************************/
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
 unsigned char status,i;

 digitalWrite(CSN, 0);                  // Set CSN low, init SPI tranaction
 status = SPI_RW(reg);          // Select register to write to and read status unsigned char

 for(i=0;i<bytes;i++)
 {
   pBuf[i] = SPI_RW(0);    // Perform SPI_RW to read unsigned char from nRF24L01
 }

 digitalWrite(CSN, 1);                   // Set CSN high again

 return(status);                  // return nRF24L01 status unsigned char
}
/**************************************************/

/**************************************************
* Function: SPI_Write_Buf();
*
* Description:
* Writes contents of buffer '*pBuf' to nRF24L01
* Typically used to write TX payload, Rx/Tx address
/**************************************************/
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
 unsigned char status,i;

 digitalWrite(CSN, 0);                  // Set CSN low, init SPI tranaction
 status = SPI_RW(reg);             // Select register to write to and read status unsigned char
 for(i=0;i<bytes; i++)             // then write all unsigned char in buffer(*pBuf)
 {
   SPI_RW(*pBuf++);
 }
 digitalWrite(CSN, 1);                   // Set CSN high again
 return(status);                  // return nRF24L01 status unsigned char
}
/**************************************************/

/**************************************************
* Function: TX_Mode();
*
* Description:
* This function initializes one nRF24L01 device to
* TX mode, set TX address, set RX address for auto.ack,
* fill TX payload, select RF channel, datarate & TX pwr.
* PWR_UP is set, CRC(2 unsigned chars) is enabled, & PRIM:TX.
*
* ToDo: One high pulse(>10us) on CE will now send this
* packet and expext an acknowledgment from the RX device.
**************************************************/
void TX_Mode(void)
{
 digitalWrite(CE, 0);
 
 SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH);    // Writes TX_Address to nRF24L01
 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // RX_Addr0 same as TX_Adr for Auto.Ack

 SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);      // Enable Auto.Ack:Pipe0
 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);  // Enable Pipe0
 SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a); // 500us + 86us, 10 retrans...
 SPI_RW_Reg(WRITE_REG + RF_CH, 40);        // Select RF channel 40
 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);   // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR
 SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e);     // Set PWR_UP bit, enable CRC(2 unsigned chars) & Prim:TX. MAX_RT & TX_DS enabled..
 SPI_Write_Buf(WR_TX_PLOAD,tx_buf,TX_PLOAD_WIDTH);
 
 digitalWrite(CE, 1);
}

void SD_Init(void)
{
   
    //SD code
    //If I commemnt this then the transcevier works, if not, then the transceiver doesn't works.
    //There is a code problem.
 Serial.print("Initializing SD card...");
 if (!SD.begin(7)) {
   Serial.println("initialization failed!");
   return;
 }
 Serial.println("initialization done.");


These are the code objectives:

1) Initialize SD
2) Initialize RF
3) Send a fixed payload (from 0x00 to 0x0F)

The system is not able to send the payload (I am noticed of that because I have a receiver prepared and for sure, the receivers works properly, because I have tested the code before for the receiver (sending a fix payload with a transmiter without an SD).

EnricF


It sounds right, the only shared pins are 5v.gnd, miso,mosi, and sck
the sd cs, and the nrf ce,csn are separate and selectable in programming
I've done it with the arduino mega so I imagine it should work fine on the uno assuming you have the miso,mosi,sck on them right pins


May you send me your pin connection? Or a code?

Thank you

EnricF


Post all of your code for one side. Start with the sender. Explain what happens when that code is executed, and what does not happen.


I've forgot to post the pin connections (now is a bit different from before according to this post comments).

Code: [Select]
**   SPI-compatible                                                 **
**   CE - to digital pin 8                                          **
**   CSN - to digital pin 9  (SS pin)                               **
**   SCK - to digital pin 13 (SCK pin)                              **
**   MOSI - to digital pin 11 (MOSI pin)                            **
**   MISO - to digital pin 12 (MISO pin)                            **
**   IRQ - to digital pin 6 (MISO pin)                             **
*********************************************************************/


/*
  SD card basic file example

This example shows how to create and destroy an SD card file
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS (CSN2) - pin 7

winner10920

Here's the code im using, there's alot more here for some other things, srry im on my phone and can only copy and paste really
Code: [Select]
#include "TFTLCD.h"
#include "TouchScreen.h"
#include "SD.h"
#include "OneWire.h"
#include "DallasTemperature.h"
#include "SPI.h"
#include "Mirf.h"
#include "nRF24L01.h"
#include "MirfHardwareSpiDriver.h"

#define LCD_CS A3    // Chip Select goes to Analog 3
#define LCD_CD A2    // Command/Data goes to Analog 2
#define LCD_WR A1    // LCD Write goes to Analog 1
#define LCD_RD A0    // LCD Read goes to Analog 0
#define LCD_RESET A8  // reset goes to a8
#define LCD_BRIGHTNESS 32 //brightness of backlite pin to 32

// Color definitions
#define BLACK           0x0000
#define BLUE            0x001F
#define RED             0xF800
#define GREEN           0x07E0
#define CYAN            0x07FF
#define MAGENTA         0xF81F
#define YELLOW          0xFFE0
#define WHITE           0xFFFF

//for touchscreen
#define YP A5  // Y+ goes to A5
#define XM A4  // X- goes to A4
#define YM 30  // Y- goes to pin 30
#define XP 31   // X+ goes to pin 31

#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

#define MINPRESSURE 50
#define MAXPRESSURE 1000

//for temp sensor

OneWire oneWire(39);
//ONE-WIRE-BUS = pin for one wie comm, (39)
DallasTemperature sensors(&oneWire);

#define TEMPERATURE_PRECISION 9
DeviceAddress insideThermometer = {
 0x28, 0x70, 0x45, 0x3F, 0x03, 0x00, 0x00, 0x8B };
DeviceAddress outsideThermometer   = {
 0x28, 0x4C, 0x62, 0x3F, 0x03, 0x00, 0x00, 0xB2 };
DeviceAddress boardThermometer   = {
 0x28, 0x4C, 0x62, 0x3F, 0x03, 0x00, 0x00, 0xB2 };

//mirf
byte data[32]; //size of payload


unsigned long time = 0;
float mintime = 0;
unsigned long sensorreadtimer = 0;
unsigned long sensdisptimer = 0;
unsigned long datalogtimer = 0;

int HOME = 1;  //page to a word to simplify code and to not get confused
int settings = 2;
int lights = 3;
int sensorspage = 4;
int deletedatalog = 5;
int pwm = 6;
int page = HOME;  //setting the first page to start on
int refresh = 1;  //uses a var to know when to redraw, used to optimize speed of overall process so its not constantly drawing.

float temperature1 = 0;  // inside temp
float temperature2 = 0;  // outside temp
float temperature3 = 0; // board temp

boolean ceilinglights = 0;  //true or false for the zone control pins to be on or off
boolean floorlights = 1;
boolean outsidelights = 0;
boolean headlights = 0;
boolean datalogenable = 1;  //datalog normally on
int pwm1 = 0;
int pwm2 = 0;
int pwm3 = 0;
int pwm4 = 0;

float batteryvoltage = 0.1;  //displayed battery voltage
float rawvoltage = 0.1;  // reading from voltage sensor
float batterycurrent = 0.1; // displayed battery current
float rawcurrent = 0.1; // reading from current sensor
int rando = 45;  // size of the circle in the setup
int screendelay = 300;  //a delay to prevent the touchscreen from being used while the screen is stil refreshing
int touchdelay = 250;  // a delay to prevent the touch screen from toggling too fast
int sensreaddelay = 300;  // a time that the sensors will re-read, not a delay
int sensdispdelay = 1000; // a time to re-display the sensors information on the sensors screen
unsigned long datalogdelay = 3000;  // a time to write the data log , interval

int tempcount = 0;
int tempdelay = 10;
int temp1 = 0;
int temp2 = 0;
int temp3 = 0;
float datalogsize = 0; // size of datalog

boolean brightness = 1; // brightness var
unsigned long screentimeout = 30000;
unsigned long screentimer = 0;
Point p ;

float vref = 5.00;  // to make more acurate eading, cuurently set to my laptops usb out
String datalog = "";
boolean sdver = 0;


int devid = 0;


boolean deletechoice = 0;
/*
outputs are
ceilinglights = pin 40
floorlights = pin 41
outsidelights = pin 42
headlights = pin 43
*/

//sd cs to 53
//sd miso to 50
//sd mosi to 51
//sd sck to 52
void(* resetFunc)() = 0;


TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
void setup() {
 
 pinMode(53, OUTPUT);  //sd cs
 pinMode(10, OUTPUT);  //led1
 pinMode(11, OUTPUT);  //led2  wireless light
 pinMode(12, OUTPUT);  //led3  sd write
 pinMode(13, OUTPUT);  //led4  running light
 pinMode(33, OUTPUT);  //led5
 pinMode(35, OUTPUT);  //led6
 pinMode(37, OUTPUT);  //led7
 pinMode(40, OUTPUT);  //relay1     ceilinglights
 pinMode(41, OUTPUT);  //relay2     floorlights
 pinMode(42, OUTPUT);  //relay3     outsidelights
 pinMode(43, OUTPUT);  //relay4     headlights
 pinMode(39, INPUT);   //one wire bus
 pinMode(2, OUTPUT);   //pwm1
 pinMode(3, OUTPUT);   //pwm2
 pinMode(4, OUTPUT);   //pwm3
 pinMode(5, OUTPUT);   //pwm4
 pinMode(LCD_BRIGHTNESS, OUTPUT); //screen brightness
 digitalWrite(LCD_BRIGHTNESS, 1);
 digitalWrite(40, 0);
 digitalWrite(13, 1);
 digitalWrite(41, 1);
 digitalWrite(42, 0);
 digitalWrite(43, 0);
 digitalWrite(A8, 0);
 delay(300);
 digitalWrite(A8, 1);

 pinMode(A15, INPUT); //voltage sensor
 pinMode(A14, INPUT); //current sensor

 Mirf.cePin = 46;
 Mirf.csnPin = 47;
 Mirf.spi = &MirfHardwareSpi;
 Mirf.init();

 Mirf.setRADDR((byte *)"reply");
 Mirf.setTADDR((byte *)"sendr");
 Mirf.payload = 32;
 Mirf.channel = 8;
 Mirf.config();

 randomSeed(analogRead(A0));
 tft.reset();
 tft.initDisplay();
 tft.fillScreen(BLACK);
 tft.setTextColor(WHITE);
 tft.setTextSize(3);
 tft.setRotation(3);
 tft.setCursor(0, (tft.height() / 2) - 15);  // set to half the screen then minus a few so the words are centered
 tft.println("BY: RICH FEDOR"); // me


 while(rando > 1){                                                 // a little fun thing
   tft.drawCircle(235, 180, rando, random(0x0000, 0xFFFF));
   rando = rando - 1;
 }
 tft.setTextSize(2);
 tft.setCursor(0, (tft.height() / 2) + 15);

 sdver = SD.begin();  //check if sd is present
 if(sdver == 1){
   tft.println("SD GOOD");
 }
 if(sdver == 0){
   tft.println("SD BAD");
   datalogenable = 0;
 }
 delay(100);
 sensors.begin();
 sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
 sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);
 sensors.setResolution(boardThermometer, TEMPERATURE_PRECISION);
 temp1 = sensors.getResolution(insideThermometer);
 temp2 = sensors.getResolution(outsideThermometer);
 temp3 = sensors.getResolution(boardThermometer);
 if(temp1 !=9){
   tft.println("TEMP1 BAD");
 }
 if(temp1 == 9){
   tft.println("TEMP1 GOOD");
   temp1 = 1;
 }
 if(temp2 !=9){
   tft.println("TEMP2 BAD");
 }
 if(temp2 == 9){
   tft.println("TEMP2 GOOD");
   temp2 = 1;
 }
 if(temp3 !=9){
   tft.println("TEMP3 BAD");
 }
 if(temp3 == 9){
   tft.println("TEMP3 GOOD");
   temp3 = 1;
 }
 oneWire.reset();

 delay(500);  // gives you a chance to see my little fun thing
 digitalWrite(13, 0);
}


This is what I do to set it up

winner10920

heres my loop code for it
Code: [Select]
  if(time - datalogtimer > datalogdelay){  // datalog process
    datalogtimer = time;
    if(datalogenable == 1 && sdver == 1){  // will only log if is enabled in settings
      // proces to write to sd card
      digitalWrite(12,1);
      File dataFile = SD.open("datalog.txt", FILE_WRITE);//print datalog
      dataFile.print("Minutes = ");
      dataFile.println(mintime) ;   
      dataFile.print("temperature1 = ");
      dataFile.println(temperature1);
      dataFile.print("temperature2 = ");
      dataFile.println(temperature2);
      dataFile.print("temperature3 = ");
      dataFile.println(temperature3);
      dataFile.print("batteryvoltage = ");
      dataFile.println(batteryvoltage);
      dataFile.print("batterycurrent = ");
      dataFile.println(batterycurrent);
      dataFile.println("");
      dataFile.print( "-----------------------------"); 
      dataFile.println("");
      dataFile.close();

    }//end print file
    digitalWrite(12, 0);
  }//end datalog process
  //begin wireless process
  if(Mirf.dataReady()){
    digitalWrite(11,1);
    Mirf.getData(data);
    if(data[0] == 'R'){
      data[0] = 'T';
      data[1] = '1';
      data[2] = '=';
      data[3] = (int(temperature1) / 100) + 48;
      data[4] = (int(temperature1) - ((int(temperature1) / 100) * 100)) / 10 + 48;
      data[5] = (int(temperature1) - ((int(temperature1) / 100) * 100))-(((int(temperature1) - ((int(temperature1) / 100) * 100)) / 10)* 10) + 48;
      data[6] = 'T';
      data[7] = '2';
      data[8] = '=';
      data[9] = (int(temperature2) / 100) + 48;
      data[10] = (int(temperature2) - ((int(temperature2) / 100) * 100)) / 10 + 48;
      data[11] = (int(temperature2) - ((int(temperature2) / 100) * 100))-(((int(temperature2) - ((int(temperature2) / 100) * 100)) / 10)* 10) + 48;
      data[12] = 'T';
      data[13] = '3';
      data[14] = '=';
      data[15] = (int(temperature3) / 100) + 48;
      data[16] = (int(temperature3) - ((int(temperature3) / 100) * 100)) / 10 + 48;
      data[17] = (int(temperature3) - ((int(temperature3) / 100) * 100))-(((int(temperature3) - ((int(temperature3) / 100) * 100)) / 10)* 10) + 48;
      data[18] = 'V';
      data[19] = '=';
      batteryvoltage = batteryvoltage * 10;
      data[20] = (int(batteryvoltage) / 100) + 48;
      data[21] = (int(batteryvoltage) - ((int(batteryvoltage) / 100) * 100)) / 10 + 48;
      data[22] = '.';
      data[23] = (int(batteryvoltage) - ((int(batteryvoltage) / 100) * 100))-(((int(batteryvoltage) - ((int(batteryvoltage) / 100) * 100)) / 10)* 10) + 48;
      batterycurrent = batterycurrent * 10;
      data[24] = 'A';
      data[25] = '=';
      data[26] = (int(batterycurrent) / 100) + 48;
      data[27] = '.';
      data[28] = (int(batterycurrent) - ((int(batterycurrent) / 100) * 100)) / 10 + 48;
      data[29] = (int(batterycurrent) - ((int(batterycurrent) / 100) * 100))-(((int(batterycurrent) - ((int(batterycurrent) / 100) * 100)) / 10)* 10) + 48;
      data[30] = '~';
      data[31] = '~';

      Mirf.setTADDR((byte *)"sendr");
      Mirf.send(data);
    }

    if(data[0] == 'C'){
      refresh = 1;
      if(data[1] == '1'){
        digitalWrite(2, 1);
        digitalWrite(3, 1);
        digitalWrite(4, 1);
        digitalWrite(5, 1);
        digitalWrite(40, 1);
        digitalWrite(41, 1);
        digitalWrite(42, 1);
        digitalWrite(43, 1);
        digitalWrite(10, 1);
        digitalWrite(11, 1);
        digitalWrite(12, 1);
        digitalWrite(13, 1);
        digitalWrite(33, 1);
        digitalWrite(35, 1);
        digitalWrite(37, 1);
        ceilinglights = 1; 
        floorlights = 1;
        outsidelights = 1;
        headlights = 1;
        datalogenable = 1;
        pwm1 = 1;
        pwm2 = 1;
        pwm3 = 1;
        pwm4 = 1;

      }
      if(data[1] == '0'){
        digitalWrite(2, 0);
        digitalWrite(3, 0);
        digitalWrite(4, 0);
        digitalWrite(5, 0);
        digitalWrite(40, 0);
        digitalWrite(41, 0);
        digitalWrite(42, 0);
        digitalWrite(43, 0);
        digitalWrite(10, 0);
        digitalWrite(11, 0);
        digitalWrite(12, 0);
        digitalWrite(13, 0);
        digitalWrite(33, 0);
        digitalWrite(35, 0);
        digitalWrite(37, 0);
        ceilinglights = 0; 
        floorlights = 0;
        outsidelights = 0;
        headlights = 0;
        datalogenable = 0;
        pwm1 = 0;
        pwm2 = 0;
        pwm3 = 0;
        pwm4 = 0;
      }   
      if(data[1] == 'F'){
        for(int i = 0; i < 5; i++){
          digitalWrite(2, 1);
          digitalWrite(3, 1);
          digitalWrite(4, 1);
          digitalWrite(5, 1);
          digitalWrite(40, 1);
          digitalWrite(41, 1);
          digitalWrite(42, 1);
          digitalWrite(43, 1);
          digitalWrite(10, 1);
          digitalWrite(11, 1);
          digitalWrite(12, 1);
          digitalWrite(13, 1);
          digitalWrite(33, 1);
          digitalWrite(35, 1);
          digitalWrite(37, 1);
          delay(200);
          digitalWrite(2, 0);
          digitalWrite(3, 0);
          digitalWrite(4, 0);
          digitalWrite(5, 0);
          digitalWrite(40, 0);
          digitalWrite(41, 0);
          digitalWrite(42, 0);
          digitalWrite(43, 0);
          digitalWrite(10, 0);
          digitalWrite(11, 0);
          digitalWrite(12, 0);
          digitalWrite(13, 0);
          digitalWrite(33, 0);
          digitalWrite(35, 0);
          digitalWrite(37, 0);
          delay(200);
        }
        ceilinglights = 0; 
        floorlights = 0;
        outsidelights = 0;
        headlights = 0;
        datalogenable = 0;
        pwm1 = 0;
        pwm2 = 0;
        pwm3 = 0;
        pwm4 = 0;
      }
    }     

    digitalWrite(11,0);
  }
  Mirf.isSending();
  //end wireless process

EnricF

Hi winner,

thank you for your code. Next week I will compare the codes and then try to fix my errors.

Thank you,
Enric.

EnricF

Hi all, I want to regret all for your attention. You were so helpful on my project. Now I'am able to use two SPI devices on a single Arduino. Thank you! This post can be closed. I attach the code to help anyone who need it.


Go Up