2016-12-04 2 views
0

Мне нужно сделать проект для школы, но наш учитель не может нам помочь. Мне было интересно, если кто-то из вас захочет помочь мне с проблемой, которую я имею в своем проекте Arduino.Arduino Project Simon Says

Я хочу кнопку в моем проекте, которая запускает мою игру полностью, включая мою стартовую кнопку и т. Д. Теперь она начинается автоматически, и это не то, о чем говорит мое задание. Он должен начать все это, когда я нажимаю эту кнопку. В настоящее время у меня есть следующий код для моей игры.

Заранее спасибо

/*Simon Says 
*/ 
#include <Tone.h> //Librarie die ik zelf heb toegevoegd om de gebluiden van de buzzer/luidspreker beter te kunnen behandelen Tone speakerpin; int starttune[] = { NOTE_E5, NOTE_E5, 0, NOTE_E5, 0, NOTE_C5, NOTE_E5, 0, NOTE_G5, 0, 0, 0, NOTE_G4, 0, 0, 0, NOTE_C5, 0, 0, NOTE_G4, 0, 0, NOTE_E4, 0, 0, NOTE_A4, 0, NOTE_B4, 0, NOTE_AS4, NOTE_A4, 0, NOTE_G4, NOTE_E5, NOTE_G5, NOTE_A5, 0, NOTE_F5, NOTE_G5, 0, NOTE_E5, 0,NOTE_C5, NOTE_D5, NOTE_B4, 0, 0, NOTE_C5, 0, 0, NOTE_G4, 0, 0, NOTE_E4, 0, 0, NOTE_A4, 0, NOTE_B4, 0, NOTE_AS4, NOTE_A4, 0, NOTE_G4, NOTE_E5, NOTE_G5, NOTE_A5, 0, NOTE_F5, NOTE_G5, 0, NOTE_E5, 0,NOTE_C5, NOTE_D5, NOTE_B4, 0, 0 }; int duurToon2[] = { 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 

12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 

9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 

12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 

9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12 }; int note[] 
= {NOTE_C4, NOTE_C4, NOTE_G4, NOTE_C5, NOTE_G4, NOTE_C5}; int duurToon[] = {100, 100, 100, 300, 100, 300}; int button[] = {2, 3, 4, 5}; //The four button input pins int ledpin[] = {8, 9, 10, 11}; // LED pins int levelSpel = 0; // levelSpel counter int buttonstate = 0; // button state checker int randomArray[100]; //Intentionally long to store up to 100 inputs (doubtful anyone will get this far) int inputArray[100]; 

int score; int highscore; 

