MUstar

IoT 아두이노 0421 - LED_ex2

Apr 21st, 2017
82
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // LED control value
  2. #define OFF 0
  3. #define RED 0x01
  4. #define GREEN 0x02
  5. #define BLUE 0x04
  6.  
  7. int pin_GND = 11;  // ground pin
  8. int pin_LED[3] = {8, 9, 10};  // red, green, blue LED pin
  9.  
  10. void setup() {
  11.   // put your setup code here, to run once:
  12.   pinMode(pin_GND, OUTPUT);  // ground pin Output setup
  13.   pinMode(pin_LED[0], OUTPUT); // red LED pin Output setup
  14.   pinMode(pin_LED[1], OUTPUT); // green LED pin Output setup
  15.   pinMode(pin_LED[2], OUTPUT); // blue LED pin Output setup
  16.   digitalWrite(pin_GND, LOW);  // ground pin LOW Output
  17. }
  18.  
  19. void loop() {
  20.   // put your main code here, to run repeatedly:
  21.   LED_control(RED);   // red on
  22.   delay(1000);        // wait one second
  23.   LED_control(GREEN); // green on
  24.   delay(1000);        // wait one second
  25.   LED_control(BLUE);  // blue on
  26.   delay(1000);        // wait one second
  27. }
  28.  
  29. // RGB LED control function ('bxxxxxBGR' -> R-red control bit, G-greeb control bit, B-blue control bit)
  30. void LED_control(uint8_t da)
  31. {
  32.   digitalWrite(pin_LED[0], da & 0x01);  // red LED control
  33.   da >>= 1;                             // 1bit shift to the right
  34.   digitalWrite(pin_LED[1], da & 0x01);  // green LED control
  35.   da >>= 1;                             // 1bit shift to the right
  36.   digitalWrite(pin_LED[2], da & 0x01);  // blue LED control
  37. }
RAW Paste Data