Pages: [1]   Go Down
Author Topic: converting data in an array after a wireless transmission  (Read 812 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
#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);

    }


Logged

Seattle, WA
Offline Offline
God Member
*****
Karma: 11
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Lose the array.

Code:
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.
« Last Edit: June 16, 2011, 09:36:20 pm by maniacbug » Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 37
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
char buffer[3];
  buffer[0] = rec[0];
  buffer[1] = rec[1];
  buffer[3] = '\0';
  int  n;
  n = atoi(buffer);

  Serial.println(n);
Logged

Seattle, WA
Offline Offline
God Member
*****
Karma: 11
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Mirf.send((byte*)&rate);
Logged


Pages: [1]   Go Up
Jump to: