code to drive the motors to the right position
Dependencies: HIDScope QEI mbed
Fork of BMT-K9_potmeter_fade by
main.cpp@0:e300738b9507, 2013-09-24 (annotated)
- Committer:
- vsluiter
- Date:
- Tue Sep 24 14:50:29 2013 +0000
- Revision:
- 0:e300738b9507
- Child:
- 1:f5b12280ea8a
Initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:e300738b9507 | 1 | #include "mbed.h" |
vsluiter | 0:e300738b9507 | 2 | |
vsluiter | 0:e300738b9507 | 3 | // myled is an object of class PwmOut. It uses the LED_RED pin |
vsluiter | 0:e300738b9507 | 4 | // in human speech: myled is an output that can be controlled with PWM. LED_RED is the pin which is connected to the output |
vsluiter | 0:e300738b9507 | 5 | PwmOut myled(LED_RED); |
vsluiter | 0:e300738b9507 | 6 | |
vsluiter | 0:e300738b9507 | 7 | // pot is an object of class AnalogIn. It uses the PTB0 pin |
vsluiter | 0:e300738b9507 | 8 | // in human speech: pot is an analog input. You can read the voltage on pin PTB0 |
vsluiter | 0:e300738b9507 | 9 | AnalogIn pot(PTB0); |
vsluiter | 0:e300738b9507 | 10 | |
vsluiter | 0:e300738b9507 | 11 | |
vsluiter | 0:e300738b9507 | 12 | //start 'main' function. Should be done once in every C(++) program |
vsluiter | 0:e300738b9507 | 13 | int main() |
vsluiter | 0:e300738b9507 | 14 | { |
vsluiter | 0:e300738b9507 | 15 | //setup some stuff |
vsluiter | 0:e300738b9507 | 16 | //period of PWM signal is 10kHz. Every 100 microsecond a new PWM period is started |
vsluiter | 0:e300738b9507 | 17 | myled.period_ms(0.1); |
vsluiter | 0:e300738b9507 | 18 | //while 1 is unequal to zero. For humans: loop forever |
vsluiter | 0:e300738b9507 | 19 | while(1) { |
vsluiter | 0:e300738b9507 | 20 | //Complicated stuff: |
vsluiter | 0:e300738b9507 | 21 | // pot.read() read the value of pot; this gives a value between 0 and 1 depending on the voltage on pin PTB0 |
vsluiter | 0:e300738b9507 | 22 | // this value is then put in myled.write() |
vsluiter | 0:e300738b9507 | 23 | // myled.write(x) -> write value x (between 0 and 1) to PwmOut myled. The duty cycle goes from 0% to 100%. |
vsluiter | 0:e300738b9507 | 24 | //Warning: Because the LED is full on when the output is completely low (0% duty cycle), the led is full on when the potmeter value is zero. |
vsluiter | 0:e300738b9507 | 25 | |
vsluiter | 0:e300738b9507 | 26 | //The code below could also be written as: |
vsluiter | 0:e300738b9507 | 27 | //myled = pot; |
vsluiter | 0:e300738b9507 | 28 | myled.write(pot.read()); |
vsluiter | 0:e300738b9507 | 29 | //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 | 30 | //This loop executes at roughly 100Hz (1/0.01s) |
vsluiter | 0:e300738b9507 | 31 | wait(0.01); |
vsluiter | 0:e300738b9507 | 32 | } |
vsluiter | 0:e300738b9507 | 33 | } |
vsluiter | 0:e300738b9507 | 34 |