Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BUZZER 10
- #define NOTE_A5 880
- #define NOTE_G5 784
- #define NOTE_B5 988
- #define NOTE_D6 1175
- #define NOTE_G6 1568
- #define W_BUTTON 11
- #define Y_BUTTON 6
- #define R_BUTTON 7
- #define G_BUTTON 8
- #define B_BUTTON 9
- #define PIN_BLU 5
- #define PIN_GREEN 4
- #define PIN_RED 3
- #define PIN_YELLOW 2
- #define WHITE 64
- #define BLU 32
- #define GREEN 16
- #define RED 8
- #define YELLOW 4
- int colors[5]={BLU,GREEN,RED,YELLOW,WHITE};
- int pin_led[4]={PIN_BLU,PIN_GREEN,PIN_RED,PIN_YELLOW};
- int pin_button[5]={B_BUTTON,G_BUTTON,R_BUTTON,Y_BUTTON,W_BUTTON};
- int fwave[5]={NOTE_G5,NOTE_B5,NOTE_D6,NOTE_G6,NOTE_A5};
- #define NULLA 0
- long Number;
- #define MAXLEN 64
- #define BEGIN 0
- #define PLAY 1
- #define START 2
- int lenSequenza;
- int stato;
- int bottone;
- int Sequenza[MAXLEN];
- void AccendiLed(int colore)
- {
- digitalWrite(pin_led[colore],HIGH);
- }
- void SpegniLed(int colore)
- {
- digitalWrite(pin_led[colore],LOW);
- }
- void SuonaBip(int nota)
- {
- tone(BUZZER, fwave[nota]);
- }
- int LeggiPulsanti()
- {
- int buttonState=0;
- float Start = 0;
- float Time = 0;
- Start = millis();
- do{
- if (digitalRead(Y_BUTTON)!=0) buttonState|=YELLOW;
- if (digitalRead(R_BUTTON)!=0) buttonState|=RED;
- if (digitalRead(G_BUTTON)!=0) buttonState|=GREEN;
- if (digitalRead(B_BUTTON)!=0) buttonState|=BLU;
- if (digitalRead(W_BUTTON)!=0) buttonState|=WHITE;
- Time = millis();
- }while((Time - Start )<2000 && buttonState==0);
- return buttonState;
- }
- int LeggiColore(int bottone)
- {
- int colore=0;
- if (bottone&BLU ) colore=0;
- if (bottone&GREEN ) colore=1;
- if (bottone&RED ) colore=2;
- if (bottone&YELLOW ) colore=3;
- if (bottone&WHITE ) colore=4;
- return colore;
- }
- void SequenzaStart()
- {
- int i;
- for(i=0;i<4;i++)
- {
- AccendiLed(i);
- }
- delay(300);
- for(i=0;i<4;i++)
- {
- SpegniLed(i);
- }
- for(i=0;i<4;i++)
- {
- AccendiLed(i);
- SuonaBip(i);
- delay(500);
- noTone(BUZZER);
- SpegniLed(i);
- }
- for(i=0;i<4;i++)
- {
- AccendiLed(i);
- }
- delay(300);
- for(i=0;i<4;i++)
- {
- SpegniLed(i);
- }
- delay(1000);
- }
- void setup() {
- // put your setup code here, to run once:
- stato=BEGIN;
- Serial.begin(9600);
- pinMode(PIN_BLU, OUTPUT);
- pinMode(PIN_GREEN, OUTPUT);
- pinMode(PIN_RED, OUTPUT);
- pinMode(PIN_YELLOW, OUTPUT);
- pinMode(Y_BUTTON, INPUT);
- pinMode(R_BUTTON, INPUT);
- pinMode(G_BUTTON, INPUT);
- pinMode(B_BUTTON, INPUT);
- pinMode(W_BUTTON, INPUT);
- pinMode(BUZZER, OUTPUT);
- randomSeed(analogRead(0));
- SequenzaStart();
- }
- void loop() {
- int i;
- int colore;
- if(stato==BEGIN){
- bottone = LeggiPulsanti();
- if (bottone&WHITE)
- {
- stato=PLAY;
- SuonaBip(4);
- delay(300);
- noTone(BUZZER);
- }
- }
- else{
- for (i=0;i<lenSequenza;i++){
- AccendiLed(Sequenza[i]);
- SuonaBip(Sequenza[i]);
- delay(500);
- noTone(BUZZER);
- SpegniLed(Sequenza[i]);
- delay(200);
- }
- for (i=0;i<lenSequenza;i++){
- bottone = LeggiPulsanti();
- if (bottone == 0){
- stato=BEGIN;
- break;
- }
- colore = LeggiColore(bottone);
- AccendiLed(colore);
- SuonaBip(colore);
- delay(500);
- noTone(BUZZER);
- SpegniLed(colore);
- delay(200);
- if(colore!=Sequenza[i]){
- stato=BEGIN;
- break;
- }
- }
- if(stato==BEGIN){
- lenSequenza=0;
- SequenzaStart();
- }
- else{
- Sequenza[lenSequenza]=(int)random(4);
- lenSequenza++;
- if (lenSequenza==MAXLEN) lenSequenza=0;
- delay(1000);
- stato=PLAY;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement