m3pi timer and if statment

Hi,
Im using a m3pi robot with sharp IR sensors to implement an obstacle avoiding algorithm. Ideally i want to use a timer to see how long the robot will follow a particular wall for. As a result i am trying to use the timer function to help me do this. However, i cant seem to embed the timer section of the code properly within the obstacle avoiding program.
Any help would be appreciated.
The basic bit of code i am using so far is as follows :

#include "mbed.h"   
#include "m3pi.h"       // libraries

m3pi m3pi;
AnalogIn ir1(p20); // Front sensor
DigitalOut led(LED1);
AnalogIn ir2(p19); // Left sensor
DigitalOut led2(LED2);
AnalogIn ir3(p15); // right sensor
DigitalOut led3(LED3);

float x;  
 
float bat;
Timer t;
LocalFileSystem local("local");   



char buffer[4];
void battery() {
        
        bat=m3pi.battery();
        bat=bat*1000;
        sprintf (buffer, "%f", bat);
        m3pi.cls();
        m3pi.printf(buffer);
        wait(2);        
    
}

void forward_movement(){
       
        m3pi.cls();
        m3pi.locate(0,1);
        m3pi.printf("Moving");
        m3pi.forward(0.2);
         
        
        
void turn_left(){
        m3pi.cls();
        m3pi.printf("obstacle");
        m3pi.left(0.25);
        wait(0.3);
        }
        
        

 
        
int main(){
    battery();
    while(1){
        forward:
        x=ir1.read(); // front
        y=ir2.read(); // left
        z=ir3.read(); // right
        
      
       // t.reset();
      //  t.start();
        forward_movement();
            if  (x > 0.7000) {   
       //      t.stop(); 
        //    FILE *fp = fopen("/local/out.txt", "w");  // Open "out.txt" on the local file system for writing
        //  fprintf(fp, "RHS%f",t.read());
        //  fclose(fp);         
             turn_left();
          
          
                
            
          goto forward; 
                       
                }
        else{ 
        led=0;
        }
        
        }

    m3pi.cls();
    m3pi.locate(0,0);
   // m3pi.stop();
    m3pi.printf("Finished");           
            
        }

The program works fine without the use of timer, but when i include the timer sections the robot does not execute the if statement. does that mean the timer cant run simultaneously?

Regards
Dan

Hi, Dan.

The timer should be able to run asynchronously. Are you sure your code compiles at all? I don’t see a closing brace on your forward_movement function. Your code seems to be weirdly indented; perhaps that is making it hard to trace the control flow.

- Ryan

Hi Ryan,
thanks for the reply. Sorry i uploaded an edited bit of code which i didnt re check.
Here is the code i am using:

#include "mbed.h"   
#include "m3pi.h"       // libraries

m3pi m3pi;
AnalogIn ir1(p20); // Front sensor
DigitalOut led(LED1);
AnalogIn ir2(p19); // Left sensor
DigitalOut led2(LED2);
AnalogIn ir3(p15); // right sensor
DigitalOut led3(LED3);

float x;  
 
float bat;
Timer t;
LocalFileSystem local("local");   



char buffer[4];
void battery() {
        
        bat=m3pi.battery();
        bat=bat*1000;
        sprintf (buffer, "%f", bat);
        m3pi.cls();
        m3pi.printf(buffer);
        wait(2);        
    
}

void forward_movement(){
       
        m3pi.cls();
        m3pi.locate(0,1);
        m3pi.printf("Moving");
        m3pi.forward(0.2);
         
        
        
}


void turn_left(){
        m3pi.cls();
        m3pi.printf("obstacle");
        m3pi.left(0.25);
        wait(0.3);
        }
        
        

  
 
        
int main(){
    battery();
    while(1){
        
       x=ir1.read(); // front sensor
       
        
      
       t.reset();
       t.start();
       
      // forward_movement();
            if  (x > 0.7000) {   
                 
             turn_left();
             forward_movement();
            }
            else{ 
            led=0;
            }
            
        t.stop(); 
        FILE *fp = fopen("/local/out.txt", "w");  // Open "out.txt" on the local file system for writing
        fprintf(fp, "RHS%f",t.read());
        fclose(fp);   
        }

    m3pi.cls();
    m3pi.locate(0,0);
   // m3pi.stop();
    m3pi.printf("Finished");           
            
        }

In essence all i am trying to do is time how long the program is in a particular loop and write that recorded time to a local file. Unfortunately in this case the robot doesnt execute the if command after being tested so I am curious about the timer itself. Any suggestions on how to fix this or adapt it will be greatly appreciated.

Regards,
Dan

That code makes a lot more sense. I suggest simplifying your code until you know all the parts of it work by themselves. For example, have you made a program that just writes a constant text string to a local file? After that, try to write the value of a timer, without calling functions or using an if statement. I also noticed you have some code at the end of the main that will never run because your while loop is infinite. You should also be able to use the LEDs as an indicator of exactly what line your program is stuck on.

- Ryan