error 127

15 Jul 2010

Hi,

I'm not well versed in C, I got some help sticking a few programs together to serve a basic purpose, and I've been modifying it to further suit my needs.

Here is the code:

#include "mbed.h"

#define ON   1
#define OFF  0

LocalFileSystem local("local");
AnalogIn myinput(p20);
DigitalOut myled(p5);
DigitalOut led1(LED1);
DigitalOut led2(LED2);

float analog_value;
int count = 0;

int main() {
    printf("Read and store 100 analog sample at 0.5 sec interval\r\n");
    
    FILE *fp = fopen("/local/analog.txt","w");  // Open a file to save samples
    printf("File analog.txt is open.\r\n");
    
 analog_value = myinput.read();         // Read analog (range 0.0 to 1.0)    
 if (analog_value > 0.6)                        // If value is above 0.3, then store 
    {   
    while (count < 100) {
        fprintf (fp, "%3i: %f \r\n", count+1, analog_value);
        led1 = ON;                             // Turn LED ON if sample was stored.
        printf ("%3i: %f\r\n", count+1, analog_value);
        count++;           // Increment sample counter.
        led2 = ON;         // LED2 ON.
        wait_ms(50);       // Wait 0.05 second
        led2 = OFF;        // LED2 OFF.
        wait_ms(50);       // Wait 0.05 second
        fclose(fp);        // Close the sampe file after 100 samples read.
        printf("File analog.txt closed.\r\n");
        printf("Finished. 100 samples taken. Bye!\r\n");
        } 
  else {
        fprintf (fp, "%3i: %f [below]\r\n", count+1, analog_value);
        led2 = ON;
        wait_ms(50);
        led2 = Off;
        }    
    }        
}
Not my work for the most part (thanks D. Wendelboe)

Basically I want this to now evaluate a measured voltage and record the values over the next short length of time, IF the values is over a limit, other wise to continue to poll the value.

However, when compiling I get an error "E127" on line 50 (the line with else) along with "expected a statement".

Ultimately I'd like the output file to have be named the time at which the initial value crossed the threshold, any hint with that would be great (the RTC on mbed seems kind of difficult to maneuver).

15 Jul 2010 . Edited: 15 Jul 2010

I think the number of your braces is wrong.

Try this:

 

int main() {
    printf("Read and store 100 analog sample at 0.5 sec interval\r\n");
    
    FILE *fp = fopen("/local/analog.txt","w");  // Open a file to save samples
    printf("File analog.txt is open.\r\n");
    
 analog_value = myinput.read();         // Read analog (range 0.0 to 1.0)    
 if (analog_value > 0.6)                        // If value is above 0.3, then store 
    {   
        while (count < 100) {
          fprintf (fp, "%3i: %f \r\n", count+1, analog_value);
          led1 = ON;                             // Turn LED ON if sample was stored.
          printf ("%3i: %f\r\n", count+1, analog_value);
          count++;           // Increment sample counter.
          led2 = ON;         // LED2 ON.
          wait_ms(50);       // Wait 0.05 second
          led2 = OFF;        // LED2 OFF.
          wait_ms(50);       // Wait 0.05 second
          fclose(fp);        // Close the sampe file after 100 samples read.
          printf("File analog.txt closed.\r\n");
          printf("Finished. 100 samples taken. Bye!\r\n");
        } 
    }
  else {
        fprintf (fp, "%3i: %f [below]\r\n", count+1, analog_value);
        led2 = ON;
        wait_ms(50);
        led2 = Off;
        }    
         
}
16 Jul 2010

Hi again, I've made some significant changes to the code, the change suggested above has been succesful, however, I have the same error code on the else line again, my braces look correct to me, any suggestions?

 

int main() {
    printf("Read and store 100 analog sample at 0.5 sec interval\r\n");
    
    FILE *fp = fopen("/local/analog.txt","w");    // Open a file to save samples
    printf("File analog.txt is open.\r\n");

    REPEAT
         analog_value = myinput.read();
         switch_value = kill.read();
             if (analog_value < TV);
             { 
                   led2 = ON;
                   wait_ms(2*SR);           // sampling rate
                   led2 = OFF;
              } else {
                    while (count < SD) {   // sampling duration
                        analog_value = myinput.read();
                        fprintf (fp, "%3i: %f \r\n", count+1, analog_value);
                        led1 = ON;
                        myled = ON;        // Turn LED ON if sample was stored.
                        printf ("%3i: %f\r\n", count+1, analog_value);
                        count++;           // Increment sample counter.
                        led2 = ON;         // LED2 ON.
                        wait_ms(SR);       // Wait x second(s)(sampling rate)
                        led2 = OFF;        // LED2 OFF.
                        wait_ms(SR);                 
                                        }
                        led1 = OFF;
                        myled = OFF;
                     }
    UNTIL (switch_value > 0.6)
        fclose(fp);   
}

16 Jul 2010

on the line

"if (analog_value < TV);" remove the ";" so it reads "if (analog_value < TV)"