MUstar

IoT 아두이노 0519 - 프로젝트 with 주석

May 19th, 2017
62
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Pin number declaration
  2. int pin_PIEZO = 12; //Buzz +
  3. int pin_LED[3] = {0,1,2}; //RGB LED +
  4. int pin_SW = 7; //Switch +
  5. int pin_GND1 = 3; //RGB LED -
  6. int pin_GND2 = 8; //Switch -
  7. int scale_data[9] = {-255,130,146,164,174,195,220,246,261}; //Buzz Soundnum
  8.  
  9. //Set Input and Output
  10. void setup(){
  11. pinMode(pin_PIEZO, OUTPUT); //Set to Buzz Output
  12. pinMode(pin_SW,INPUT_PULLUP); //Set to Switch Intput, LOW = ON , HIGH = OFF
  13. pinMode(pin_LED[0], OUTPUT); //Set to RED Output
  14. pinMode(pin_LED[1], OUTPUT); //Set to GREEN Output
  15. pinMode(pin_LED[2], OUTPUT); //Set to BLUE Output
  16. pinMode(pin_GND1, OUTPUT); digitalWrite(pin_GND1, LOW); // RGB LED - Set to Ground value
  17. pinMode(pin_GND2, OUTPUT); digitalWrite(pin_GND2, LOW); //Switch - Set to Ground value
  18. }
  19.  
  20. //Set RGB LED Control
  21. void LED_control(uint8_t da)
  22. {
  23.   digitalWrite(pin_LED[0], da & 0x01);
  24.   da >>= 1;
  25.   digitalWrite(pin_LED[1], da & 0x01);
  26.   da >>= 1;
  27.   digitalWrite(pin_LED[2], da & 0x01);
  28. }
  29.  
  30. //Set RGB LED - Color Name
  31. #define OFF 0
  32. #define RED 0x01
  33. #define GREEN 0x02
  34. #define BLUE 0x04
  35.  
  36. //Program Loop
  37. void loop(){
  38.   //digitalRead(pin_SW) -> Read Switch Digital Signal
  39.   //When pin_SW is turn ON.
  40.   if(digitalRead(pin_SW) == LOW)
  41.   {
  42.     //RGB LED In turn
  43.     LED_control(RED);delay(1000); LED_control(GREEN);delay(1000); LED_control(BLUE);delay(1000);LED_control(0);delay(1000);
  44.     //Song : Mary had a little lamb
  45.     //tone(pin_PIEZO, scale_date[num];dealay(sec);noTone(pin_PIEZO);delay(125);LED_control(num);
  46.     tone(pin_PIEZO, scale_data[3]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(RED);
  47.     tone(pin_PIEZO, scale_data[2]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(GREEN);
  48.     tone(pin_PIEZO, scale_data[1]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(BLUE);
  49.     tone(pin_PIEZO, scale_data[2]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(RED);
  50.     tone(pin_PIEZO, scale_data[3]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(GREEN);
  51.     tone(pin_PIEZO, scale_data[3]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(BLUE);
  52.     tone(pin_PIEZO, scale_data[3]);delay(250);noTone(pin_PIEZO);delay(250);LED_control(RED);
  53.     tone(pin_PIEZO, scale_data[2]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(GREEN);
  54.     tone(pin_PIEZO, scale_data[2]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(BLUE);
  55.     tone(pin_PIEZO, scale_data[2]);delay(250);noTone(pin_PIEZO);delay(250);LED_control(RED);
  56.     tone(pin_PIEZO, scale_data[3]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(GREEN);
  57.     tone(pin_PIEZO, scale_data[5]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(BLUE);
  58.     tone(pin_PIEZO, scale_data[5]);delay(250);noTone(pin_PIEZO);delay(250);LED_control(RED);
  59.     tone(pin_PIEZO, scale_data[3]);delay(187.5);noTone(pin_PIEZO);delay(187.5);LED_control(GREEN);
  60.     tone(pin_PIEZO, scale_data[2]);delay(62.5);noTone(pin_PIEZO);delay(62.5);LED_control(BLUE);
  61.     tone(pin_PIEZO, scale_data[1]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(RED);
  62.     tone(pin_PIEZO, scale_data[2]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(GREEN);
  63.     tone(pin_PIEZO, scale_data[3]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(BLUE);
  64.     tone(pin_PIEZO, scale_data[3]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(RED);
  65.     tone(pin_PIEZO, scale_data[3]);delay(250);noTone(pin_PIEZO);delay(250);LED_control(GREEN);
  66.     tone(pin_PIEZO, scale_data[2]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(BLUE);
  67.     tone(pin_PIEZO, scale_data[2]);delay(125);noTone(pin_PIEZO);delay(125);LED_control(RED);
  68.     tone(pin_PIEZO, scale_data[3]);delay(187.5);noTone(pin_PIEZO);delay(187.5);LED_control(GREEN);
  69.     tone(pin_PIEZO, scale_data[2]);delay(62.5);noTone(pin_PIEZO);delay(62.5); LED_control(BLUE);
  70.     tone(pin_PIEZO, scale_data[1]);delay(500);noTone(pin_PIEZO);delay(500);LED_control(0);
  71.   }
  72.   //When pin_SW is turn OFF.
  73.   else{
  74.     digitalWrite(pin_LED, LOW);
  75.     digitalWrite(pin_PIEZO, LOW);
  76.   }
  77. }
RAW Paste Data