I have a sensor setup to store the values every 75ms and store that in a string. So for example every 75ms it adds "101," (comma separated) to the string of results. The problem is that I want to run this for roughly 2 seconds which would give me 27 values and the string seems to overflow before that. I have read about string arrays, and could say for the first 10 values add that to string_a then the next 10 string_b ,but was wondering if there was a better way to do this since the total time will not always be constant? Any thoughts or help would be greatly appreciate.
You are adding 53.333 characters per second so over two seconds you should accumulate about 107 characters. That shouldn't be a problem so you probably have a mistake in your code. If you post your sketch perhaps someone will spot the mistake.
It is highly likely that it is my code. Thanks for your help.
#include <AcceleroMMA7361.h> // accelerometer library
AcceleroMMA7361 accelero; // initialize library
unsigned long lastTime; // used to compare time
const unsigned long interval = 75; // how many times a second does it collect data ex. 75 equals every 75ms
boolean startup; // test w/ true or false
int x; // x axis
int y;
int z;
int sum = 0;
int record = 0; // record the data
int bite_size = 0; //not use right now
int pushButton1 = 2; // button 1
int prevState1 = LOW; // comparer to state of button 1
int pushButton2 = 3;
int prevState2 = LOW;
int printer = 0; // does it send the data to serial monitor or not
String sum1; // string to store data
String total_printed; // where data is stored
void setup()
{
Serial.begin(9600);
accelero.begin(13, 12, 11, 10, A0, A1, A2); // pins used for accelerometer
accelero.setARefVoltage(5); //sets the AREF voltage to 3.3V
accelero.setSensitivity(LOW); //sets the sensitivity to +/-6G
accelero.calibrate(); // calibrate the sensor
sum = 0;
sum1 = 0;
pinMode(A5,OUTPUT); // pin A5 connected to LED
pinMode(pushButton1, INPUT); // voltage increase when pins is pressed so pins detect HIGH or LOW
pinMode(pushButton2, INPUT);
pinMode(7,OUTPUT);
delay(50);
}
void loop()
{
if(startup == false){ // has the startup function been run
startsup(); // blink A5 twice to signal that everything is ready and setup has completed
}
int currState1 = digitalRead(pushButton1); // read the state of push button 1
int currState2 = digitalRead(pushButton2);
if (currState1 != prevState1){
record = record + 1; // add 1 to record
delay(350); // wait until push is competed so it does not record change twice
}
if (currState2 != prevState2){ // test whether state of pushbutton has changed
printer = printer + 1;
delay(350);
}
if (record == 1){
collect();
digitalWrite(7,HIGH); // light up 7 to indicate that data is being collected
}
if (record == 2){ //set record back to 0 so that record does not go 3,4,5....
record = 0;
digitalWrite(7,LOW);// turn off indicator light
lastTime = millis() - interval; // make sure that lastTime keeps up with the time so that it does not record the entire time
}
if (record == 0){
digitalWrite(7,LOW);
lastTime = millis() - interval;
}
if (printer == 1){
send_data();
}
if (printer == 2){
printer = 0;
}
if (printer == 0 ){
digitalWrite(A5,LOW);
}
} // end of main loop
void collect(){ // function that collects the data
if(millis() - lastTime > interval){ //has it been x ms and time to run loop agian
x = accelero.getXAccel(); // collect on x axis
y = accelero.getYAccel();
z = accelero.getZAccel();
sum = sqrt((x*x)+(y*y)+(z*z)); // magnitude of the 3 axis
sum1 = sum1 +","+String(sum); // add to string seperated by comma
total_printed = sum1;
Serial.print(total_printed);// print data stored in total_printed
lastTime += interval; // add time so that loop does not run forever
}
}
void startsup(){ // function that blinks A5 twice to signal everything is ready
digitalWrite(A5,HIGH);
delay(500);
digitalWrite(A5,LOW);
delay(500);
digitalWrite(A5,HIGH);
delay(500);
digitalWrite(A5,LOW);
startup = true;
}
void send_data(){ // function to send the data
delay(100);
digitalWrite(A5,HIGH);
Serial.print("Here it is: "); // start of data
Serial.print(total_printed); // send the data
Serial.print("That is it"); // end of data
delay(100);
digitalWrite(A5,LOW);
printer = 0; // reset all values meanin all data wiped and ready to run agian
sum1 = 0;
sum = 0;
}
[\code]
Your problem is right here.
String sum1; // string to store data
String total_printed; // where data is stored
Ditch the String class, and learn to use char arrays.
Is there a limit to the string size? I understand that a char array is better, but why wouldn't a string work?
but why wouldn't a string work?
Look, there is a huge difference between a string and a String. You are not using strings. You should be.
You are using Strings. The problem with Strings is all the memory allocation/deallocation going on. There is a bug in the free() function that corrupts memory. Call free() often enough (and String::~String does) and you will have problems. As you are.
A string (a NULL terminated array of chars) WILL work. Just fine.
Why have the startsup() function instead of just putting that in setup()?
Why is total_printed printed in both collect() and send_data()?