Arduino 如何使程序在迭代次数后停止

Arduino 如何使程序在迭代次数后停止,arduino,Arduino,所以,我这里有我的程序,我试着做一个程序,我们手动拉出电线并放回,每次我拉出它,它是1次迭代,放回是1次迭代。无论何时连接到端口,外部LED都是暗的,拔出时,外部LED是亮的。我应该能够做10次迭代,并在到达那里后停止 问题是,当我包括while(true);声明在,我的外部LED不工作,但没有在那里的声明,该程序运行的方式,我想除了它不会停止工作后,10次迭代,任何帮助是感激的 #include<EEPROM.h> const int LED = 12; const int SWI

所以,我这里有我的程序,我试着做一个程序,我们手动拉出电线并放回,每次我拉出它,它是1次迭代,放回是1次迭代。无论何时连接到端口,外部LED都是暗的,拔出时,外部LED是亮的。我应该能够做10次迭代,并在到达那里后停止

问题是,当我包括while(true);声明在,我的外部LED不工作,但没有在那里的声明,该程序运行的方式,我想除了它不会停止工作后,10次迭代,任何帮助是感激的

#include<EEPROM.h>
const int LED = 12;
const int SWITCH = 4;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  pinMode(LED, OUTPUT);           //LED is always outputting information
  pinMode(LED_BUILTIN, OUTPUT);   //Built in LED is always outputting information
  pinMode(SWITCH, INPUT_PULLUP);  //Switch inputs value when in/out of ground
}

void loop() {
  // put your main code here, to run repeatedly:
  int addr = 0;     //Declaring variables
  int count = 0;
  int seconds;

  if (digitalRead(SWITCH) == LOW) {             //If wire starts in ground, record values
    Serial.println("----Recording----");

    while (count <= 10) {                       //While count value is less than or equal to 10
      if (digitalRead(SWITCH) == LOW) {         //When wire is connected to 4
        count = count + 1;                      //Add one to count in each iteration
        digitalWrite(LED, LOW);                 //LED light is off in this position
        delay(50);                              //Checks switch state every 0.05 seconds
      }
      else if (digitalRead(SWITCH) == HIGH) {   //When wire isnt connected to 4
        count = count + 1;                      //Add one to count in each iteration
        digitalWrite(LED, HIGH);                //LED light is on in this position
        delay(50);                              //Checks switch state every 0.05 seconds
      }
          while (true);
    }
  }
}
#包括
常数int LED=12;
常数int开关=4;
无效设置(){
//将安装代码放在此处,以便运行一次:
序列号开始(115200);
pinMode(LED,输出);//LED总是输出信息
pinMode(LED_内置,输出);//内置LED始终输出信息
pinMode(开关,输入\上拉);//接地/离地时开关输入值
}
void循环(){
//将主代码放在此处,以便重复运行:
int addr=0;//声明变量
整数计数=0;
整数秒;
如果(digitalRead(SWITCH)=低){//如果导线从地开始,则记录值
Serial.println(“----录制----”;

while(count为什么不起作用?让我们重写一下代码:

    while (count <= 10) {
      count = count + 1;
      digitalWrite(LED, digitalRead(SWITCH));
      delay(50);

      while (true); // aaand kill it
    }

while(count)我不认为它应该放在
while(count)里面。我试着移动它的位置,但那没有改变任何东西。你试过把它换到下一行吗?我也不认为你会注意到。它会工作500毫秒,然后停止。它在没有while的情况下已经工作了(真);语句…这只是为了每0.05秒检查一次开关的状态,所以我不确定问题是什么。所以,这只是循环中运行的底部代码吗?还是我要用代码的其他部分替换它?@Thomas嗯,如果你在进入计数循环之前将LED设置为高,那么就完了
    while (count <= 10) {
      byte = digitalRead(SWITCH);
      if (byte != digitalRead(LED))
      { 
        count = count + 1;
        digitalWrite(LED, byte);
      }
      delay(50);
    }
    while (true); // aaand kill it