HACKING 120% {Hacking, programmazione, computer & molto altro}

[arduino] lightmemory

« Older   Newer »
  Share  
view post Posted on 26/7/2014, 14:42     +1   +1   -1
Avatar

Water can take unforseen forms.

Group:
Founder
Posts:
5,273
Reputation:
+1,147
Location:
Shabang

Status:


Ho arduino da pressapoco cinque giorni, questo è il mio primo lavoro che si discosta dalla guida.

Lightmemory è basato su tre luci e tre pulsanti, quando si accende/resetta si accendono le tre luci in sequenza di cinque; dopo questo si dovranno premere i tre bottoni nella stessa sequenza delle luci, in caso di errore si dovrà ricominciare a premere.

CODICE
int count = 0;
int lim = 5;
int res[5];
int acc;
int accp = 1;

void light(int num = 0) {
   digitalWrite(3, LOW);
   digitalWrite(4, LOW);
   digitalWrite(5, LOW);
   digitalWrite(num, HIGH);
}

void right() {
   digitalWrite(3, HIGH);
   digitalWrite(4, HIGH);
   digitalWrite(5, HIGH);
   delay(1000);
   light();
}

void error() {
   light(3);
   delay(100);
   light(4);
   delay(100);
   light(5);
   delay(100);
   light();
}

void check(int numButton){
   if (res[count-6] == numButton-5){
       light(numButton-5);
       count++;
   } else {
       error();
       count = lim+1;
   }
}

void setup() {
   //Serial.begin(9600);
   pinMode(3, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(5, OUTPUT);
   pinMode(8, INPUT);
   pinMode(9, INPUT);
   pinMode(10, INPUT);
   randomSeed(analogRead(0));
}

void loop() {
   if (count < lim) {
       int acc = random(3, 6);
       if (acc != accp) {
           light(acc);
           res[count] = acc;
           accp = acc;
           count++;
           delay(500);
       }
   } else if (count == lim) {
       light();
       delay(500);
       error();
       delay(500);
       light();
       count++;
   } else if (count < 2*lim+1) {
       if (digitalRead(8)) {
           acc = 8;
           if (acc != accp){
                check(8);
           }
       }
       if (digitalRead(9)) {
           acc = 9;
           if (acc != accp){
               check(9);
           }
       }
       if (digitalRead(10)) {
           acc = 10;
           if (acc != accp){
               check(10);
           }
       }
       accp = acc;
   }
   else {
       right();
       delay(500);
       light();
       delay(500);
       right();
       delay(500);
       light();
       delay(50000);
   }
}



Nel tar ho caricato anche il .fzz del circuito, attenzione che le resistenze dei pulsanti non sono da 220, ma sono da 10k.

Download attachment
lightmemory.tar ( Number of downloads: 38 )

 
Web  Top
vogy
view post Posted on 27/7/2014, 10:10     +1   -1




Ehhhh ma io voglio anche la foto :O
 
Top
view post Posted on 27/7/2014, 11:44     +1   -1
Avatar

Water can take unforseen forms.

Group:
Founder
Posts:
5,273
Reputation:
+1,147
Location:
Shabang

Status:


L'ho già smontato :P
 
Web  Top
vogy
view post Posted on 27/7/2014, 17:48     +1   -1




if ( RottoLeBalle || GrandiProgettiInProgress ) {
printf "L'ho già smontato\n";
}
 
Top
view post Posted on 29/7/2014, 12:56     +1   -1
Avatar

Water can take unforseen forms.

Group:
Founder
Posts:
5,273
Reputation:
+1,147
Location:
Shabang

Status:


if ( RottoLeBalle || GrandiProgettiInProgress && loRimontoInCinqueSecondi ) {
printf "L'ho già smontato\n";
}[
 
Web  Top
view post Posted on 31/7/2014, 12:37     +1   -1
Avatar

Water can take unforseen forms.

Group:
Founder
Posts:
5,273
Reputation:
+1,147
Location:
Shabang

Status:


Ho aggiunto un buzzer sul pin 7 e un vibratore preso da un joystick di play 2 sull'11. Non ho ancora rivisitato il source perché non ho tempo, ma così funziona:

CODICE
int count = 0;
int lim = 5;
int res[5];
int acc;
int accp = 1;
int note[] = {261, 294, 330, 349, 392, 440, 494, 523};
int nowp;

void light(int num = 0) {
   digitalWrite(3, LOW);
   digitalWrite(4, LOW);
   digitalWrite(5, LOW);
   digitalWrite(num, HIGH);
}

void right() {
   digitalWrite(3, HIGH);
   digitalWrite(4, HIGH);
   digitalWrite(5, HIGH);
   delay(1000);
   light();
}

void start() {
   light(3);
   delay(100);
   light(4);
   delay(100);
   light(5);
   delay(100);
   light();
}

void error() {
   digitalWrite(11, 2);
   start();
   digitalWrite(11, LOW);
}

void check(int numButton) {
   if (res[count - 6] == numButton - 5) {
       noTone(7);
       tone(7, note[10 - count]);
       nowp = millis();
       light(numButton - 5);
       count++;
       accp = acc;
   } else {
       noTone(7);
       error();
       count = lim + 1;
       accp = 0;
   }
}

void setup() {
   //Serial.begin(9600);
   pinMode(3, OUTPUT);
   pinMode(4, OUTPUT);
   pinMode(5, OUTPUT);
   pinMode(8, INPUT);
   pinMode(9, INPUT);
   pinMode(10, INPUT);
   pinMode(11, OUTPUT);
   pinMode(7, OUTPUT);
   randomSeed(analogRead(0));
}

void loop() {
   if (count < lim) {
       tone(7, note[count]);
       int acc = random(3, 6);
       if (acc != accp) {
           light(acc);
           res[count] = acc;
           accp = acc;
           count++;
           delay(500);
           noTone(7);
       }
   } else if (count == lim) {
       tone(7, note[count]);
       light();
       delay(500);
       noTone(7);
       start();
       delay(500);
       light();
       count++;
   } else if (count < 2 * lim + 1) {
       if (millis() - nowp > 500) {
           noTone(7);
       }
       if (digitalRead(8)) {
           acc = 8;
           if (acc != accp) {
               check(8);
           }
       }
       if (digitalRead(9)) {
           acc = 9;
           if (acc != accp) {
               check(9);
           }
       }
       if (digitalRead(10)) {
           acc = 10;
           if (acc != accp) {
               check(10);
           }
       }
   } else {
       right();
       noTone(7);
       delay(500);
       light();
       delay(500);
       right();
       delay(500);
       light();
       delay(50000);
       digitalWrite(11, HIGH);
       delay(100);
       digitalWrite(11, LOW);
   }
}
 
Web  Top
5 replies since 26/7/2014, 14:42   81 views
  Share