gpt4 book ai didi

更改 7 段计数器显示

转载 作者:太空宇宙 更新时间:2023-11-04 07:04:51 24 4
gpt4 key购买 nike

我使用我的 Arduino 和 ETS-7000 设备设计了一个 react 游戏,但我遇到了一个小问题。

游戏的想法是两个玩家互相对战,我使用了三个 LED,它们已经安装在 ETS-7000 上。

两个 7 段将从 9 和 9 开始,当第三个 LED 闪烁时,两个玩家应按下开关。

速度较慢的玩家将减少 1 分,因此他的 7 段将变为 8,游戏将继续进行,直到有人达到 0 并输掉。

我的问题是我做的每件事都是正确的,但不是速度较慢的玩家在他的 7 段上减少 1 分,而是速度较快的玩家。

我需要帮助所以我将代码调整为相反的方式

附言我对7段使用了BCD编码

  int SW2 = 3; //try
int SW1 = 4;
int LED0 = A0;
int LED1 = 12;
int LED2 = 13;
// int fault = 0;
//long wait = 0;
//long now = 0;
void setup(){
pinMode(A5, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(LED0, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(SW1, INPUT);
pinMode(SW2, INPUT);
//Initializing components
randomSeed(analogRead(1));
}
void loop(){
reset:
int n=9, x=9;
digitalWrite(A5, HIGH);
digitalWrite(A4, LOW);
digitalWrite(A3, LOW);
digitalWrite(A2, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
do{
digitalWrite(LED0, HIGH);
delay(2000);
digitalWrite(LED1, HIGH);
//Wait for a random period of time, between 3 seconds
//And 6 seconds. Get ready!
delay(random(500,8000));
digitalWrite(LED2, HIGH);
//Swing your swords as fast as you can, the faster one
//will be returned by getWinner()
int winner=getWinner();
//The yellow led by side of the winner will light up
if(winner==1){
if (n==1)
{
digitalWrite(A5, LOW);
digitalWrite(A4, LOW);
digitalWrite(A3, LOW);
digitalWrite(A2, LOW);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED0, HIGH);
delay(100);
digitalWrite(LED0, LOW);
delay(100);
digitalWrite(LED0, HIGH);
delay(100);
digitalWrite(LED0, LOW);
delay(100);
digitalWrite(LED0, HIGH);
delay(100);
digitalWrite(LED0, LOW);
delay(100);
digitalWrite(LED0, HIGH);
delay(100);
digitalWrite(LED0, LOW);
delay(100);
digitalWrite(LED0, HIGH);
delay(100);
digitalWrite(LED0, LOW);
delay(5000);
break;
}
else
{n=n-1;}
}else{ if (x==1)
{
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
delay(100);
digitalWrite(LED2, LOW);
delay(100);
digitalWrite(LED2, HIGH);
delay(100);
digitalWrite(LED2, LOW);
delay(100);
digitalWrite(LED2, HIGH);
delay(100);
digitalWrite(LED2, LOW);
delay(100);
digitalWrite(LED2, HIGH);
delay(100);
digitalWrite(LED2, LOW);
delay(100);
digitalWrite(LED2, HIGH);
delay(100);
digitalWrite(LED2, LOW);
delay(5000);
break;
}
else { x=x-1;}
}
if (n==0){
digitalWrite(A5, LOW);
digitalWrite(A4, LOW);
digitalWrite(A3, LOW);
digitalWrite(A2, LOW);
} else if (n==1){
digitalWrite(A5, HIGH);
digitalWrite(A4, LOW);
digitalWrite(A3, LOW);
digitalWrite(A2, LOW);
} else if (n==2){
digitalWrite(A5, LOW );
digitalWrite(A4, HIGH);
digitalWrite(A3, LOW);
digitalWrite(A2, LOW);
} else if (n==3){
digitalWrite(A5, HIGH);
digitalWrite(A4, HIGH);
digitalWrite(A3, LOW);
digitalWrite(A2, LOW);
} else if (n==4){
digitalWrite(A5, LOW);
digitalWrite(A4, LOW);
digitalWrite(A3, HIGH);
digitalWrite(A2, LOW);
} else if (n==5){
digitalWrite(A5, HIGH);
digitalWrite(A4, LOW);
digitalWrite(A3, HIGH);
digitalWrite(A2, LOW);
} else if (n==6) {
digitalWrite(A5, LOW);
digitalWrite(A4, HIGH);
digitalWrite(A3, HIGH);
digitalWrite(A2, LOW);
} else if (n==7){
digitalWrite(A5, HIGH);
digitalWrite(A4, HIGH);
digitalWrite(A3, HIGH);
digitalWrite(A2, LOW);
} else if (n==8){
digitalWrite(A5, LOW);
digitalWrite(A4, LOW);
digitalWrite(A3, LOW);
digitalWrite(A2, HIGH);
} else if (n==9){
digitalWrite(A5, HIGH);
digitalWrite(A4, LOW);
digitalWrite(A3, LOW);
digitalWrite(A2, HIGH);
}
if (x==0){
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
} else if (x==1){
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
} else if (x==2){
digitalWrite(8, LOW );
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
} else if (x==3){
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
} else if (x==4){
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
} else if (x==5){
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
} else if (x==6) {
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
} else if (x==7){
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
} else if (x==8){
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
} else if (x==9) {
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
}
digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
delay(2000);
} while(true);
goto reset;
}
//The function below waits for either of the tilter
//switch to be swang. The first one to swing
//will be returned by its number
int getWinner(){
do{
if(digitalRead(SW1)==HIGH){
return 1;
}else if(digitalRead(SW2)==HIGH){
return 2;
}
}while(true);
}

最佳答案

我会在连续的片段中给你一个更正的版本。

首先,引脚号应在顶部定义并赋予有意义的名称。如果所有内容都按玩家编号索引,这也会使代码变得非常简单。

#define PLAYER1 0
#define PLAYER2 1

const int ALERT_LED = 12;

// Pins of the BCD display for both players, most significant bit first
const int PLAYER_DISPLAY[2][4] = {
{ A2, A3, A4, A5 },
{ 11, 10, 9, 8 }
};

const int PLAYER_LED[2] = { A0, 13 };
const int PLAYER_SWITCH[2] = {4, 3 };

函数是你的 friend 。我们将在这里重新组合输出功能。

void set_display (int player, int value)
{
for (int i = 0; i < 4; i++) {
digitalWrite (PLAYER_DISPLAY[player][3 - i], value & (1 << i));
}
}

void blink_led (int pin)
{
for (int i = 0; i < 5; i++) {
digitalWrite (pin, HIGH);
delay(100);
digitalWrite (pin, LOW);
delay(100);
}
}

我们对输入功能做同样的事情。

// return the number of the first player to press its switch
int get_winner ()
{
while (true) {
for (int i = 0; i < 2; i++) {
if (digitalRead(PLAYER_SWITCH[i]) == HIGH) {
return i;
}
}
}
}

我们设置引脚方向。

void setup ()
{
// set pins direction and ensure the leds are off
pinMode (ALERT_LED, OUTPUT);
digitalWrite (ALERT_LED, LOW);

for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
pinMode (PLAYER_DISPLAY[i][j], OUTPUT);
}
pinMode (PLAYER_LED[i], OUTPUT);
digitalWrite (PLAYER_LED[i], LOW);
pinMode (PLAYER_SWITCH[i], INPUT);
}

// initialize random number generator
randomSeed(analogRead(1));
}