void setup() { Serial.begin(9600); speakerpin.begin(12); // speaker is on pin 12 


    for(int x=0; x<4; x++) // LED pins are outputs, Range altijd 1 optellen tot aan 4, elke noemer gaat men overlopen { 
    pinMode(ledpin[x], OUTPUT); } 
    for(int x=0; x<4; x++) { 
    pinMode(button[x], INPUT_PULLUP); // button pins are inputs 
    digitalWrite(button[x], HIGH); // enable internal pullup; buttons start in high position; logic reversed } 

    randomSeed(analogRead(0)); //Added to generate "more randomness" with the randomArray for the output function for (int thisNote = 0; thisNote < 13; thisNote ++) { 
    // play the next note: 
    speakerpin.play(starttune[thisNote]); 
    // hold the note: 
    if (thisNote==0 || thisNote==2 || thisNote==4 || thisNote== 6) 
    { 
     digitalWrite(ledpin[0], HIGH); 
    } 
    if (thisNote==1 || thisNote==3 || thisNote==5 || thisNote== 7 || thisNote==9 || thisNote==11) 
    { 
     digitalWrite(ledpin[1], HIGH); 
    } 
    if (thisNote==8 || thisNote==12) 
    { 
     digitalWrite(ledpin[2], HIGH); 
    } 
    if (thisNote==10) 
    { 
     digitalWrite(ledpin[3], HIGH); 
    } 
    delay(duurToon2[thisNote]*10); 
    // stop for the next note: 
    speakerpin.stop(); 
    digitalWrite(ledpin[0], LOW); 
    digitalWrite(ledpin[1], LOW); 
    digitalWrite(ledpin[2], LOW); 
    digitalWrite(ledpin[3], LOW); 
    delay(25); 
    } delay(1000); } void loop() {  
    for (int y=0; y<=99; y++) { 
    //function for generating the array to be matched by the player 
    digitalWrite(ledpin[0], HIGH); 
    digitalWrite(ledpin[1], HIGH); 
    digitalWrite(ledpin[2], HIGH); 
    digitalWrite(ledpin[3], HIGH); 

    for (int thisNote = 0; thisNote < 6; thisNote ++) { 
    // play the next note: 
    speakerpin.play(note[thisNote]); 
    // hold the note: 
    delay(duurToon[thisNote]); 
    // stop for the next note: 
    speakerpin.stop(); 
    delay(25); 
    } 

    digitalWrite(ledpin[0], LOW); 
    digitalWrite(ledpin[1], LOW); 
    digitalWrite(ledpin[2], LOW); 
    digitalWrite(ledpin[3], LOW); 
    delay(1000); 

    for (int y=levelSpel; y <= levelSpel; y++) 
    { //Limited by the levelSpel variable 
     int sensorValue = analogRead(A2); //ophalen waarde van POTmeter 
     int MaalPunten = (sensorValue/1024.0)*5; //Formule omzetting naar VOLT, dus uitkomst = spanning 
     MaalPunten += 1; // +1 toevoegen omdat spanning 0 kan zijn maar dan krijg je ook 0 punten 
     score = score + y * MaalPunten; //score wordt vermeerderd met het geslaadge level vermenigvuldigd met het aantal Volts 
     Serial.println(""); 
     Serial.println("----------------------------------------------"); 
     Serial.println("Puntengraad:"); 
     Serial.println(String(MaalPunten)); 
     Serial.println("----------------------------------------------"); 
     Serial.println("Huidige score: "); //Some serial output to follow along 
     Serial.println(score); 
     Serial.print("Highscore "); 
     Serial.println(highscore); 
     Serial.println("----------------------------------------------"); 
     Serial.print("Level "); 
     Serial.print(y+1); 
     Serial.println(""); 
     randomArray[y] = random(1, 5); //Assigning a random number (1-4) to the randomArray[y], y being the levelSpel count 
     for (int x=0; x <= levelSpel; x++) 
     { 


     for(int y=0; y<4; y++) 
     { 

      if (randomArray[x] == 1 && ledpin[y] == 8) 
      { //if statements to display the stored values in the array 
      digitalWrite(ledpin[y], HIGH); 
      speakerpin.play(NOTE_G3, 100); 
      delay(400); 
      digitalWrite(ledpin[y], LOW); 
      delay(100); 
      } 

      if (randomArray[x] == 2 && ledpin[y] == 9) 
      { 
      digitalWrite(ledpin[y], HIGH); 
      speakerpin.play(NOTE_A3, 100); 
      delay(400); 
      digitalWrite(ledpin[y], LOW); 
      delay(100); 
      } 

      if (randomArray[x] == 3 && ledpin[y] == 10) 
      { 
      digitalWrite(ledpin[y], HIGH); 
      speakerpin.play(NOTE_B3, 100); 
      delay(400); 
      digitalWrite(ledpin[y], LOW); 
      delay(100); 
      } 

      if (randomArray[x] == 4 && ledpin[y] == 11) 
      { 
      digitalWrite(ledpin[y], HIGH); 
      speakerpin.play(NOTE_C4, 100); 
      delay(400); 
      digitalWrite(ledpin[y], LOW); 
      delay(100); 
      } 
     } 
     } 
    } 
    input(); } } 

    void input() { //Function for allowing user input and checking input against the generated array 

    for (int x=0; x <= levelSpel;) { //Statement controlled by levelSpel count 

    for(int y=0; y<4; y++) 
    { 

     buttonstate = digitalRead(button[y]); 

     if (buttonstate == LOW && button[y] == 2) 
     { //Checking for button push 
     digitalWrite(ledpin[0], HIGH); 
     speakerpin.play(NOTE_G3, 100); 
     delay(200); 
     digitalWrite(ledpin[0], LOW); 
     inputArray[x] = 1; 
     delay(250); 
     Serial.print(" "); 
     Serial.print("Rood "); 
     if (inputArray[x] != randomArray[x]) { //Checks value input by user and checks it against 
      fail();        //the value in the same spot on the generated array 
     // Serial.print(randomArray[x]); 
     }          //The fail function is called if it does not match 
     x++; 
     } 
     if (buttonstate == LOW && button[y] == 3) 
     { 
     digitalWrite(ledpin[1], HIGH); 
     speakerpin.play(NOTE_A3, 100); 
     delay(200); 
     digitalWrite(ledpin[1], LOW); 
     inputArray[x] = 2; 
     delay(250); 
     Serial.print(" "); 
     Serial.print("Geel "); 
     if (inputArray[x] != randomArray[x]) { 
      fail(); 
     } 
     x++; 
     } 

     if (buttonstate == LOW && button[y] == 4) 
     { 
     digitalWrite(ledpin[2], HIGH); 
     speakerpin.play(NOTE_B3, 100); 
     delay(200); 
     digitalWrite(ledpin[2], LOW); 
     inputArray[x] = 3; 
     delay(250); 
     Serial.print(" "); 
     Serial.print("Groen "); 
     if (inputArray[x] != randomArray[x]) { 
      fail(); 
     } 
     x++; 
     } 

     if (buttonstate == LOW && button[y] == 5) 
     { 
     digitalWrite(ledpin[3], HIGH); 
     speakerpin.play(NOTE_C4, 100); 
     delay(200); 
     digitalWrite(ledpin[3], LOW); 
     inputArray[x] = 4; 
     delay(250); 
     Serial.print(" "); 
     Serial.print("Blauw "); 
     if (inputArray[x] != randomArray[x]) 
     { 
      fail(); 
     } 
     x++; 
     } 
    } } delay(500); levelSpel++; //Increments the levelSpel count, also the last action before starting the output function over again } 

void fail() { //Function used if the player fails to match the sequence 
    for (int y=0; y<=2; y++) { //Flashes lights for failure 

    digitalWrite(ledpin[0], HIGH); 
    digitalWrite(ledpin[1], HIGH); 
    digitalWrite(ledpin[2], HIGH); 
    digitalWrite(ledpin[3], HIGH); 
    speakerpin.play(NOTE_G3, 300); 
    delay(200); 
    digitalWrite(ledpin[0], LOW); 
    digitalWrite(ledpin[1], LOW); 
    digitalWrite(ledpin[2], LOW); 
    digitalWrite(ledpin[3], LOW); 
    speakerpin.play(NOTE_C3, 300); 
    delay(200); 

    if (score > highscore){ 
     highscore = score; 
     } } delay(500); levelSpel = -1; //Resets levelSpel value so the game starts over without need for a reset button } 

ответ

1

Прости, что я не читаю весь код, но почему бы не использовать только петлю, а в начале? что-то вроде этого:

int Push_Button = 2; 
void setup() { 
Serial.begin(9600); 
pinMode(Push_Button,INPUT); 
bool on = digitalRead(Push_Button); 
while(!on) 
{ 
    on = digitalRead(Push_Button); 
    delay(50); 
} 
... 
} 

я надеюсь, что я получить то, что вы хотите сделать ...

хороший день!

+0

Эй, спасибо за помощь, но это не сработает ... Если я нажму кнопку, ничего не получится ... – BeginnerArduino

+0

, если вы подключаете кнопку для подключения к одной стороне и к vdd с другой стороны? – YoavShtainer

+0

* Вы подключили кнопку для вывода 2 в одну сторону, а другую в vdd? – YoavShtainer

Смежные вопросы