Hi Everyone,
I would like to ask you guys for some help in a project I am working.
This is the situation.
In the below code, I get the data from a Bluetooth module and store them in an array of 20 elements.
The thing is that I would like to pass this array of elements to a function, but only the elements the function gets. In other words, at the beginning of the sketch, I define the array with a maximum of 20 elements, but I just want to pass to the function only the elements inside and not the 20.
For instance, if I get the characters "hello" from the Bluetooth, I want to save "hello" in the array. Once "hello" is kept within the array, I would like only to pass to the array and write "hello" on a sdcard file.
So far, I am able to do that, but for some reason that I can not understand yet is why all the 20 elements are written within the sdcard file. That is, "hello" is written on the file of the sdcard but also the rest of the characters that make up the array.
How might I write on the sdcard file only those characters sent by the Bluetooth module and not the whole size of the array?.
Thank you in advance for your help.
Regards.
P.S. I used capital letters (comments) in the sketch to indicate the part of the code related to the above explanation
/*********************************************************************
This is an example for our nRF8001 Bluetooth Low Energy Breakout
Pick one up today in the adafruit shop!
------> http://www.adafruit.com/products/1697
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Kevin Townsend/KTOWN for Adafruit Industries.
MIT license, check LICENSE for more information
All text above, and the splash screen below must be included in any redistribution
*********************************************************************/
// This version uses the internal data queing so you can treat it like Serial (kinda)!
#include <SPI.h>
#include <SD.h>
#include "Adafruit_BLE_UART.h"
File myFile;
int i;
char c[20];
//const int sdarray_size = 18;
//char sdarray[sdarray_size];
// Connect CLK/MISO/MOSI to hardware SPI
// e.g. On UNO & compatible: CLK = 13, MISO = 12, MOSI = 11
#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 2 // This should be an interrupt pin, on Uno thats #2 or #3
#define ADAFRUITBLE_RST 9
Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
/**************************************************************************/
/*!
Configure the Arduino and start advertising with the radio
*/
/**************************************************************************/
void setup(void)
{
Serial.begin(9600);
while(!Serial); // Leonardo/Micro should wait for serial init
Serial.println(F("Adafruit Bluefruit Low Energy nRF8001 Print echo demo"));
// BTLEserial.setDeviceName("NEWNAME"); /* 7 characters max! */
BTLEserial.begin();
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
}
/**************************************************************************/
/*!
Constantly checks for new events on the nRF8001
*/
/**************************************************************************/
aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED;
void loop()
{
// Tell the nRF8001 to do whatever it should be working on.
BTLEserial.pollACI();
// Ask what is our current status
aci_evt_opcode_t status = BTLEserial.getState();
// If the status changed....
if (status != laststatus) {
// print it out!
if (status == ACI_EVT_DEVICE_STARTED) {
Serial.println(F("* Advertising started"));
}
if (status == ACI_EVT_CONNECTED) {
Serial.println(F("* Connected!"));
}
if (status == ACI_EVT_DISCONNECTED) {
Serial.println(F("* Disconnected or advertising timed out"));
}
// OK set the last status change to this one
laststatus = status;
}
if (status == ACI_EVT_CONNECTED) {
// Lets see if there's any data for us!
if (BTLEserial.available()) {
Serial.print("* "); Serial.print(BTLEserial.available()); Serial.println(F(" bytes available from BTLE"));
}
// OK while we still have something to read, get a character and print it out
while (BTLEserial.available()) {
//char c = BTLEserial.read();
for (int i=0; i<20; i++)
{
c[i] = BTLEserial.read(); //I READ VALUES FROM THE BLUETOOTH MODULE AND KEEP THEM IN AN ARRAY OF 20 ELEMENTS
//Serial.print(c[i]);
}
sdcard(c, sizeof(c) / sizeof(c[0]));//I PASS THE ARRAY TO THE FUNCTION WITH THE CHARACTERS (ELEMENTS) SENT BY THE BLUETOOTH MODULE
}
// Next up, see if we have any data to get from the Serial console
if (Serial.available()) {
// Read a line from Serial
Serial.setTimeout(100); // 100 millisecond timeout
String s = Serial.readString();
// We need to convert the line to bytes, no more than 20 at this time
uint8_t sendbuffer[20];
s.getBytes(sendbuffer, 20);
char sendbuffersize = min(20, s.length());
Serial.print(F("\n* Sending -> \"")); Serial.print((char *)sendbuffer); Serial.println("\"");
// write the data
for (i=0; i>=1; i--) {
BTLEserial.println("");
}
BTLEserial.write(sendbuffer, sendbuffersize);
//i++;
BTLEserial.println("");
}
}
}
char sdcard(char y[], int siz)//HERE I GOT THE ARRAY AND ITS SIZE WITH THE ELEMENTS
{
myFile = SD.open("thursday.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print(y); //AT THIS POINT, I TRY TO PRINT ON THE SERIAL MONITOR THE ARRAY WITH ONLY THOSE ELEMENTS RECEIVED FROM THE BLUETOOH MODULE
myFile.println(y);// AND STORE THEM IN A .TXT FILE
Serial.println("");
myFile.close();
}
// Serial.println("done.");
else {
// if the file didn't open, print an error:
Serial.println("error opening sdcard.txt");
}
}