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: FastPWM mbed QEI biquadFilter HIDScope MODSERIAL
Diff: main.cpp
- Revision:
- 12:ef4ba26d0e2f
- Parent:
- 11:3efd6a324f16
diff -r 3efd6a324f16 -r ef4ba26d0e2f main.cpp --- a/main.cpp Mon Oct 08 15:10:47 2018 +0000 +++ b/main.cpp Fri Oct 12 12:17:08 2018 +0000 @@ -1,9 +1,9 @@ #include "mbed.h" #include "FastPWM.h" +#include "MODSERIAL.h" #include "QEI.h" // Includes library for encoder -Ticker motor; - +MODSERIAL pc(USBTX, USBRX); AnalogIn pot1(A1); AnalogIn pot2(A2); InterruptIn button1(D0); @@ -28,6 +28,7 @@ DigitalOut pin7(D7); // Motor 1 direction //float u1 = pot1; +Ticker motor; float u3 = 0.0; // Normalised variable for the movement of motor 3 void draaibuttons() @@ -36,32 +37,65 @@ simultaneously results for the turning speed of motor 3 in a slower movement, and eventually the motor will turn the other way around. */ - if (button1 == 1) - { if (button2 == 1) - { u3 = u3 + 0.1; //In stapjes van 0.1 + enum Rotatingdir3 {CCWrotating,CWrotating}; + Rotatingdir3 RotatingCurrent = CCWrotating; + bool RotatingChanged = true; + + switch (RotatingCurrent) + { case CCWrotating: + if (RotatingChanged) + { + RotatingChanged = false; + } + + // Code for rotating counterclockwise + if (button2 == 1) + { u3 = u3 + 0.1f; //In stapjes van 0.1 pin3 = fabs(u3); - if (u3>1.0) - { u3 = 1.0; + if (u3>1.0f) + { u3 = 1.0f; } } - } - else if (button1 == 0) - { if (button2 == 1) - { u3 = u3 - 0.1; + + // Transition to CWrotating if button is pressed + if (button1 == 1) + { + RotatingCurrent = CWrotating; + RotatingChanged = true; + pc.printf("The rotating direction for motor 3 is now counterclockwise"); + } + break; + + case CWrotating: + if (RotatingChanged) + { + RotatingChanged = false; + } + // Code for rotating clockwise + if (button2 == 1) + { u3 = u3 - 0.1f; pin3 = fabs(u3); - if (u3>1.0) - { u3 = 1.0; + if (u3>1.0f) + { u3 = 1.0f; } } + + // Transition to CWrotating if button is pressed + if (button1 == 1) + { RotatingCurrent = CCWrotating; + RotatingChanged = true; + pc.printf("The rotating direction for motor 3 is now clockwise"); + } + break; } } - -void draai() + +void draai() /* Function for the movement of all motors, using the potmeters for the moving direction and speed of motor 1 and 2, and using button 1 and 2 on the biorobotics shield for the moving direction and speed of motor 3. -*/ -{ +*/ +{ float u1 = 2.0*(pot1 - 0.5); // Normalised variable for the movement of motor 1 if (u1>0) { pin4 = true; @@ -93,7 +127,10 @@ int main() { + pc.baud(115200); + pin5.period(1.0/10000); + button1.rise(&draaibuttons); button2.rise(&draaibuttons); @@ -105,6 +142,7 @@ pin6.period_us(50); motor.attach(draai, 0.001); + while(true) { }