code to drive the motors to the right position
Dependencies: HIDScope QEI mbed
Fork of BMT-K9_potmeter_fade by
main.cpp@2:6a4a2e355cd9, 2015-09-17 (annotated)
- Committer:
- ewoud
- Date:
- Thu Sep 17 08:38:02 2015 +0000
- Revision:
- 2:6a4a2e355cd9
- Parent:
- 1:f5b12280ea8a
- Child:
- 3:6a97e1d68511
2 motors pwm control by pot meter;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:e300738b9507 | 1 | #include "mbed.h" |
ewoud | 1:f5b12280ea8a | 2 | #include "HIDScope.h" |
ewoud | 1:f5b12280ea8a | 3 | |
vsluiter | 0:e300738b9507 | 4 | |
vsluiter | 0:e300738b9507 | 5 | // myled is an object of class PwmOut. It uses the LED_RED pin |
vsluiter | 0:e300738b9507 | 6 | // in human speech: myled is an output that can be controlled with PWM. LED_RED is the pin which is connected to the output |
ewoud | 2:6a4a2e355cd9 | 7 | PwmOut myled2(D5); |
ewoud | 2:6a4a2e355cd9 | 8 | PwmOut myled1(D6); |
ewoud | 2:6a4a2e355cd9 | 9 | |
ewoud | 2:6a4a2e355cd9 | 10 | //DigitalOut motor1(D4); |
vsluiter | 0:e300738b9507 | 11 | // pot is an object of class AnalogIn. It uses the PTB0 pin |
vsluiter | 0:e300738b9507 | 12 | // in human speech: pot is an analog input. You can read the voltage on pin PTB0 |
ewoud | 2:6a4a2e355cd9 | 13 | AnalogIn pot1(A0); |
ewoud | 2:6a4a2e355cd9 | 14 | AnalogIn pot2(A1); |
vsluiter | 0:e300738b9507 | 15 | |
ewoud | 1:f5b12280ea8a | 16 | //HIDScope scope(1); |
ewoud | 1:f5b12280ea8a | 17 | Serial pc(USBTX, USBRX); |
vsluiter | 0:e300738b9507 | 18 | |
vsluiter | 0:e300738b9507 | 19 | //start 'main' function. Should be done once in every C(++) program |
vsluiter | 0:e300738b9507 | 20 | int main() |
vsluiter | 0:e300738b9507 | 21 | { |
vsluiter | 0:e300738b9507 | 22 | //setup some stuff |
vsluiter | 0:e300738b9507 | 23 | //period of PWM signal is 10kHz. Every 100 microsecond a new PWM period is started |
ewoud | 1:f5b12280ea8a | 24 | myled1.period_ms(0.1); |
ewoud | 1:f5b12280ea8a | 25 | myled2.period_ms(0.1); |
ewoud | 2:6a4a2e355cd9 | 26 | //motor1=1; |
vsluiter | 0:e300738b9507 | 27 | //while 1 is unequal to zero. For humans: loop forever |
vsluiter | 0:e300738b9507 | 28 | while(1) { |
ewoud | 1:f5b12280ea8a | 29 | pc.printf("potvalue: %f \n",pot1.read()); |
vsluiter | 0:e300738b9507 | 30 | |
ewoud | 1:f5b12280ea8a | 31 | myled1.write(pot1.read()); |
ewoud | 1:f5b12280ea8a | 32 | myled2.write(pot2.read()); |
vsluiter | 0:e300738b9507 | 33 | //wait some time to give the LED output a few PWM cycles. Otherwise a new value is written before the previously set PWM period (of 100microseconds) is finished |
vsluiter | 0:e300738b9507 | 34 | //This loop executes at roughly 100Hz (1/0.01s) |
vsluiter | 0:e300738b9507 | 35 | wait(0.01); |
vsluiter | 0:e300738b9507 | 36 | } |
vsluiter | 0:e300738b9507 | 37 | } |
vsluiter | 0:e300738b9507 | 38 |