Информатика - решения школьных заданий

Предмет: Информатика, автор: karishamak15
Предмет: Информатика, автор: fen1r
Tinkercad СРОЧНОООО найдіть помилку в коді завдання було зробити кнопку рестарту гри (кнопка не працює) фото схеми нижче. Відповіді від GPT не потрібні. КОД: #define BUTTON_RESTART 13
#define LED_RED 11

#define LED_GREEN 9

#define LED_BLUE 7

#define LED_YELLOW 5

#define LED_CORRECT 4

#define LED_WRONG 2



#define BUTTON_RED 12

#define BUTTON_GREEN 10

#define BUTTON_BLUE 8

#define BUTTON_YELLOW 6



#define BUZZER 3

#define RED_TONE 220

#define GREEN_TONE 262

#define BLUE_TONE 330

#define YELLOW_TONE 392

#define TONE_DURATION 250



int restartButtonState = 0;

int GAME_SPEED = 250;

int GAME_STATUS = 0;

const int GAME_MAX_SEQUENCE = 50;

int GAME_SEQUENCE[GAME_MAX_SEQUENCE];

int GAME_STEP = 0;

int READ_STEP = 0;





void setup(){



pinMode(BUTTON_RESTART, INPUT_PULLUP);



Serial.begin(9500);



randomSeed(analogRead(0));



pinMode(LED_RED, OUTPUT);

pinMode(LED_GREEN, OUTPUT);

pinMode(LED_BLUE, OUTPUT);

pinMode(LED_YELLOW, OUTPUT);

pinMode(LED_CORRECT, OUTPUT);

pinMode(LED_WRONG, OUTPUT);



pinMode(BUTTON_RED, INPUT_PULLUP);

pinMode(BUTTON_GREEN, INPUT_PULLUP);

pinMode(BUTTON_BLUE, INPUT_PULLUP);

pinMode(BUTTON_YELLOW, INPUT_PULLUP);



pinMode(BUZZER, OUTPUT);





}



void loop() {

restartButtonState = digitalRead(BUTTON_RESTART);



switch (GAME_STATUS) {

case 0:

if (restartButtonState == HIGH) {

delay(100);

resetGame();

}

break;

case 1:

playSequence();

break;

case 2:

readSequence();

break;

case 3:

gameOver();

break;

case 4:

resetGame();

break;

}

}





void resetGame() {

GAME_STATUS = 0;

GAME_STEP = 0;

READ_STEP = 0;

GAME_SPEED = 250;



for (int i = 0; i < GAME_MAX_SEQUENCE; i++) {

GAME_SEQUENCE[i] = random(4) + 1;

}

GAME_STATUS = 1;

}









void playSequence(){

for(int i = 0; i <= GAME_STEP; i++){

Serial.print("Set LED");

Serial.println(GAME_SEQUENCE[i]);

delay(GAME_SPEED * 2);

setLED(GAME_SEQUENCE[i]);

playTone(GAME_SEQUENCE[i]);

delay(GAME_SPEED);

clearLEDs();

}

GAME_STATUS = 2;

}







void readSequence(){

int button_value = readButtons();



if(button_value > 0){

if(button_value == GAME_SEQUENCE[READ_STEP]){

setLED(button_value);

playTone(button_value);

digitalWrite(LED_CORRECT, HIGH);

delay(GAME_SPEED);

clearLEDs();

digitalWrite(LED_CORRECT, LOW);



if (GAME_SPEED > 100) {

GAME_SPEED = GAME_SPEED-15;

}



Serial.println("Correct!");



if(READ_STEP == GAME_STEP){

READ_STEP = 0;

GAME_STEP++;

GAME_STATUS = 1;

Serial.println("Next step");



setLEDs(true,true,true,true);

delay(GAME_SPEED);

setLEDs(false,false,false,false);



}else{

READ_STEP++;

}



delay(10);



}else{

GAME_STATUS = 3;

Serial.println("Game Over!");

}

}

delay(10);

}



