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: Encoder HIDScope MODSERIAL mbed
Revision 2:1656c259189f, committed 2015-09-23
- Comitter:
- Vigilance88
- Date:
- Wed Sep 23 09:16:11 2015 +0000
- Parent:
- 1:e0c4625bbbab
- Commit message:
- using buttons on shield to run in a certain direction
Changed in this revision
| MODSERIAL.lib | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MODSERIAL.lib Wed Sep 23 09:16:11 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/Sissors/code/MODSERIAL/#6ffa97119f4f
--- a/main.cpp Sun Sep 20 16:31:03 2015 +0000
+++ b/main.cpp Wed Sep 23 09:16:11 2015 +0000
@@ -1,26 +1,67 @@
#include "mbed.h"
#include "encoder.h"
+#include "MODSERIAL.h"
-
-Encoder encoder1(D13,D12);
-DigitalOut motor2direction(D4); //D4 en D5 zijn motor 2 (op het motorshield)
-PwmOut motor2speed(D5);
-
-
-int main()
+volatile bool looptimerflag;
+
+void setlooptimerflag(void)
{
- int position = encoder1.getPosition();
- Serial pc(USBTX,USBRX);
+ looptimerflag = true;
+}
+
+int main(){
+ //VARIABLES
+ AnalogIn potmeter(A1);
+ AnalogIn potmeter2(A0);
+ MODSERIAL pc(USBTX,USBRX);
+ DigitalIn button(PTA1);
+ DigitalIn button1(PTB9);
+
+ Encoder motor1(D13,D12,true); // channel A and B from encoder, true - getSpeed works
+ PwmOut pwm_motor1(D6); // D4 and D5 = motor 2, D7 and D6 = motor 2
+ DigitalOut dir_motor1(D7); //
+
+ Encoder motor2(D10,D9,true); // channel A and B from encoder, true - getSpeed works
+ PwmOut pwm_motor2(D5); // D4 and D5 = motor 2, D7 and D6 = motor 2
+ DigitalOut dir_motor2(D4); //
+
+ // MOTOR1
+ float goal;
+ float pwm_to_motor;
+ // MOTOR2
+ float goal2;
+ float pwm_to_motor2;
+
+ //CODE
pc.baud(9600);
- motor2speed=0.0f;
- motor2direction=1;
-
- if(stop_knop.read() == 0) {
- motor2speed=0.0f;
- }
- while (true) {
+ //pwm_motor1.write(0.2f); // Speed
+ //dir_motor1.write(1); // Direction
+
+ Ticker looptimer;
+ looptimer.attach(setlooptimerflag,0.01);
+
+ while (1) {
+ while(looptimerflag != true);
+ looptimerflag = false;
- pc.printf("pos: %d, speed %f \r\n",encoder1.getPosition(), encoder1.getSpeed());
+ //MOTOR1
+ if (button.read() < 0.5) { //button pressed - direction
+ dir_motor2.write(1);
+ pwm_motor2.write(0.5);
+ pc.printf("position = %d \r\n", motor2.getPosition());
+ }
+ else if(button1.read() < 0.5) { //button pressed - other direction
+ dir_motor2.write(0);
+ pwm_motor2.write(0.5);
+ pc.printf("position = %d \r\n", motor2.getPosition());
+ }
+ else { //button not pressed
+ dir_motor2.write(0);
+ pwm_motor2.write(0);
+ pc.printf("position = %d \r\n", motor2.getPosition());
+ }
+
+ // pc.printf("pos: %d, speed %f \r\n",motor1.getPosition(), motor1.getSpeed());
}
}
\ No newline at end of file