弹球目标 - 不会重置

时间:2016-07-10 23:10:55

标签: button arduino

我是艺术家,而不是程序员。但我正在和Arduino一起制作弹球游戏,而且我没有程序员,所以我正在学习如何使这项工作成功。

我正在尝试编写执行此操作的代码: - 3个目标(按钮),当它们被击中时,它们会给出点,并点亮LED。 - 如果再次按下相同的按钮,它仍会给出分数,并且LED保持点亮状态。 - 如果所有3个灯同时点亮,则会给出奖励分数,并且所有3个灯都会熄灭。

我尝试使用Arduino论坛,但我能从中获得的最好建议是#34;去学习编码。"

我被告知要了解阵列'而且我已经尝试了但是我在缠绕它时遇到了一些严重的麻烦。如果可以只使用' IF'和' ELSE'陈述我真的更喜欢用漫长而低效的方式做事。

这是我目前的代码。目前我的面包板上有4个灯。出于调试原因,灯#4(D)立即点亮。按下按钮时,其他3个指示灯(ABC)中的每一个都会亮起。全部3个指示灯点亮时,(D)指示灯熄灭,但(ABC)保持点亮。我需要他们出去(D)。而且我不知道为什么他们不会。我知道代码识别出所有3都是HIGH。因为(D)变低。我不知道为什么(ABC)也会失败。

// 3 TARGET SCORE FUNCTION. 
// WHEN A TARGET IS HIT IT LIGHTS UP AND PROVIDES POINTS.
// IF A TARGET IS HIT AGAIN WHILE IT IS ALREADY LIT, IT STILL SCORES POINTS AND STAYS LIT.
// WHEN ALL 3 TARGETS ARE LIT THEY FLASH AND PROVIDE A JACKPOT
// WHEN JACKPOT IS PROVIDED ALL LIGHTS TURN OFF. READY TO BE ACTIVATED AGAIN. 

int dropButtonA = 2;         // the number of the input pin
int dropButtonB = 3;        
int dropButtonC = 4;  

int dropLightA = 11;       // the number of the output pin
int dropLightB = 12;     
int dropLightC = 13; 
int dropLightD = 10;     

int stateA = LOW;      // the current state of the output pin
int stateB = LOW;      
int stateC = LOW;   
int stateD = LOW;  

int readingA;   // the current reading from the input pin
int readingB;
int readingC; 

int previousA = LOW;    // the previous reading from the input pin
int previousB = LOW;   
int previousC = LOW;    

long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  pinMode(dropButtonA, INPUT);
  pinMode(dropButtonB, INPUT);
  pinMode(dropButtonC, INPUT);

  pinMode(dropLightA, OUTPUT);
  pinMode(dropLightB, OUTPUT);
  pinMode(dropLightC, OUTPUT);
  pinMode(dropLightD, OUTPUT);
    }

void loop()
{

  //droptarget_01//

   readingA = digitalRead(dropButtonA);

        if (readingA == HIGH && previousA == LOW && millis() - time > debounce) {

        stateA = HIGH;

      time = millis();    
    }

    digitalWrite(dropLightA, stateA);

    previousA = readingA;

  //droptarget_02//

   readingB = digitalRead(dropButtonB);

        if (readingB == HIGH && previousB == LOW && millis() - time > debounce) {

            stateB = HIGH;

          time = millis();    
        }

        digitalWrite(dropLightB, stateB);

        previousB = readingB;

   //droptarget_03//

   readingC = digitalRead(dropButtonC);

        if (readingC == HIGH && previousC == LOW && millis() - time > debounce) {

            stateC = HIGH;

          time = millis();    
        }

        digitalWrite(dropLightC, stateC);

        previousC = readingC;


  //RESET TARGETS - BONUS SCORE
if (digitalRead(dropLightA)==HIGH && digitalRead(dropLightB)==HIGH && digitalRead(dropLightC)==HIGH)
{
  digitalWrite(dropLightA, LOW);
  digitalWrite(dropLightB, LOW);
  digitalWrite(dropLightC, LOW);
  digitalWrite(dropLightD, LOW);
}
else 
{
  digitalWrite(dropLightD, HIGH);
 }

    }

1 个答案:

答案 0 :(得分:0)

问题在于变量名为" stateX" ABC转LOW时不会改变。您需要将这些语句添加到" // RESET TARGETS - BONUS SCORE"当条件为真时循环:

stateA = LOW;
stateB = LOW;
stateC = LOW;
stateD = LOW;

恕我直言,当只有四个项目时,你不需要使用数组 - 代码的清晰度(特别是作者)具有价值。但是,我认为变量" stateX"和" previousX"是多余的;如果你没有两者,你就不会有这个错误。

另外,我建议在" // RESET TARGETS - BONUS SCORE"

中使用先前读取的引脚值进行循环测试
if (readingA == HIGH && readingB == HIGH && readingC == HIGH)

使用当前代码,您可以在循环期间读取引脚的值两次。在这种情况下,引脚的值有可能在循环期间发生变化,您可能会真的混淆结果。