现在,由于我们的准备工作,主循环变得更简单,其逻辑清晰明了,更容易更改,也更容易发现错误。

void loop()
{
int score[2] = { 9, 9 };

for (int i = 0; i < 2; i++) {
set_display (i, score[i]);
}

while (true) {
// turn on the leds in sequence
digitalWrite(PLAYER_LED[PLAYER1], HIGH);
delay(2000);
digitalWrite(PLAYER_LED[PLAYER2], HIGH);
delay(random(500,8000));
digitalWrite(ALERT_LED, HIGH);

// get winner
int winner = get_winner();
int loser = (winner == PLAYER1) ? PLAYER2 : PLAYER1;

// update score
score[loser]--;
set_display (loser, score[loser]);

// turn the leds off
digitalWrite(PLAYER_LED[PLAYER1], LOW);
digitalWrite(PLAYER_LED[PLAYER2], LOW);
digitalWrite(ALERT_LED, LOW);

// if the game is over, blink the winner's led and restart
if (score[loser] == 0) {
blink_led (PLAYER_LED[winner]);
delay(5000);
break;
}

delay(2000);
}
}

我希望您能从这些原则中学习。如果您不了解所有内容(尤其是 set_display() 的工作原理)也没关系。

关于更改 7 段计数器显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33959861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com