Go Down

Topic: converting data in an array after a wireless transmission (Read 978 times) previous topic - next topic

instrumentek

HI, I'm using a nRF24L01 to send a value between to Arduino's. The problem is on the receiver side the data is in an array format. I want to have the value stored as a regular int. How do you convert an array like this to a regular int?

I am new to this so.. would there be a way just to transmit the INT not in an array format?

This is the receiver
Code: [Select]
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>


char rec[16];

int data[16];
int numbers[3];
void setup(){
 
  Serial.begin(9600);
  pinMode(4, OUTPUT);
   
    Mirf.spi = &MirfHardwareSpi;
    Mirf.init();
   Mirf.setRADDR((byte *)"serv1");
    Mirf.payload = 16;
   
   char data[Mirf.payload];
 
  Mirf.config();
}

void loop(){
 
   data[Mirf.payload];
 
  if(!Mirf.isSending() && Mirf.dataReady()){
  }
    while(!Mirf.dataReady()){
    }
    Mirf.getData((byte *) &rec);


   
      Serial.print(rec[0]);
      Serial.print(rec[1]);
      Serial.println(rec[2]);
 
   
}   
     
 



Here is the transmitter
Code: [Select]
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

int rate = 0;
char buffer[16];
char data[16];



void setup(){
  Serial.begin(9600);

  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"clie1");
  Mirf.payload = 16;
  Mirf.config();
  }

void loop(){
rate = rate + 1;
   

    Mirf.setTADDR((byte *)"serv1");


    data[Mirf.payload];

sprintf(buffer,"%1d",rate);   


    Mirf.send((byte *) &buffer);


    while(Mirf.isSending()){
    }

   
    delay(1000);

    }




maniacbug

#1
Jun 17, 2011, 04:34 am Last Edit: Jun 17, 2011, 04:36 am by maniacbug Reason: 1
Lose the array.

Code: [Select]

int rate;
...
Mirf.payload = sizeof(rate);
...
Mirf.send(&rate);
...
Mirf.getData(&rate);


Also please consider using the "Auto-Format" command on code before posting to make it easier for folks to read.

instrumentek

Thanks, i tried this. I get an error compiling when I try to put an int directly into the Mirf.send function. I found another post on converting array into int. Its ugly but it works.  I did not realize there was a auto format tool. I will use it from now on. Thanks for the pointer.

Code: [Select]
char buffer[3];
  buffer[0] = rec[0];
  buffer[1] = rec[1];
  buffer[3] = '\0';
  int  n;
  n = atoi(buffer);

  Serial.println(n);

maniacbug


Thanks, i tried this. I get an error compiling when I try to put an int directly into the Mirf.send function.


Aargh, Mirf kills me.  I suppose it prefers this...

Code: [Select]

Mirf.send((byte*)&rate);

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy