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: HIDScope MODSERIAL mbed
Revision 4:275b6016f5c4, committed 2018-10-01
- Comitter:
- CasperK
- Date:
- Mon Oct 01 14:17:54 2018 +0000
- Parent:
- 3:f00ddd66fe39
- Commit message:
- Now with comments
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Sep 28 11:33:57 2018 +0000 +++ b/main.cpp Mon Oct 01 14:17:54 2018 +0000 @@ -2,6 +2,7 @@ #include "MODSERIAL.h" #include "HIDScope.h" +//create objects from K64f board PwmOut pwmpin(D6); PwmOut led(D10); AnalogIn potmeter(A5); @@ -9,11 +10,13 @@ DigitalOut directionpin(D7); MODSERIAL pc(USBTX, USBRX); -HIDScope scope(2); +//create other objects +HIDScope scope(2); //set two lines to be send to HIDScope Ticker ticker; -enum states{forward, stop, backwards}; +enum states{forward, stop, backwards}; //create possible different states states CurrentState; +//create variables volatile float x; volatile float y; volatile float scaled_potmeter; @@ -21,16 +24,16 @@ void sendData() { scope.set(0,potmeter); //set the potmeter data to the first scope - scope.set(1,x); + scope.set(1,x); //placeholder for second potmeter/motor scope.send(); //send the datapoints of the 2 motors } -void Proces_states(void){ +void Proces_states(void){ //execute the behaviour belonging to the different states switch (CurrentState){ case forward: directionpin = 1; - pwmpin.write(scaled_potmeter); //pwm of motor is potmeter value - led.write(scaled_potmeter); //led is potmeter value + pwmpin.write(scaled_potmeter); //pwm of motor is scaled potmeter value + led.write(scaled_potmeter); //led is scaled potmeter value break; case stop: // do nothing @@ -38,8 +41,8 @@ case backwards: directionpin = 0; c = scaled_potmeter*-1; - pwmpin.write(c); //pwm of motor is potmeter value - led.write(c); //led is potmeter value + pwmpin.write(c); //pwm of motor is negative of scaled potmeter value + led.write(c); //led is negative of scaled potmeter value break; } } @@ -54,22 +57,22 @@ while (true) { scaled_potmeter = (potmeter*2)-1; //scale potmeter from 0-1 to (-1 to 1) - if (scaled_potmeter > 0) { - CurrentState = forward; + if (scaled_potmeter > 0) { //if the potmeter is past the half-way point + CurrentState = forward; //set the state pc.printf("state = forward\r\n"); - Proces_states(); + Proces_states(); //execute the state } - if (scaled_potmeter == 0) { - CurrentState = stop; + if (scaled_potmeter == 0) { //if the potmeter is at the half-way point + CurrentState = stop; //set the state pc.printf("state = stop\r\n"); - Proces_states(); + Proces_states(); //execute the state } - if (scaled_potmeter < 0) { - CurrentState = backwards; + if (scaled_potmeter < 0) { //if the potmeter is before its half-way point + CurrentState = backwards; //set the state pc.printf("state = backwards\r\n"); - Proces_states(); + Proces_states(); //execute the state } - wait(0.2f); + wait(0.2f); //wait, so that the loop will repeat every 0.2 seconds (+interrupt times) } }