test program
Dependencies: LSM9DS1_Library2 MotorV2 SRF05 Tracker2 mbed nRF24L01P
Fork of DUMP_TRUCK_SPR2017 by
main.cpp@5:dc4cf6cc24b3, 2017-02-07 (annotated)
- Committer:
- simplyellow
- Date:
- Tue Feb 07 21:01:20 2017 +0000
- Revision:
- 5:dc4cf6cc24b3
- Parent:
- 0:6942f0e2198c
- Child:
- 10:cf77da9be0b8
Dump Truck
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Josahty | 0:6942f0e2198c | 1 | #include "mbed.h" |
Josahty | 0:6942f0e2198c | 2 | |
simplyellow | 5:dc4cf6cc24b3 | 3 | // Define buttons |
simplyellow | 5:dc4cf6cc24b3 | 4 | InterruptIn button_red(p5); |
simplyellow | 5:dc4cf6cc24b3 | 5 | InterruptIn button_green(p6); |
simplyellow | 5:dc4cf6cc24b3 | 6 | InterruptIn button_blue(p7); |
simplyellow | 5:dc4cf6cc24b3 | 7 | |
simplyellow | 5:dc4cf6cc24b3 | 8 | // Define LED colors |
simplyellow | 5:dc4cf6cc24b3 | 9 | PwmOut led_red(p21); |
simplyellow | 5:dc4cf6cc24b3 | 10 | PwmOut led_green(p22); |
simplyellow | 5:dc4cf6cc24b3 | 11 | PwmOut led_blue(p23); |
Josahty | 0:6942f0e2198c | 12 | |
simplyellow | 5:dc4cf6cc24b3 | 13 | // Interrupt Service Routine to increment the red color |
simplyellow | 5:dc4cf6cc24b3 | 14 | void inc_red() { |
Josahty | 0:6942f0e2198c | 15 | |
simplyellow | 5:dc4cf6cc24b3 | 16 | float pwm; |
simplyellow | 5:dc4cf6cc24b3 | 17 | |
simplyellow | 5:dc4cf6cc24b3 | 18 | // Read in current PWM value and increment it |
simplyellow | 5:dc4cf6cc24b3 | 19 | pwm = led_red.read(); |
simplyellow | 5:dc4cf6cc24b3 | 20 | pwm += 0.1f; |
simplyellow | 5:dc4cf6cc24b3 | 21 | if (pwm > 1.0f) { |
simplyellow | 5:dc4cf6cc24b3 | 22 | pwm = 0.0f; |
simplyellow | 5:dc4cf6cc24b3 | 23 | } |
simplyellow | 5:dc4cf6cc24b3 | 24 | led_red.write(pwm); |
Josahty | 0:6942f0e2198c | 25 | } |
Josahty | 0:6942f0e2198c | 26 | |
simplyellow | 5:dc4cf6cc24b3 | 27 | // Interrupt Service Routine to increment the green color |
simplyellow | 5:dc4cf6cc24b3 | 28 | void inc_green() { |
simplyellow | 5:dc4cf6cc24b3 | 29 | |
simplyellow | 5:dc4cf6cc24b3 | 30 | float pwm; |
simplyellow | 5:dc4cf6cc24b3 | 31 | |
simplyellow | 5:dc4cf6cc24b3 | 32 | // Read in current PWM value and increment it |
simplyellow | 5:dc4cf6cc24b3 | 33 | pwm = led_green.read(); |
simplyellow | 5:dc4cf6cc24b3 | 34 | pwm += 0.1f; |
simplyellow | 5:dc4cf6cc24b3 | 35 | if (pwm > 1.0f) { |
simplyellow | 5:dc4cf6cc24b3 | 36 | pwm = 0.0f; |
simplyellow | 5:dc4cf6cc24b3 | 37 | } |
simplyellow | 5:dc4cf6cc24b3 | 38 | led_green.write(pwm); |
simplyellow | 5:dc4cf6cc24b3 | 39 | } |
simplyellow | 5:dc4cf6cc24b3 | 40 | |
simplyellow | 5:dc4cf6cc24b3 | 41 | // Interrupt Service Routine to increment the blue color |
simplyellow | 5:dc4cf6cc24b3 | 42 | void inc_blue() { |
simplyellow | 5:dc4cf6cc24b3 | 43 | |
simplyellow | 5:dc4cf6cc24b3 | 44 | float pwm; |
simplyellow | 5:dc4cf6cc24b3 | 45 | |
simplyellow | 5:dc4cf6cc24b3 | 46 | // Read in current PWM value and increment it |
simplyellow | 5:dc4cf6cc24b3 | 47 | pwm = led_blue.read(); |
simplyellow | 5:dc4cf6cc24b3 | 48 | pwm += 0.1f; |
simplyellow | 5:dc4cf6cc24b3 | 49 | if (pwm > 1.0f) { |
simplyellow | 5:dc4cf6cc24b3 | 50 | pwm = 0.0f; |
simplyellow | 5:dc4cf6cc24b3 | 51 | } |
simplyellow | 5:dc4cf6cc24b3 | 52 | led_blue.write(pwm); |
simplyellow | 5:dc4cf6cc24b3 | 53 | } |
simplyellow | 5:dc4cf6cc24b3 | 54 | |
simplyellow | 5:dc4cf6cc24b3 | 55 | // Main loop |
simplyellow | 5:dc4cf6cc24b3 | 56 | int main() { |
simplyellow | 5:dc4cf6cc24b3 | 57 | |
simplyellow | 5:dc4cf6cc24b3 | 58 | // Initialize all LED colors as off |
simplyellow | 5:dc4cf6cc24b3 | 59 | led_red.write(0.0f); |
simplyellow | 5:dc4cf6cc24b3 | 60 | led_green.write(0.0f); |
simplyellow | 5:dc4cf6cc24b3 | 61 | led_blue.write(0.0f); |
simplyellow | 5:dc4cf6cc24b3 | 62 | |
simplyellow | 5:dc4cf6cc24b3 | 63 | // Define three interrupts - one for each color |
simplyellow | 5:dc4cf6cc24b3 | 64 | button_red.fall(&inc_red); |
simplyellow | 5:dc4cf6cc24b3 | 65 | button_green.fall(&inc_green); |
simplyellow | 5:dc4cf6cc24b3 | 66 | button_blue.fall(&inc_blue); |
simplyellow | 5:dc4cf6cc24b3 | 67 | |
simplyellow | 5:dc4cf6cc24b3 | 68 | // Do nothing! We wait for an interrupt to happen |
simplyellow | 5:dc4cf6cc24b3 | 69 | while(1) { |
simplyellow | 5:dc4cf6cc24b3 | 70 | } |
simplyellow | 5:dc4cf6cc24b3 | 71 | } |