Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- geovas
- Date:
- 2015-02-27
- Revision:
- 4:d58b9ad5d43d
- Parent:
- 3:6ec54d838b44
File content as of revision 4:d58b9ad5d43d:
#include "mbed.h" #include "echo_sensor.h" // echo sensor library #define LM1_ D10 #define LM2_ D9 #define RM1_ D5 #define RM2_ D6 #define RX_ PTC14 #define TX_ PTC15 PwmOut LM1(LM1_); // motors configuration PwmOut LM2(LM2_); PwmOut RM1(RM1_); PwmOut RM2(RM2_); Serial link(TX_, RX_); // bluetooth serial port //Serial link(USBTX, USBRX); // usb serial port void analogWrite(mbed::PwmOut *prt, char spd) // write to PWM { float a = spd / 256.0; // convert 0..256 to 0..1 *prt = a; } void GoForward(char spd) { analogWrite(&LM1,spd); analogWrite(&LM2,0); analogWrite(&RM1,spd); analogWrite(&RM2,0); } void GoBack(char spd) { analogWrite(&LM1,0); analogWrite(&LM2,spd); analogWrite(&RM1,0); analogWrite(&RM2,spd); } void TurnRight(char spd) { analogWrite(&LM1,spd); analogWrite(&LM2,0); analogWrite(&RM1,0); analogWrite(&RM2,spd); } void TurnLeft(char spd) { analogWrite(&LM1,0); analogWrite(&LM2,spd); analogWrite(&RM1,spd); analogWrite(&RM2,0); } int charToInt(char a, char b, char c) { int ans; if (a>'9' || b>'9' || c>'9') return 0; if (a<'0' || b<'0' || c<'0') return 0; ans = (a - 48)*100 + (b - 48)*10 + (c - 48); if (ans > 255) ans = 255; return ans; } char str[4]; void intToChar(int dat) { int d = dat; if (dat<100) str[0] = '0'; else { str[0] = d/100 + 48; d %= 100; } if (dat<10) str[1] = '0'; else { str[1] = d/10 + 48; d %= 10; } str[2] = d + 48; } char tolower(char ch) { if (ch >= 'a' && ch <= 'z') return ch; if (ch >= 'A' && ch <= 'Z') return ch - 'A' + 'a'; return 0; } int main(void) { char dat[5], b, i; link.baud(115200); initEcho(); // initialize echo sensor GoForward(0); // stop all motors while (!link.readable()); // trying to fount the starting byte while (link.getc() != 'Z'); while (true) { link.printf("Take a sequence\n\r"); for (i=0; i<5; i++) { while (!link.readable()); dat[i] = link.getc(); } b = tolower(dat[0]); int spd = charToInt(dat[1],dat[2],dat[3]); if (b == 'f') GoForward(spd); // movements if (b == 'b') GoBack(spd); if (b == 'r') TurnRight(spd); if (b == 'l') TurnLeft(spd); if (b == 'n') GoForward(0); if (b == 'e') // echo sensor { intToChar(getEcho()); link.printf(str); } //link.printf(dat); link.printf("%c%d ",b,spd); } }