code to drive the motors to the right position
Dependencies: HIDScope QEI mbed
Fork of BMT-K9_potmeter_fade by
main.cpp@1:f5b12280ea8a, 2015-09-15 (annotated)
- Committer:
- ewoud
- Date:
- Tue Sep 15 17:10:27 2015 +0000
- Revision:
- 1:f5b12280ea8a
- Parent:
- 0:e300738b9507
- Child:
- 2:6a4a2e355cd9
pot meters controlling LED brightness;
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 | 1:f5b12280ea8a | 7 | PwmOut myled2(D9); |
ewoud | 1:f5b12280ea8a | 8 | PwmOut myled1(D7); |
vsluiter | 0:e300738b9507 | 9 | // pot is an object of class AnalogIn. It uses the PTB0 pin |
vsluiter | 0:e300738b9507 | 10 | // in human speech: pot is an analog input. You can read the voltage on pin PTB0 |
ewoud | 1:f5b12280ea8a | 11 | AnalogIn pot1(A1); |
ewoud | 1:f5b12280ea8a | 12 | AnalogIn pot2(A0); |
vsluiter | 0:e300738b9507 | 13 | |
ewoud | 1:f5b12280ea8a | 14 | //HIDScope scope(1); |
ewoud | 1:f5b12280ea8a | 15 | Serial pc(USBTX, USBRX); |
vsluiter | 0:e300738b9507 | 16 | |
vsluiter | 0:e300738b9507 | 17 | //start 'main' function. Should be done once in every C(++) program |
vsluiter | 0:e300738b9507 | 18 | int main() |
vsluiter | 0:e300738b9507 | 19 | { |
vsluiter | 0:e300738b9507 | 20 | //setup some stuff |
vsluiter | 0:e300738b9507 | 21 | //period of PWM signal is 10kHz. Every 100 microsecond a new PWM period is started |
ewoud | 1:f5b12280ea8a | 22 | myled1.period_ms(0.1); |
ewoud | 1:f5b12280ea8a | 23 | myled2.period_ms(0.1); |
vsluiter | 0:e300738b9507 | 24 | //while 1 is unequal to zero. For humans: loop forever |
vsluiter | 0:e300738b9507 | 25 | while(1) { |
ewoud | 1:f5b12280ea8a | 26 | pc.printf("potvalue: %f \n",pot1.read()); |
vsluiter | 0:e300738b9507 | 27 | |
ewoud | 1:f5b12280ea8a | 28 | myled1.write(pot1.read()); |
ewoud | 1:f5b12280ea8a | 29 | myled2.write(pot2.read()); |
vsluiter | 0:e300738b9507 | 30 | //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 | 31 | //This loop executes at roughly 100Hz (1/0.01s) |
vsluiter | 0:e300738b9507 | 32 | wait(0.01); |
vsluiter | 0:e300738b9507 | 33 | } |
vsluiter | 0:e300738b9507 | 34 | } |
vsluiter | 0:e300738b9507 | 35 |