I have a setup that has an arduino and accelerometer and a transmitter sending the acceleration to another arduino that has a receiver and a sd card to store the date. I am unable to get the data in the right format.
Transmitter
/*Arduino --------------- MMA8452Q Breakout
5V --------------- 3.3V (used a level shifter)
GND --------------- GND
SDA (A4) --\/330 Ohm\/-- SDA
SCL (A5) --\/330 Ohm\/--
pin 9 ------------- tx pin
vin -------- tranmiter v+
*/
#include <VirtualWire.h>
#include <Wire.h>
#include <SFE_MMA8452Q.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
MMA8452Q accel;
void setup()
{
Serial.begin(2400);
accel.init();
// or accel.init(SCALE_4G); gives +/- 4g
//or accel.init(SCALE_8G); give +/- 8g
//ODR_800, ODR_400, ODR_200, ODR_100, ODR_50, ODR_12,
// ODR_6, or ODR_1.
//Sets to 800, 400, 200, 100, 50, 12.5, 6.25, or 1.56 Hz output
// //accel.init(SCALE_8G, ODR_6);
vw_set_ptt_inverted(true);// Required for RF Link module
vw_setup(2000); // Bits per sec
vw_set_tx_pin(9);
}
void loop()
{
// accelerometer code
// Use the accel.available() function to wait for new data
// from the accelerometer.
if (accel.available())
{
// First, use accel.read() to read the new variables:
accel.read();
// accel.read() will update two sets of variables.
// * int's x, y, and z will store the signed 12-bit values
// read out of the accelerometer.
// * floats cx, cy, and cz will store the calculated
// acceleration from those 12-bit values. These variables
// are in units of g's.
// Check the two function declarations below for an example
// of how to use these variables.
printCalculatedAccels();
//printAccels(); // Uncomment to print digital readings
// The library also supports the portrait/landscape detection
// of the MMA8452Q. Check out this function declaration for
// an example of how to use that.
printOrientation();
char x = convert(accel.x); //converts int to string
char y = convert(accel.y);
char z = convert(accel.z);
Serial.println(); // Print new line every time.
// tranmitter code
const char *msg = ("x:/n", x, "y:/n", y, "z:/n", z);
digitalWrite(13, true); // Flash a light to show transmitting
vw_send((uint8_t *)msg, strlen(msg));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false);
delay(200);
}
}
// The function demonstrates how to use the accel.x, accel.y and
// accel.z variables.
// Before using these variables you must call the accel.read()
// function!
void printAccels()
{
Serial.print(accel.x, 3);
Serial.print("\t");
Serial.print(accel.y, 3);
Serial.print("\t");
Serial.print(accel.z, 3);
Serial.print("\t");
}
// This function demonstrates how to use the accel.cx, accel.cy,
// and accel.cz variables.
// Before using these variables you must call the accel.read()
// function!
void printCalculatedAccels()
{
Serial.print(accel.cx, 3);
Serial.print("\t");
Serial.print(accel.cy, 3);
Serial.print("\t");
Serial.print(accel.cz, 3);
Serial.print("\t");
}
// This function demonstrates how to use the accel.readPL()
// function, which reads the portrait/landscape status of the
// sensor.
void printOrientation()
{
// accel.readPL() will return a byte containing information
// about the orientation of the sensor. It will be either
// PORTRAIT_U, PORTRAIT_D, LANDSCAPE_R, LANDSCAPE_L, or
// LOCKOUT.
byte pl = accel.readPL();
switch (pl)
{
case PORTRAIT_U:
Serial.print("Portrait Up");
break;
case PORTRAIT_D:
Serial.print("Portrait Down");
break;
case LANDSCAPE_R:
Serial.print("Landscape Right");
break;
case LANDSCAPE_L:
Serial.print("Landscape Left");
break;
case LOCKOUT:
Serial.print("Flat");
break;
}
}
char convert(int n)
{
int Number = n; // number to be converted to a string
String string = String(Number);
string.toCharArray(Result[],string.length());
return Result;
}
Receiver
#include <SD.h>
#include <VirtualWire.h>
const int chipSelect = 4;
void setup()
{
Serial.begin(2400);
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
vw_set_rx_pin(8); //set receiver pin to 8
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
}
void loop()
{
// make a string for assembling the data to log:
String dataString = "";
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
digitalWrite(9, true); // Flash a light to show received good message
// Message with a good checksum received, dump it.
Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i]);
Serial.print(" ");
dataString += ((char)buf[i]);
}
Serial.println("");
digitalWrite(9, false);
}
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}
}
Right now I am getting an invalid conversion form char to constant char* but am lost on how to fix it. Thanks for the help!