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.
Dependencies: mbed
Diff: main.cpp
- Revision:
- 0:8ee013fa99a5
- Child:
- 1:7198bf0747eb
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Tue Aug 27 19:09:49 2019 +0000
@@ -0,0 +1,107 @@
+#include "mbed.h"
+
+PwmOut pwm(p25);
+DigitalOut dir (p26);
+PwmOut pwm1(p24);
+DigitalOut dir1 (p23);
+DigitalIn button(p18);
+DigitalOut led(p20);
+
+Serial bt(p28, p27);
+
+InterruptIn A (p21);
+DigitalIn B (p22);
+
+Ticker END;
+
+int rightCount = 0;
+int lastCount = 0;
+
+
+void rightEncoderEvent (){
+ if (B.read() == 0){
+ rightCount++;
+ }
+
+ else{
+ rightCount--;
+ }
+}
+
+void Speed (void) {
+
+ int realSpeed = (rightCount - lastCount);
+ lastCount = rightCount;
+ bt.printf("\n\rCount: %d ## Speed: %d",rightCount,realSpeed);
+
+}
+
+int main() {
+
+ char ch;
+ int speed = 1;
+ bt.baud(9600);
+ A.rise(&rightEncoderEvent);
+
+ END.attach(&Speed, 1.0);
+
+ pwm.period(0.01f);
+ pwm1.period(0.01f);
+
+ while (button.read()) {led = 1; wait(0.2); led=0; wait(0.2); }
+
+ wait(2);
+
+ pwm.write(0.0f);
+ pwm1.write(0.0f);
+
+ while(1) {
+
+ if(bt.readable())
+ {
+ ch=bt.getc();
+
+ if (ch == 'l') { led = !led;}
+ if (ch == 'w') { dir = 0; dir1 = 0; pwm.write((0.2*speed)); pwm1.write((0.2*speed)); wait (0.02);}
+ if (ch == 's') { dir = 1; dir1 = 1; pwm.write((0.2*speed)); pwm1.write((0.2*speed)); wait(0.02);}
+ if (ch == 'a') { dir = 0; dir1 = 1; pwm.write((0.2*speed)); pwm1.write((0.2*speed)); wait(0.02);}
+ if (ch == 'd') { dir = 1; dir1 = 0; pwm.write((0.2*speed)); pwm1.write((0.2*speed)); wait(0.02);}
+ if (ch == 'r') {
+ bt.printf("\n\rChose speed: ");
+
+ while(bt.readable()) {}
+ ch = bt.getc();
+ speed = (int)ch - 48;
+ bt.printf("\n\rActual speed: %d\n",speed);}
+
+ //bt.printf("%c",ch);
+
+
+ }
+ //bt.printf("Count: ");
+ //bt.printf("%d",rightCount);
+ //bt.printf("\r\n");
+ /*
+ if (ch == 'S') { dir = 0; dir1 = 0; pwm.write(0.0f); pwm1.write(0.0f); }
+ if (ch == 'F') { dir = 0; dir1 = 0; pwm.write(0.2f); pwm1.write(0.2f); }
+ if (ch == 'G') { dir = 1; dir1 = 1; pwm.write(0.2f); pwm1.write(0.2f); }
+ if (ch == 'L') { dir = 0; dir1 = 1; pwm.write(0.2f); pwm1.write(0.2f); }
+ if (ch == 'R') { dir = 1; dir1 = 0; pwm.write(0.2f); pwm1.write(0.2f); }
+ */
+ pwm.write(0.0f);
+ pwm1.write(0.0f);
+
+
+ /*wait(0.5);
+ dir = 1;
+ dir1 = 1;
+ pwm.write(0.5f);
+ pwm1.write(0.5f);
+ wait(0.5);
+ dir = 0;
+ dir1 = 0;
+ pwm.write(0.5f);
+ wait(0.5);*/
+
+ }
+}