我必须为学校制作一个项目,但我们的老师无法帮助我们。我想知道你们其中一个人是否愿意帮助我解决我在Arduino项目中遇到的问题。
我想在我的项目中创建一个按钮来启动我的完整游戏,包括我的starttune等。现在它自动启动,这不是我的任务所说的。当我按下按钮时,它必须启动所有这一切。目前我的游戏自我有以下代码。
提前致谢
/*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 }
答案 0 :(得分:1)
抱歉,我没有阅读您的所有代码,但为什么不在开始时只使用一个while循环?像这样的东西:
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);
}
...
}
我希望我能得到你想做的......
美好的一天!