卡在while循环中 [英] Stuck in while loop arduino

查看:130
本文介绍了卡在while循环中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的while主循环

Here is my while main loop

void loop () {

  s4state = digitalRead(11); //Lit pin 11 switch

  if (s4state == 1) {  // wargning/detrese
    detresse(1);
  }
  if (s4state == 0) {  // wargning/detrese
    detresse(0);
  }
}

这是我要调用的函数.

void detresse(int valeurPin) {
  while(1==valeurPin) {
  digitalWrite (2, HIGH) ;
  digitalWrite (3, HIGH) ;
  digitalWrite (4, HIGH) ;
  digitalWrite (5, HIGH) ;
  digitalWrite (6, HIGH) ;
  digitalWrite (7, HIGH) ;

    delay (500) ;

    digitalWrite (2, LOW) ;
    digitalWrite (3, LOW) ;
    digitalWrite (4, LOW) ;
    digitalWrite (5, LOW) ;
    digitalWrite (6, LOW) ;
    digitalWrite (7, LOW) ;

    delay (500) ;
  }
}

但是由于某些原因,当我将引脚切换得太关闭时,ligth会一直打开和关闭.我不明白为什么我会卡在这个循环中.我该如何逃脱?

But for some reason when I switch the pin too off the ligth keep turning on and off. I do not understand why I am stuck is this loop. How can I escape from it?

推荐答案

但是由于某些原因,当我将针脚也从灯丝上移开时,保持 打开和关闭.我不明白为什么我会卡在这个循环中. 我该如何逃脱?

But for some reason when I switch the pin too off the ligth keep turning on and off. I do not understand why I am stuck is this loop. How can I escape from it?

循环不会中断,因为void detresse(int valeurPin)的输入不会改变.也就是说,一旦被调用,开关的状态就不会对该功能产生任何影响,并且它将在while循环中无休止地运行.您可以做的是像这样更改detresse:

The loop doesn't break because the input to void detresse(int valeurPin) is not going to change. That is to say, once it is called, the state of the switch has no effect on that function and it will run in the while loop endlessly. What you can do is change up detresse like so:

void detresse(void) {
  digitalWrite (2, HIGH) ;
  digitalWrite (3, HIGH) ;
  digitalWrite (4, HIGH) ;
  digitalWrite (5, HIGH) ;
  digitalWrite (6, HIGH) ;
  digitalWrite (7, HIGH) ;

    delay (500) ;

    digitalWrite (2, LOW) ;
    digitalWrite (3, LOW) ;
    digitalWrite (4, LOW) ;
    digitalWrite (5, LOW) ;
    digitalWrite (6, LOW) ;
    digitalWrite (7, LOW) ;

    delay (500) ;
}

此外,无论开关的状态如何,您都将调用此函数,这不是您想要的,因为无论您用开关做什么,都会触发照明顺序.由于开关只能有两种状态,因此您只能在一个或另一个上调用detresse,而不要同时调用这两个状态.假设您要使用正逻辑,这将使loop看起来像这样:

Also, you are calling this function regardless of the state of the switch, which is not what you want because the lighting sequence will be triggered regardless of what you are doing with the switch. Since there can only be two states for the switch, you should only call detresse on the either one or the other, not both. Assuming you want positive logic, this will make loop look like this:

void loop () {

  s4state = digitalRead(11); //Lit pin 11 switch

  if (s4state == 1) {  // wargning/detrese
    detresse();
  }
}

这篇关于卡在while循环中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