Working with several local files.

11 Apr 2011

Dear all ...

I´ve got a problem working with several local files at the same time. I´m trying to open a file for reading and another for writting. Basically, the first one includes different constants and the other stores the measured data. The code is the following ...

  1. include "mbed.h"

LocalFileSystem local("local");

float mean_S1, mean_S2, mean_S3; float L_S1, L_S2, L_S3; float V_micro, alfa_S1, beta_S1, alfa_S2, beta_S2, alfa_S3, beta_S3; int i;

/*-------------*/ /*---- M A I N P R O G R A M ----*/ /*-------------*/

int main() {

FILE *fp_2 = fopen("/local/Calib.txt", "a"); fscanf(fp_2, "%f %f %f %f %f %f %f", V_micro, alfa_S1, beta_S1, alfa_S2, beta_S2, alfa_S3, beta_S3); fclose(fp_2);

FILE *fp = fopen("/local/Results.txt", "a"); fprintf(fp, "Sensor 1 Sensor 2 Sensor 3\n"); fclose(fp);

while(1) {

mean_S1 = 0; mean_S2 = 0; mean_S3 = 0; i = 0;

for(i=0; i<1000; i++){ mean_S1 = mean_S1 + V_micro; mean_S2 = mean_S2 + V_micro; mean_S3 = mean_S3 + V_micro; }

mean_S1 = mean_S1/i; mean_S2 = mean_S2/i; mean_S3 = mean_S3/i;

L_S1 = mean_S1*alfa_S1+beta_S1; L_S2 = mean_S2*alfa_S2+beta_S2; L_S3 = mean_S3*alfa_S3+beta_S3;

FILE *fp = fopen("/local/Results.txt", "a"); fprintf(fp, "\n%f %f %f", L_S1, L_S2, L_S3); fclose(fp); } }

Previously, I´ve created both files "Results.txt" and "Calib.txt". The problem is very easy ... no data is taken from the Calib file and the "Results" equals zero. Does anyone know why the constant are not loaded?

Thanks in advance.

11 Apr 2011

I cannot see any thing crazy wrong, but ALWAYS print out your answers along the way.

NOTE, your code will run forever .......

the test will keep going, never allowing local disk to free up.

try this

include "mbed.h"
LocalFileSystem local("local");

float mean_S1, mean_S2, mean_S3; float L_S1, L_S2, L_S3; 
float V_micro, alfa_S1, beta_S1, alfa_S2, beta_S2, alfa_S3, beta_S3; 
int i;

/*-------------*/ /*---- M A I N P R O G R A M ----*/ /*-------------*/

int main() 
{

	FILE *fp_2 = fopen("/local/Calib.txt", "a"); 
	fscanf(fp_2, "%f %f %f %f %f %f %f", V_micro, alfa_S1, beta_S1, alfa_S2, beta_S2, alfa_S3, beta_S3); 
	fclose(fp_2);

	FILE *fp = fopen("/local/Results.txt", "a"); 
	fprintf(fp, "Sensor 1 Sensor 2 Sensor 3\n"); 
	fclose(fp);
	
	
	// Read Succsesful ??
	printf ("Values READ IN = %f %f %f %f %f %f %f \r\n", V_micro, alfa_S1, beta_S1, alfa_S2, beta_S2, alfa_S3, beta_S3); 

	
	MyLED = 0;
	


		mean_S1 = 0; 
		mean_S2 = 0; 
		mean_S3 = 0; 
		i = 0;

		for(i=0; i<1000; i++)
		{ 
			mean_S1 = mean_S1 + V_micro; 
			mean_S2 = mean_S2 + V_micro; 
			mean_S3 = mean_S3 + V_micro;
		}
		
		// Sums OK ??
		printf ("Sums OK - %f %f %f\r\n", mean_S1, mean_S2, mean_S3);

		mean_S1 = mean_S1/i; 
		mean_S2 = mean_S2/i; 
		mean_S3 = mean_S3/i;

		// Sums OK 2??
		printf ("Sums OK 2 - %f %f %f\r\n", mean_S1, mean_S2, mean_S3);		

		L_S1 = mean_S1*alfa_S1+beta_S1; 
		L_S2 = mean_S2*alfa_S2+beta_S2; 
		L_S3 = mean_S3*alfa_S3+beta_S3;
		
		// Sums OK 3??
		printf ("Sums OK 3 - %f %f %f\r\n", L_S1, L_S2, L_S3);
		

		FILE *fp = fopen("/local/Results.txt", "a"); 
		fprintf(fp, "\n%f %f %f", L_S1, L_S2, L_S3); 
		fclose(fp); 
		
		// Set LED
		MyLED = 1;
		Printf ("Safe to remove device\r\n")
		
		wait (5.0);
		
		while(1) 
		{		
			MyLED2 = !MyLED2;
		} 
}

Enjoy Ceri.