Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 4 months ago.
I need to convert to the arduino c code?
- include "mbed.h"
- include "motordriver.h"
- include "TextLCD.h" DigitalOut myled(LED1); AnalogIn ain1(p20); AnalogIn ain2(p18); AnalogIn ain3(p19); TextLCD lcd(p15, p16, p17, p11, p12, p13); rs, e, d4-d7
Motor left(p21, p22, p23, 1); pwm, fwd, rev, has brake feature Motor right(p26, p25, p24, 1);
int main() {
float temp,temp2,temp3; while (1) { temp=100*(1- ain1); if(temp>40){ lcd.printf("distance=%f",temp); left.speed(0.4); right.speed(0.4); wait(0.5); left.stop(0.5); right.stop(0.5); wait(0.005); temp2=100*(1- ain2); temp3=100*(1- ain3); if(temp2<40){ lcd.printf("Take right turn"); left.speed(0.4); right.stop(1); wait(0.1); left.stop(1); right.stop(1); wait(0.1); } if(temp3<40) { lcd.printf("Take left turn"); left.stop(1); right.speed(0.4); wait(0.1); left.stop(1); right.stop(1); wait(0.1); } if(temp2<40 && temp3<40){ float s; s=(-1)*(0.5); lcd.printf("Take reverse turn"); left.speed(s); right.speed(s); wait(2); left.stop(1); right.stop(1); wait(0.5); } lcd.cls(); } else { temp2=100*(1- ain2); temp3=100*(1- ain3);
if(temp2<40 && temp3>=40){ lcd.printf("Take right turn"); left.speed(0.4); right.stop(1); wait(0.6); left.stop(1); right.stop(1); wait(0.5); }
if(temp2>=40 && temp3 <40){ lcd.printf("Take left turn"); left.stop(1); right.speed(0.4); wait(0.6); left.stop(1); right.stop(1); wait(0.5); } if(temp2 >40 && temp3 >40){ lcd.printf("Take right turn by default"); left.speed(0.4); right.stop(1); wait(0.6); left.stop(1); right.stop(1); wait(0.5); } } } }
1 Answer
10 years, 4 months ago.
cleaned up for proper viewing: well... almost
include "mbed.h" include "motordriver.h" include "TextLCD.h" DigitalOut myled(LED1); AnalogIn ain1(p20); AnalogIn ain2(p18); AnalogIn ain3(p19); TextLCD lcd(p15, p16, p17, p11, p12, p13); rs, e, d4-d7 Motor left(p21, p22, p23, 1); pwm, fwd, rev, has brake feature Motor right(p26, p25, p24, 1); int main() { float temp,temp2,temp3; while (1) { temp=100*(1- ain1); if(temp>40){ lcd.printf("distance=%f",temp); left.speed(0.4); right.speed(0.4); wait(0.5); left.stop(0.5); right.stop(0.5); wait(0.005); temp2=100*(1- ain2); temp3=100*(1- ain3); if(temp2<40){ lcd.printf("Take right turn"); left.speed(0.4); right.stop(1); wait(0.1); left.stop(1); right.stop(1); wait(0.1); } if(temp3<40) { lcd.printf("Take left turn"); left.stop(1); right.speed(0.4); wait(0.1); left.stop(1); right.stop(1); wait(0.1); } if(temp2<40 && temp3<40){ float s; s=(-1)*(0.5); lcd.printf("Take reverse turn"); left.speed(s); right.speed(s); wait(2); left.stop(1); right.stop(1); wait(0.5); } lcd.cls(); } else { temp2=100*(1- ain2); temp3=100*(1- ain3); if(temp2<40 && temp3>=40){ lcd.printf("Take right turn"); left.speed(0.4); right.stop(1); wait(0.6); left.stop(1); right.stop(1); wait(0.5); } if(temp2>=40 && temp3 <40){ lcd.printf("Take left turn"); left.stop(1); right.speed(0.4); wait(0.6); left.stop(1); right.stop(1); wait(0.5); } if(temp2 >40 && temp3 >40){ lcd.printf("Take right turn by default"); left.speed(0.4); right.stop(1); wait(0.6); left.stop(1); right.stop(1); wait(0.5); } } } }