void gameOver(){

digitalWrite(LED_WRONG, HIGH);

tone(BUZZER, 98, TONE_DURATION);

delay(TONE_DURATION);

tone(BUZZER, 93, TONE_DURATION);

delay(TONE_DURATION);

tone(BUZZER, 87, TONE_DURATION);

delay(TONE_DURATION);

delay(GAME_SPEED);

}



void setLED(int id){

switch(id){

case 0:

setLEDs(false,false,false,false);

break;

case 1:

setLEDs(true,false,false,false);

break;

case 2:

setLEDs(false,true,false,false);

break;

case 3:

setLEDs(false,false,true,false);

break;

case 4:

setLEDs(false,false,false,true);

break;

}

}



void playTone(int id){

switch(id){

case 0:

noTone(BUZZER);

break;

case 1:

tone(BUZZER, RED_TONE, TONE_DURATION);

break;

case 2:

tone(BUZZER, GREEN_TONE, TONE_DURATION);

break;

case 3:

tone(BUZZER, BLUE_TONE, TONE_DURATION);

break;

case 4:

tone(BUZZER, YELLOW_TONE, TONE_DURATION);

break;

}

}



void setLEDs(boolean red, boolean green, boolean blue, int yellow ){

if (red) digitalWrite(LED_RED, HIGH);

else digitalWrite(LED_RED, LOW);

if (green) digitalWrite(LED_GREEN, HIGH);

else digitalWrite(LED_GREEN, LOW);

if (blue) digitalWrite(LED_BLUE, HIGH);

else digitalWrite(LED_BLUE, LOW);

if (yellow) digitalWrite(LED_YELLOW, HIGH);

else digitalWrite(LED_YELLOW, LOW);

}



void clearLEDs(){

setLEDs(false,false,false,false);

}



int readButtons(void){

if (digitalRead(BUTTON_RED) == 0) return 1;

else if (digitalRead(BUTTON_GREEN) == 0) return 2;

else if (digitalRead(BUTTON_BLUE) == 0) return 3;

else if (digitalRead(BUTTON_YELLOW) == 0) return 4;



return 0;

}
Предмет: Информатика, автор: fen1r
tinkercad СРОЧНОООО найдіть помилку в коді завдання було зробити кнопку рестарту гри (кнопка не працює) фото схеми нижче. Відповіді від GPT не потрібні. КОД: #define BUTTON_RESTART 13



#define LED_RED 11

#define LED_GREEN 9

#define LED_BLUE 7

#define LED_YELLOW 5

#define LED_CORRECT 4

#define LED_WRONG 2



#define BUTTON_RED 12

#define BUTTON_GREEN 10

#define BUTTON_BLUE 8

#define BUTTON_YELLOW 6



#define BUZZER 3

#define RED_TONE 220

#define GREEN_TONE 262

#define BLUE_TONE 330

#define YELLOW_TONE 392

#define TONE_DURATION 250



int restartButtonState = 0;

int GAME_SPEED = 250;

int GAME_STATUS = 0;

const int GAME_MAX_SEQUENCE = 50;

int GAME_SEQUENCE[GAME_MAX_SEQUENCE];

int GAME_STEP = 0;

int READ_STEP = 0;





void setup(){



pinMode(BUTTON_RESTART, INPUT_PULLUP);



Serial.begin(9500);



randomSeed(analogRead(0));



pinMode(LED_RED, OUTPUT);

pinMode(LED_GREEN, OUTPUT);

pinMode(LED_BLUE, OUTPUT);

pinMode(LED_YELLOW, OUTPUT);

pinMode(LED_CORRECT, OUTPUT);

pinMode(LED_WRONG, OUTPUT);



pinMode(BUTTON_RED, INPUT_PULLUP);

pinMode(BUTTON_GREEN, INPUT_PULLUP);

pinMode(BUTTON_BLUE, INPUT_PULLUP);

pinMode(BUTTON_YELLOW, INPUT_PULLUP);



pinMode(BUZZER, OUTPUT);





}



void loop() {

restartButtonState = digitalRead(BUTTON_RESTART);



switch (GAME_STATUS) {

case 0:

if (restartButtonState == HIGH) {

delay(100);

resetGame();

}

break;

case 1:

playSequence();

break;

case 2:

readSequence();

break;

case 3:

gameOver();

break;

case 4:

resetGame();

break;

}

}





