Advertisement
Guest User

miniSimon

a guest
Jul 22nd, 2021
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.77 KB | None | 0 0
  1.  
  2. #define BUZZER 10
  3. #define NOTE_A5  880
  4. #define NOTE_G5  784
  5. #define NOTE_B5  988
  6. #define NOTE_D6  1175
  7. #define NOTE_G6  1568
  8.  
  9. #define W_BUTTON 11
  10. #define Y_BUTTON 6
  11. #define R_BUTTON 7
  12. #define G_BUTTON 8
  13. #define B_BUTTON 9
  14.  
  15. #define PIN_BLU 5
  16. #define PIN_GREEN 4
  17. #define PIN_RED 3
  18. #define PIN_YELLOW 2
  19.  
  20. #define WHITE 64
  21. #define BLU 32
  22. #define GREEN 16
  23. #define RED 8
  24. #define YELLOW 4
  25.  
  26. int colors[5]={BLU,GREEN,RED,YELLOW,WHITE};
  27. int pin_led[4]={PIN_BLU,PIN_GREEN,PIN_RED,PIN_YELLOW};
  28. int pin_button[5]={B_BUTTON,G_BUTTON,R_BUTTON,Y_BUTTON,W_BUTTON};
  29. int fwave[5]={NOTE_G5,NOTE_B5,NOTE_D6,NOTE_G6,NOTE_A5};
  30.  
  31.  
  32. #define NULLA 0
  33. long Number;
  34.  
  35.  
  36.  
  37. #define MAXLEN 64
  38. #define BEGIN 0
  39. #define PLAY 1
  40. #define START 2
  41. int lenSequenza;
  42. int stato;
  43. int bottone;
  44. int Sequenza[MAXLEN];
  45. void AccendiLed(int colore)
  46. {
  47.    digitalWrite(pin_led[colore],HIGH);
  48. }
  49. void SpegniLed(int colore)
  50. {
  51.    digitalWrite(pin_led[colore],LOW);
  52. }
  53. void SuonaBip(int nota)
  54. {
  55.     tone(BUZZER, fwave[nota]);
  56. }
  57.  
  58. int LeggiPulsanti()
  59. {
  60.    int buttonState=0;
  61.    float Start = 0;        
  62.    float Time  = 0;  
  63.    Start = millis();
  64.    do{
  65.       if (digitalRead(Y_BUTTON)!=0) buttonState|=YELLOW;
  66.       if (digitalRead(R_BUTTON)!=0) buttonState|=RED;
  67.       if (digitalRead(G_BUTTON)!=0) buttonState|=GREEN;
  68.       if (digitalRead(B_BUTTON)!=0) buttonState|=BLU;
  69.       if (digitalRead(W_BUTTON)!=0) buttonState|=WHITE;
  70.       Time = millis();
  71.  
  72.    }while((Time - Start )<2000 && buttonState==0);
  73.    return buttonState;
  74. }
  75. int LeggiColore(int bottone)
  76. {
  77.   int colore=0;
  78.   if (bottone&BLU ) colore=0;
  79.   if (bottone&GREEN ) colore=1;
  80.   if (bottone&RED ) colore=2;
  81.   if (bottone&YELLOW ) colore=3;
  82.   if (bottone&WHITE ) colore=4;
  83.    
  84.   return colore;
  85. }
  86. void SequenzaStart()
  87. {
  88.   int i;
  89.   for(i=0;i<4;i++)
  90.   {
  91.       AccendiLed(i);
  92.   }
  93.   delay(300);
  94.   for(i=0;i<4;i++)
  95.   {
  96.       SpegniLed(i);
  97.   }
  98.   for(i=0;i<4;i++)
  99.   {
  100.       AccendiLed(i);
  101.       SuonaBip(i);
  102.       delay(500);
  103.       noTone(BUZZER);
  104.       SpegniLed(i);
  105.   }
  106.   for(i=0;i<4;i++)
  107.   {
  108.       AccendiLed(i);
  109.   }
  110.   delay(300);
  111.   for(i=0;i<4;i++)
  112.   {
  113.       SpegniLed(i);
  114.   }
  115.   delay(1000);
  116. }
  117. void setup() {
  118.    // put your setup code here, to run once:
  119.  
  120.    stato=BEGIN;
  121.    Serial.begin(9600);
  122.    pinMode(PIN_BLU, OUTPUT);
  123.    pinMode(PIN_GREEN, OUTPUT);
  124.    pinMode(PIN_RED, OUTPUT);
  125.    pinMode(PIN_YELLOW, OUTPUT);
  126.    pinMode(Y_BUTTON,  INPUT);
  127.    pinMode(R_BUTTON,  INPUT);
  128.    pinMode(G_BUTTON,  INPUT);
  129.    pinMode(B_BUTTON,  INPUT);
  130.    pinMode(W_BUTTON,  INPUT);
  131.    pinMode(BUZZER, OUTPUT);
  132.    randomSeed(analogRead(0));
  133.    SequenzaStart();
  134. }
  135.  
  136. void loop() {
  137.  
  138.    int i;
  139.    int colore;
  140.  
  141.  
  142.    if(stato==BEGIN){
  143.  
  144.       bottone = LeggiPulsanti();
  145.    
  146.       if (bottone&WHITE)
  147.       {
  148.           stato=PLAY;
  149.           SuonaBip(4);
  150.           delay(300);
  151.           noTone(BUZZER);
  152.       }
  153.    }
  154.    else{
  155.  
  156.      for (i=0;i<lenSequenza;i++){
  157.         AccendiLed(Sequenza[i]);
  158.         SuonaBip(Sequenza[i]);
  159.         delay(500);
  160.         noTone(BUZZER);
  161.         SpegniLed(Sequenza[i]);
  162.         delay(200);
  163.      }
  164.      for (i=0;i<lenSequenza;i++){
  165.         bottone = LeggiPulsanti();
  166.         if (bottone == 0){
  167.           stato=BEGIN;
  168.           break;  
  169.         }
  170.         colore = LeggiColore(bottone);
  171.         AccendiLed(colore);
  172.         SuonaBip(colore);
  173.         delay(500);
  174.         noTone(BUZZER);
  175.         SpegniLed(colore);
  176.         delay(200);
  177.         if(colore!=Sequenza[i]){
  178.           stato=BEGIN;
  179.           break;
  180.         }
  181.      }
  182.      if(stato==BEGIN){
  183.       lenSequenza=0;
  184.       SequenzaStart();
  185.      }
  186.      else{
  187.        Sequenza[lenSequenza]=(int)random(4);
  188.        lenSequenza++;
  189.        if (lenSequenza==MAXLEN) lenSequenza=0;
  190.        delay(1000);
  191.        stato=PLAY;
  192.      }
  193.   }
  194. }
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement