A program that connects a distance sensor to the m3pi and avoids obstacles.

Dependencies:   mbed m3pi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "m3pi.h"
00003 
00004 m3pi m3pi;
00005 
00006 AnalogIn ain(p20);
00007 DigitalOut led1(LED1);
00008 DigitalOut led2(LED2);
00009 DigitalOut led3(LED3);
00010 DigitalOut led4(LED4);
00011 
00012 int main() {
00013 
00014     m3pi.locate(0,1);
00015     m3pi.printf("LO World");
00016 
00017     wait(2.0);
00018     
00019     for (int i = 0; i < 30; i++) {
00020         m3pi.forward(0.1);
00021         wait(0.5);
00022         //         
00023         led1 = (ain > 0.2) ? 1 : 0;
00024         led2 = (ain > 0.4) ? 1 : 0;
00025         led3 = (ain > 0.6) ? 1 : 0;
00026         led4 = (ain > 0.8) ? 1 : 0;
00027         
00028         if (ain > 0.8) {
00029             m3pi.backward(0.1);
00030             wait(0.5);
00031             m3pi.left(0.1);
00032             wait(0.5);
00033         }
00034     }
00035 
00036     m3pi.stop();       
00037 }