ese519
Dependencies: Adafruit-PWM-Servo-Driver mbed-rtos mbed
Fork of xlab_lights by
main.cpp@0:0d0dcca68fba, 2015-02-13 (annotated)
- Committer:
- sangmin92
- Date:
- Fri Feb 13 21:51:43 2015 +0000
- Revision:
- 0:0d0dcca68fba
- Child:
- 1:ae6c0b321908
first publish
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sangmin92 | 0:0d0dcca68fba | 1 | #include "mbed.h" |
sangmin92 | 0:0d0dcca68fba | 2 | #include "Adafruit_PWMServoDriver.h" |
sangmin92 | 0:0d0dcca68fba | 3 | |
sangmin92 | 0:0d0dcca68fba | 4 | #define STRONG 39 |
sangmin92 | 0:0d0dcca68fba | 5 | #define MEDIUM 22 |
sangmin92 | 0:0d0dcca68fba | 6 | #define WEAK 5 |
sangmin92 | 0:0d0dcca68fba | 7 | |
sangmin92 | 0:0d0dcca68fba | 8 | // Contains the color codes - Red, Green, Blue |
sangmin92 | 0:0d0dcca68fba | 9 | int purple[] = {204, 0, 255}; |
sangmin92 | 0:0d0dcca68fba | 10 | int orange[] = {255, 69, 0}; |
sangmin92 | 0:0d0dcca68fba | 11 | int pink[] = {255, 0, 255}; |
sangmin92 | 0:0d0dcca68fba | 12 | int blue[] = {0, 0, 255}; |
sangmin92 | 0:0d0dcca68fba | 13 | int black[] = {0, 0, 0}; |
sangmin92 | 0:0d0dcca68fba | 14 | int red[] = {255, 0, 0}; |
sangmin92 | 0:0d0dcca68fba | 15 | int green[] = {0, 255, 0}; |
sangmin92 | 0:0d0dcca68fba | 16 | |
sangmin92 | 0:0d0dcca68fba | 17 | Adafruit_PWMServoDriver pwm(p9, p10); |
sangmin92 | 0:0d0dcca68fba | 18 | |
sangmin92 | 0:0d0dcca68fba | 19 | void setServoPulse(uint8_t n, float pulse) { |
sangmin92 | 0:0d0dcca68fba | 20 | float pulselength = 10000; // 10,000 us per second |
sangmin92 | 0:0d0dcca68fba | 21 | pulse = 4094 * pulse / pulselength; |
sangmin92 | 0:0d0dcca68fba | 22 | pwm.setPWM(n, 0, pulse); |
sangmin92 | 0:0d0dcca68fba | 23 | } |
sangmin92 | 0:0d0dcca68fba | 24 | |
sangmin92 | 0:0d0dcca68fba | 25 | void setColor(int *color, int intensity, int duration){ |
sangmin92 | 0:0d0dcca68fba | 26 | setServoPulse(0, color[0] * intensity); |
sangmin92 | 0:0d0dcca68fba | 27 | setServoPulse(1, color[1] * intensity); |
sangmin92 | 0:0d0dcca68fba | 28 | setServoPulse(2, color[2] * intensity); |
sangmin92 | 0:0d0dcca68fba | 29 | wait_ms(duration); |
sangmin92 | 0:0d0dcca68fba | 30 | } |
sangmin92 | 0:0d0dcca68fba | 31 | |
sangmin92 | 0:0d0dcca68fba | 32 | void initServoDriver() { |
sangmin92 | 0:0d0dcca68fba | 33 | pwm.begin(); |
sangmin92 | 0:0d0dcca68fba | 34 | //pwm.setPWMFreq(100); //This dosen't work well because of uncertain clock speed. Use setPrescale(). |
sangmin92 | 0:0d0dcca68fba | 35 | pwm.setPrescale(64); //This value is decided for 10ms interval. |
sangmin92 | 0:0d0dcca68fba | 36 | pwm.setI2Cfreq(400000); //400kHz |
sangmin92 | 0:0d0dcca68fba | 37 | } |
sangmin92 | 0:0d0dcca68fba | 38 | |
sangmin92 | 0:0d0dcca68fba | 39 | int main() { |
sangmin92 | 0:0d0dcca68fba | 40 | //pwm.i2c_probe(); |
sangmin92 | 0:0d0dcca68fba | 41 | initServoDriver(); |
sangmin92 | 0:0d0dcca68fba | 42 | |
sangmin92 | 0:0d0dcca68fba | 43 | while(1){ |
sangmin92 | 0:0d0dcca68fba | 44 | |
sangmin92 | 0:0d0dcca68fba | 45 | // Test basic color patterns |
sangmin92 | 0:0d0dcca68fba | 46 | setColor(purple, STRONG, 1000); |
sangmin92 | 0:0d0dcca68fba | 47 | setColor(orange, STRONG, 1000); |
sangmin92 | 0:0d0dcca68fba | 48 | setColor(pink, STRONG, 1000); |
sangmin92 | 0:0d0dcca68fba | 49 | setColor(blue, STRONG, 1000); |
sangmin92 | 0:0d0dcca68fba | 50 | |
sangmin92 | 0:0d0dcca68fba | 51 | // Test blinking patterns |
sangmin92 | 0:0d0dcca68fba | 52 | for(int i = 0 ; i < 10 ; i++){ |
sangmin92 | 0:0d0dcca68fba | 53 | setColor(red, STRONG, 250); |
sangmin92 | 0:0d0dcca68fba | 54 | setColor(green, STRONG, 250); |
sangmin92 | 0:0d0dcca68fba | 55 | } |
sangmin92 | 0:0d0dcca68fba | 56 | |
sangmin92 | 0:0d0dcca68fba | 57 | // Test dimming |
sangmin92 | 0:0d0dcca68fba | 58 | for(int i = 10 ; i < 0 ; i++){ |
sangmin92 | 0:0d0dcca68fba | 59 | setColor(blue, 4 * i, 250); |
sangmin92 | 0:0d0dcca68fba | 60 | } |
sangmin92 | 0:0d0dcca68fba | 61 | |
sangmin92 | 0:0d0dcca68fba | 62 | } |
sangmin92 | 0:0d0dcca68fba | 63 | |
sangmin92 | 0:0d0dcca68fba | 64 | } |