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

Dependencies:   mbed m3pi

Committer:
kanpapa
Date:
Wed Nov 23 14:26:08 2011 +0000
Revision:
1:97befd99f2b7
Parent:
0:1c32a3ae67e1
alpha version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kanpapa 0:1c32a3ae67e1 1 #include "mbed.h"
kanpapa 0:1c32a3ae67e1 2 #include "m3pi.h"
kanpapa 0:1c32a3ae67e1 3
kanpapa 0:1c32a3ae67e1 4 m3pi m3pi;
kanpapa 0:1c32a3ae67e1 5
kanpapa 0:1c32a3ae67e1 6 AnalogIn ain(p20);
kanpapa 0:1c32a3ae67e1 7 DigitalOut led1(LED1);
kanpapa 0:1c32a3ae67e1 8 DigitalOut led2(LED2);
kanpapa 0:1c32a3ae67e1 9 DigitalOut led3(LED3);
kanpapa 0:1c32a3ae67e1 10 DigitalOut led4(LED4);
kanpapa 0:1c32a3ae67e1 11
kanpapa 0:1c32a3ae67e1 12 int main() {
kanpapa 0:1c32a3ae67e1 13
kanpapa 0:1c32a3ae67e1 14 m3pi.locate(0,1);
kanpapa 0:1c32a3ae67e1 15 m3pi.printf("LO World");
kanpapa 0:1c32a3ae67e1 16
kanpapa 0:1c32a3ae67e1 17 wait(2.0);
kanpapa 0:1c32a3ae67e1 18
kanpapa 0:1c32a3ae67e1 19 for (int i = 0; i < 30; i++) {
kanpapa 0:1c32a3ae67e1 20 m3pi.forward(0.1);
kanpapa 0:1c32a3ae67e1 21 wait(0.5);
kanpapa 0:1c32a3ae67e1 22 //
kanpapa 0:1c32a3ae67e1 23 led1 = (ain > 0.2) ? 1 : 0;
kanpapa 0:1c32a3ae67e1 24 led2 = (ain > 0.4) ? 1 : 0;
kanpapa 0:1c32a3ae67e1 25 led3 = (ain > 0.6) ? 1 : 0;
kanpapa 0:1c32a3ae67e1 26 led4 = (ain > 0.8) ? 1 : 0;
kanpapa 0:1c32a3ae67e1 27
kanpapa 0:1c32a3ae67e1 28 if (ain > 0.8) {
kanpapa 0:1c32a3ae67e1 29 m3pi.backward(0.1);
kanpapa 0:1c32a3ae67e1 30 wait(0.5);
kanpapa 0:1c32a3ae67e1 31 m3pi.left(0.1);
kanpapa 0:1c32a3ae67e1 32 wait(0.5);
kanpapa 0:1c32a3ae67e1 33 }
kanpapa 0:1c32a3ae67e1 34 }
kanpapa 0:1c32a3ae67e1 35
kanpapa 0:1c32a3ae67e1 36 m3pi.stop();
kanpapa 0:1c32a3ae67e1 37 }