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