Advertisement
Voha888

lights and music 8 keys 8 leds 10-11-2018 4-26

Nov 9th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.98 KB | None | 0 0
  1. const int pinKey1 = 3; // установим пины для кнопок
  2. const int pinKey2 = 4;
  3. const int pinKey3 = 5;
  4. const int pinKey4 = 6;
  5. const int pinKey5 = 7;
  6. const int pinKey6 = 8;
  7. const int pinKey7 = 9;
  8. const int pinKey8 = 10;
  9.  
  10. const int pinLed1 = 11; // установим пины для сетодиодов
  11. const int pinLed2 = 12;
  12. const int pinLed3 = 14;
  13. const int pinLed4 = 15;
  14. const int pinLed5 = 16;
  15. const int pinLed6 = 17;
  16. const int pinLed7 = 18;
  17. const int pinLed8 = 19;
  18.  
  19.  
  20. int modeWork = 1; // зададим переменные режимам работы
  21. const int modePiano = 1;
  22. const int modeTask = 2;
  23. const int modeGame = 3;
  24. const int modeLightForwardBack = 4;
  25. const int modeLightForwardWhenPressKey = 5;
  26. const int modeLightForwardBackwardFromKey = 6;
  27. const int modeLightUpDownWhenPressKey = 7;
  28.  
  29. int resetStep = 1 ; //переменная сброса шагов
  30. int currentStep = 1 ; // переменная номера шага алгоритма
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. void setup() {
  38.  
  39.  
  40.  // attachInterrupt(0, changeModeFunction, RISING); // прерывание на ножке 2 - кнопка смены режима
  41.   // put your setup code here, to run once:
  42.  
  43. pinMode(pinKey1, INPUT_PULLUP); //установим все пины кнопок на вход
  44. pinMode(pinKey2, INPUT_PULLUP);
  45. pinMode(pinKey3, INPUT_PULLUP);
  46. pinMode(pinKey4, INPUT_PULLUP);
  47. pinMode(pinKey5, INPUT_PULLUP);
  48. pinMode(pinKey6, INPUT_PULLUP);
  49. pinMode(pinKey7, INPUT_PULLUP);
  50. pinMode(pinKey8, INPUT_PULLUP);
  51. pinMode(pinLed1, OUTPUT); //установим все пины светодиодов на выход
  52. pinMode(pinLed2, OUTPUT);
  53. pinMode(pinLed3, OUTPUT);
  54. pinMode(pinLed4, OUTPUT);
  55. pinMode(pinLed5, OUTPUT);
  56. pinMode(pinLed6, OUTPUT);
  57. pinMode(pinLed7, OUTPUT);
  58. pinMode(pinLed8, OUTPUT);
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. }
  92.  
  93. void loop() {
  94.   // put your main code here, to run repeatedly:
  95. Serial.println("loop");
  96. if (resetStep == 1) { currentStep=1 ; resetStep=0; }
  97.  
  98.  
  99.  
  100. //switch (modeWork) {                                                // выполним функцию в зависимости от текущего режима работы
  101. //   case 1:  MusicMode(modePiano);  break;
  102. //  case 2:  MusicMode(modeTask); break;
  103. //  case 3:  MusicMode(modeGame);  break;
  104. //    case 4:  LightMode(modeLightForwardBack);  break;
  105.  //  case 5:  LightMode(modeLightForwardWhenPressKey); break;
  106. //   case 6:  LightMode(modeLightForwardBackwardFromKey); break;
  107. //   case 7:  LightMode(modeLightUpDownWhenPressKey);  break;    
  108. //  }
  109.  
  110.  
  111.  
  112. checkKeyFunction();
  113. delay(50);
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  currentStep++; //увеличим шаг на единицу
  142.  
  143. }
  144.  
  145. //тут мои функции:
  146.  
  147. void playingFunction(int pressedKey){   //функция получает номер нажатой кнопки и воспроизводит соответствующий сэмпл и включает соответствующий диод, и гасит остальные диоды
  148.  
  149.  
  150. switch(pressedKey) {
  151.     case pinKey1: digitalWrite(pinLed1, HIGH); digitalWrite(pinLed2, LOW); digitalWrite(pinLed3, LOW); digitalWrite(pinLed4, LOW); digitalWrite(pinLed5, LOW); digitalWrite(pinLed6, LOW); digitalWrite(pinLed7, LOW); digitalWrite(pinLed8, LOW); break;
  152.   case pinKey2: digitalWrite(pinLed1, LOW); digitalWrite(pinLed2, HIGH); digitalWrite(pinLed3, LOW); digitalWrite(pinLed4, LOW); digitalWrite(pinLed5, LOW); digitalWrite(pinLed6, LOW); digitalWrite(pinLed7, LOW);  digitalWrite(pinLed8, LOW); break;
  153.   case pinKey3: digitalWrite(pinLed1, LOW); digitalWrite(pinLed2, LOW); digitalWrite(pinLed3, HIGH); digitalWrite(pinLed4, LOW); digitalWrite(pinLed5, LOW); digitalWrite(pinLed6, LOW); digitalWrite(pinLed7, LOW);  digitalWrite(pinLed8, LOW); break;
  154.     case pinKey4: digitalWrite(pinLed1, LOW); digitalWrite(pinLed2, LOW); digitalWrite(pinLed3, LOW); digitalWrite(pinLed4, HIGH); digitalWrite(pinLed5, LOW); digitalWrite(pinLed6, LOW); digitalWrite(pinLed7, LOW);  digitalWrite(pinLed8, LOW); break;
  155.     case pinKey5: digitalWrite(pinLed1, LOW); digitalWrite(pinLed2, LOW); digitalWrite(pinLed3, LOW); digitalWrite(pinLed4, LOW); digitalWrite(pinLed5, HIGH); digitalWrite(pinLed6, LOW); digitalWrite(pinLed7, LOW);  digitalWrite(pinLed8, LOW); break;
  156.     case pinKey6: digitalWrite(pinLed1, LOW); digitalWrite(pinLed2, LOW); digitalWrite(pinLed3, LOW); digitalWrite(pinLed4, LOW); digitalWrite(pinLed5, LOW); digitalWrite(pinLed6, HIGH); digitalWrite(pinLed7, LOW);  digitalWrite(pinLed8, LOW); break;
  157.     case pinKey7: digitalWrite(pinLed1, LOW); digitalWrite(pinLed2, LOW); digitalWrite(pinLed3, LOW); digitalWrite(pinLed4, LOW); digitalWrite(pinLed5, LOW); digitalWrite(pinLed6, LOW); digitalWrite(pinLed7, HIGH);  digitalWrite(pinLed8, LOW); break;
  158.    case pinKey8: digitalWrite(pinLed1, LOW); digitalWrite(pinLed2, LOW); digitalWrite(pinLed3, LOW); digitalWrite(pinLed4, LOW); digitalWrite(pinLed5, LOW); digitalWrite(pinLed6, LOW); digitalWrite(pinLed7, LOW);  digitalWrite(pinLed8, HIGH); break;
  159.  }
  160.  
  161. return;
  162.  
  163. }
  164.  
  165.  
  166. void checkKeyFunction() // функция проверяет какая кнопка нажата и вызывает соответствующую нажатой кнопке функцию, если не одна - то тушит все диоды
  167. {
  168.  
  169.  if (digitalRead(pinKey1) ==LOW) playingFunction(pinKey1);
  170.  else if (digitalRead(pinKey2) == LOW) playingFunction(pinKey2);
  171.  else if (digitalRead(pinKey3) == LOW) playingFunction(pinKey3);
  172.  else if (digitalRead(pinKey4) == LOW) playingFunction(pinKey4);
  173.  else if (digitalRead(pinKey5) == LOW) playingFunction(pinKey5);
  174.  else if (digitalRead(pinKey6) == LOW) playingFunction(pinKey6);
  175.  else if (digitalRead(pinKey7) == LOW) playingFunction(pinKey7);
  176.  else if (digitalRead(pinKey8) == LOW) playingFunction(pinKey8);
  177. else { digitalWrite(pinLed1, LOW); digitalWrite(pinLed2, LOW); digitalWrite(pinLed3, LOW); digitalWrite(pinLed4, LOW); digitalWrite(pinLed5, LOW); digitalWrite(pinLed6, LOW); digitalWrite(pinLed7, LOW);  digitalWrite(pinLed8, LOW);}
  178.  
  179. return;
  180.  
  181. }
  182.  
  183.  
  184. int LightMode(int checkLightMode)
  185.  
  186. {
  187.   if (checkLightMode == modeLightForwardBack) {
  188.  
  189. switch (currentStep) {                                                // выполним функцию в зависимости от текущего режима работы
  190.     case 1:  digitalWrite(pinLed1, HIGH); digitalWrite(pinLed2, LOW); break;
  191.     case 2:  digitalWrite(pinLed2, HIGH); digitalWrite(pinLed1, LOW); break;
  192.     case 3:  digitalWrite(pinLed3, HIGH); digitalWrite(pinLed2, LOW); break;
  193.     case 4:  digitalWrite(pinLed4, HIGH); digitalWrite(pinLed3, LOW); break;
  194.     case 5:  digitalWrite(pinLed5, HIGH); digitalWrite(pinLed4, LOW); break;
  195.     case 6:  digitalWrite(pinLed6, HIGH); digitalWrite(pinLed5, LOW); break;
  196.     case 7:  digitalWrite(pinLed7, HIGH); digitalWrite(pinLed6, LOW); break;
  197.     case 8:  digitalWrite(pinLed8, HIGH); digitalWrite(pinLed7, LOW); break;
  198.     case 9:  digitalWrite(pinLed7, HIGH); digitalWrite(pinLed8, LOW); break;
  199.     case 10:  digitalWrite(pinLed6, HIGH); digitalWrite(pinLed7, LOW); break;
  200.     case 11:  digitalWrite(pinLed5, HIGH); digitalWrite(pinLed6, LOW); break;
  201.     case 12:  digitalWrite(pinLed4, HIGH); digitalWrite(pinLed5, LOW); break;
  202.     case 13:  digitalWrite(pinLed3, HIGH); digitalWrite(pinLed4, LOW); break;
  203.     case 14:  digitalWrite(pinLed2, HIGH); digitalWrite(pinLed3, LOW);  
  204.     default: currentStep =1;
  205.        
  206.   }
  207.  
  208.    
  209.   }
  210.  
  211.  
  212.  
  213.  
  214.  
  215. }
  216.  
  217.  
  218. int MusicMode(int checkLightMode){ delay(1); }
  219.  
  220.  
  221.  
  222. void changeModeFunction(){ //функция срабатывает по прерыванию и меняет режим на следующий
  223.  
  224.  modeWork++;
  225.  resetStep = 1 ;
  226. digitalWrite(pinLed1, LOW); digitalWrite(pinLed2, LOW); digitalWrite(pinLed3, LOW); digitalWrite(pinLed4, LOW); digitalWrite(pinLed5, LOW); digitalWrite(pinLed6, LOW); digitalWrite(pinLed7, LOW);  digitalWrite(pinLed8, LOW);
  227.  
  228.   if (modeWork>=8) modeWork =1;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement