改变在C中循环的回调函数中的全局变量



此函数将处于常量循环中。当一个按钮被按下时,它需要检查一个条件是否为真,然后改变相同的条件。(开/关按钮).

我有一个困难的时间弄清楚如何使用一个变量,我可以全局改变每次,使它保持如此,直到下一个按钮按下。

我是一个C语言新手,所以如果这很简单,我只是没有看到它,我不会感到惊讶。

编辑:

对不起,刚才说的那么模糊:

下面的

只在false时返回值:

#define RELAY  1
#define BUTTON 0
//starts in off position
bool isOn = false ;
void waitButton (void)
{
  fflush (stdout) ;
  //Button is Pressed
  if (digitalRead(BUTTON) == 1)
  {
    printf ("Button pushedn") ;
    //if it's off, turn it on
    if (isOn = false)
    {
      digitalWrite (RELAY, LOW) ;
      static bool isOn = true ;
      printf("Turned offn") ;
    }
    //if it's on turn it off
    if (isOn = true)
    {
      digitalWrite (RELAY, HIGH) ;
      static bool isOn = false ;
      printf("Turned Onn") ;
    }
    //delay for release of button so it does not trigger twice in a row
    delay(3000) ;
  } else 
  //Button is not pressed keep going through loop
  {
    printf("Waiting for Buttonn") ;
    delay(200) ;
  }
}
int main (void)
{
  setup() ;
  for (;;)
  {
    funcA () ;
  }
}

下面的代码运行。你的代码几乎可以运行了。您的工作尚未完成,但运行此命令将为您提供一个起点。

#include <stdlib.h>
#include <stdio.h>
// you said global variables , here they are
int button1 = 0;
int button2 = 0;
int condition1 = 0;
int condition2 = 0;
// end global variables
#define TRUE 1
#define FALSE 0
int setup(  )
{
  // whatever setup you need
}
funcA(  )
{
  if ( button1 )
  {
    printf( "nYou pressed button1" );
    // add code here to do something
  }
  if ( button2 )
  {
    if ( condition2 == TRUE )   // double = sign is very important here
    {
      condition2 = FALSE;   // change condition2 to false
      printf( "nYou pressed button2 and condition2 is now 0" );
    }
    else
    {
      condition2 = TRUE;    //change condition2 to true
      printf( "nYou pressed button2 and condition2 is now 1" );
    }
//    delay(500) ;
  }
}
int main( void )
{
  int i = 0;
  setup(  );
  for ( i; i < 100; i++ )   // lets loop 100 times for now
  {
    button1 = 1;
    button2 = 0;
    funcA(  );
    button1 = 0;
    button2 = 1;
    funcA(  );
  }
}

我认为您可以使用变量condition2作为全局变量,并在函数funcA中更改它。

bool condition2 = true;
funcA()
{
 // your code
}

或者您可以将condition2声明为funcA中的static变量。然后状态可以保存在循环中。如果您不熟悉static关键字,请参阅wiki页面了解详细信息。

funcA()
{
 static bool condition2 = true;
 // your code
}

希望能有所帮助。

是嵌入式系统吗?如果按钮在任何时候被按下,最好的方法是通过中断的方式。调用延迟例程可能会导致错过关键事件。

如果你只是在写一个C控制台程序,那么我仍然建议不要像你在代码中所做的那样延迟。

如果我正确理解了你的用例,你需要一个变量在每次按键时改变它的状态,并记住它直到下一次按键事件。

unsigned int gU32KeyState = CLEAR; //SET and CLEAR can be macros, defined to 1 and 0 respectively.
/* 
 * gU32KeyState shall be updated function or ISR that get the key event.
 * On a simple note, you can toggle gU32KeyState by simply EX-ORing it with 1:
 * as in -- gU32KeyState ^= 1;
 * Additionally, extern the variable if it is supposed to be used with some other source file too: 
 * as in -- extern unsigned int gU32KeyState;
 */
void funcA(void)
{
  //button pressed?
  if(SET == gU32KeyState)
  {
    if(TRUE == condition2)
    {
      //change condition2 to false
      condition2 = FALSE; //TRUE and FALSE can be macros, defined to 1 and 0 respectively.
    }
    else
    {
      //change condition2 to true
      condition2 = TRUE;
    }
    delay(500) ; //Can't quite comprehend why a delay is needed
  }
  else
  {
    delay(500) ; //Can't quite comprehend why a delay is needed
  }
}

我不知道什么是condition2。为了使逻辑易于理解,我简单地将其视为您偏好的数据类型的另一个变量。此外,假设如果按下了键,设置了变量,只要不再按下按钮,funcA()中的第一个if条件将被计算为true。但是,当第一个if求值为true时,condition2将一直保持切换状态。还需要再考虑一下吗?

显然,我给出的代码片段不能立即编译。

我不明白你的问题,所以我猜,使用如果(condition2 = = true)而不是如果(condition2=true),看它是否工作

搞懂了

语法错误太多。

int ISON = 0 ;
waitButton ()
{
  fflush (stdout) ;
  if (digitalRead(BUTTON) == 1)
  {
    printf ("Button pushedn") ;
    if (ISON == 1)
    {
      digitalWrite (RELAY, LOW) ;
      ISON = 0 ;
      printf("Turned offn") ;
    }
    else
    {
      digitalWrite (RELAY, HIGH) ;
      ISON = 1 ;
      printf("Turned Onn") ;
    }
    delay(3000) ;
  } else {
    printf("Waiting for Buttonn") ;
    delay(200) ;
  }
}
int main (void)
{
  setup () ;
  for (;;)
  {
    waitButton   () ;
  }
}

最新更新