![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Infrared sensor test
Dependencies: Chainable_RGB_LED mbed
main.cpp@0:b4d3e247e682, 2015-12-07 (annotated)
- Committer:
- yuanyi
- Date:
- Mon Dec 07 14:41:09 2015 +0000
- Revision:
- 0:b4d3e247e682
Infrared sensor;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yuanyi | 0:b4d3e247e682 | 1 | #include "mbed.h" |
yuanyi | 0:b4d3e247e682 | 2 | #include "ChainableLED.h" |
yuanyi | 0:b4d3e247e682 | 3 | |
yuanyi | 0:b4d3e247e682 | 4 | #define NUM_LED 1 |
yuanyi | 0:b4d3e247e682 | 5 | |
yuanyi | 0:b4d3e247e682 | 6 | DigitalOut myled(LED1); |
yuanyi | 0:b4d3e247e682 | 7 | DigitalIn TouchPin(D8); |
yuanyi | 0:b4d3e247e682 | 8 | ChainableLED led(D6,D7,NUM_LED); |
yuanyi | 0:b4d3e247e682 | 9 | InterruptIn motion(D2); |
yuanyi | 0:b4d3e247e682 | 10 | |
yuanyi | 0:b4d3e247e682 | 11 | int motion_detected = 0; |
yuanyi | 0:b4d3e247e682 | 12 | |
yuanyi | 0:b4d3e247e682 | 13 | void irq_handler(void) |
yuanyi | 0:b4d3e247e682 | 14 | { |
yuanyi | 0:b4d3e247e682 | 15 | motion_detected = 1; |
yuanyi | 0:b4d3e247e682 | 16 | } |
yuanyi | 0:b4d3e247e682 | 17 | |
yuanyi | 0:b4d3e247e682 | 18 | int main() { |
yuanyi | 0:b4d3e247e682 | 19 | uint8_t index = 0; |
yuanyi | 0:b4d3e247e682 | 20 | int cnt = 0; |
yuanyi | 0:b4d3e247e682 | 21 | motion.rise(&irq_handler); |
yuanyi | 0:b4d3e247e682 | 22 | while(1) { |
yuanyi | 0:b4d3e247e682 | 23 | |
yuanyi | 0:b4d3e247e682 | 24 | // myled = 1; // LED is ON |
yuanyi | 0:b4d3e247e682 | 25 | // wait(0.2); // 200 ms |
yuanyi | 0:b4d3e247e682 | 26 | // myled = 0; // LED is OFF |
yuanyi | 0:b4d3e247e682 | 27 | // wait(1.0); // 1 sec |
yuanyi | 0:b4d3e247e682 | 28 | int touch_value=TouchPin.read(); |
yuanyi | 0:b4d3e247e682 | 29 | |
yuanyi | 0:b4d3e247e682 | 30 | //if(touch_value==1){ |
yuanyi | 0:b4d3e247e682 | 31 | if(motion_detected){ |
yuanyi | 0:b4d3e247e682 | 32 | //led.setColorHSB(0,hue,1.0,0.5); |
yuanyi | 0:b4d3e247e682 | 33 | cnt++; |
yuanyi | 0:b4d3e247e682 | 34 | motion_detected=0; |
yuanyi | 0:b4d3e247e682 | 35 | printf("Hello! I've detected %d times since reset\n", cnt); |
yuanyi | 0:b4d3e247e682 | 36 | printf("\r\n"); |
yuanyi | 0:b4d3e247e682 | 37 | // generate random RGB values for LED's |
yuanyi | 0:b4d3e247e682 | 38 | uint8_t R = rand(); |
yuanyi | 0:b4d3e247e682 | 39 | uint8_t G = rand(); |
yuanyi | 0:b4d3e247e682 | 40 | uint8_t B = rand(); |
yuanyi | 0:b4d3e247e682 | 41 | |
yuanyi | 0:b4d3e247e682 | 42 | // print message to terminal |
yuanyi | 0:b4d3e247e682 | 43 | printf("index = %d, R=%x,G=%x,B=%x,",index,R,G,B); |
yuanyi | 0:b4d3e247e682 | 44 | printf("\r\n"); |
yuanyi | 0:b4d3e247e682 | 45 | // ChainableLED.setColorRGB(index_of_led, red, green, blue) |
yuanyi | 0:b4d3e247e682 | 46 | led.setColorRGB(index++, R, G, B); // increase brightness cascade down chain of LED's |
yuanyi | 0:b4d3e247e682 | 47 | index = ((index+1) % NUM_LED); // cycle through LED's |
yuanyi | 0:b4d3e247e682 | 48 | //wait(1); |
yuanyi | 0:b4d3e247e682 | 49 | } |
yuanyi | 0:b4d3e247e682 | 50 | } |
yuanyi | 0:b4d3e247e682 | 51 | } |