void resetGame() {

GAME_STATUS = 0;

GAME_STEP = 0;

READ_STEP = 0;

GAME_SPEED = 250;



for (int i = 0; i < GAME_MAX_SEQUENCE; i++) {

GAME_SEQUENCE[i] = random(4) + 1;

}

GAME_STATUS = 1;

}









void playSequence(){

for(int i = 0; i <= GAME_STEP; i++){

Serial.print("Set LED");

Serial.println(GAME_SEQUENCE[i]);

delay(GAME_SPEED * 2);

setLED(GAME_SEQUENCE[i]);

playTone(GAME_SEQUENCE[i]);

delay(GAME_SPEED);

clearLEDs();

}

GAME_STATUS = 2;

}







void readSequence(){

int button_value = readButtons();



if(button_value > 0){

if(button_value == GAME_SEQUENCE[READ_STEP]){

setLED(button_value);

playTone(button_value);

digitalWrite(LED_CORRECT, HIGH);

delay(GAME_SPEED);

clearLEDs();

digitalWrite(LED_CORRECT, LOW);



if (GAME_SPEED > 100) {

GAME_SPEED = GAME_SPEED-15;

}



Serial.println("Correct!");



if(READ_STEP == GAME_STEP){

READ_STEP = 0;

GAME_STEP++;

GAME_STATUS = 1;

Serial.println("Next step");



setLEDs(true,true,true,true);

delay(GAME_SPEED);

setLEDs(false,false,false,false);



}else{

READ_STEP++;

}



delay(10);



}else{

GAME_STATUS = 3;

Serial.println("Game Over!");

}

}

delay(10);

}



void gameOver(){

digitalWrite(LED_WRONG, HIGH);

tone(BUZZER, 98, TONE_DURATION);

delay(TONE_DURATION);

tone(BUZZER, 93, TONE_DURATION);

delay(TONE_DURATION);

tone(BUZZER, 87, TONE_DURATION);

delay(TONE_DURATION);

delay(GAME_SPEED);

}



void setLED(int id){

switch(id){

case 0:

setLEDs(false,false,false,false);

break;

case 1:

setLEDs(true,false,false,false);

break;

case 2:

setLEDs(false,true,false,false);

break;

case 3:

setLEDs(false,false,true,false);

break;

case 4:

setLEDs(false,false,false,true);

break;

}

}



void playTone(int id){

switch(id){

case 0:

noTone(BUZZER);

break;

case 1:

tone(BUZZER, RED_TONE, TONE_DURATION);

break;

case 2:

tone(BUZZER, GREEN_TONE, TONE_DURATION);

break;

case 3:

tone(BUZZER, BLUE_TONE, TONE_DURATION);

break;

case 4:

tone(BUZZER, YELLOW_TONE, TONE_DURATION);

break;

}

}



void setLEDs(boolean red, boolean green, boolean blue, int yellow ){

if (red) digitalWrite(LED_RED, HIGH);

else digitalWrite(LED_RED, LOW);

if (green) digitalWrite(LED_GREEN, HIGH);

else digitalWrite(LED_GREEN, LOW);

if (blue) digitalWrite(LED_BLUE, HIGH);

else digitalWrite(LED_BLUE, LOW);

if (yellow) digitalWrite(LED_YELLOW, HIGH);

else digitalWrite(LED_YELLOW, LOW);

}



void clearLEDs(){

setLEDs(false,false,false,false);

}



int readButtons(void){

if (digitalRead(BUTTON_RED) == 0) return 1;

else if (digitalRead(BUTTON_GREEN) == 0) return 2;

else if (digitalRead(BUTTON_BLUE) == 0) return 3;

else if (digitalRead(BUTTON_YELLOW) == 0) return 4;



return 0;

}
Предмет: Информатика, автор: maranaromanenko6
Предмет: Информатика, автор: maranaromanenko6
Предмет: Информатика, автор: maranaromanenko6