Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 制作arduino替代函数_C_Arduino - Fatal编程技术网

C 制作arduino替代函数

C 制作arduino替代函数,c,arduino,C,Arduino,我试图为arduino编写一些c代码,通过使用切换变量在两个不同的函数之间切换。有人能帮我做这件事吗 int hold = 1; void setup() { } void loop() { Serial.println(hold); if (hold == 1){ hold = 2; } if (hold == 2){ hold = 1; } } 像这样的 int hold = 1

我试图为arduino编写一些c代码,通过使用切换变量在两个不同的函数之间切换。有人能帮我做这件事吗

int hold = 1;
void setup() {
}

void loop() {
       Serial.println(hold);
     if (hold == 1){
     hold = 2;
       }
     if (hold == 2){
     hold = 1;
       }              
}
像这样的

int hold = 1;

// ...

if (hold)
    functionA();
else
    functionB();
hold = !hold;
编辑 这里还有两种方法。第一个更简单,使用
switch
语句,这实际上是另一种执行
if…else..

#include <stdio.h>

#define NUMFUNCS    4

int funcA(void);
int funcB(void);
int funcC(void);
int funcD(void);

int main(void){
    int action = 0;
    int res;
    while(1) {
        switch(action) {
            case 0:  res = funcA();
                     break;
            case 1:  res = funcB();
                     break;
            case 2:  res = funcC();
                     break;
            default: res = funcD();
                     break;
        }
        printf ("Function returned %d\n", res);
        action = (action + 1) % NUMFUNCS;
    }
    return 0;
}

int funcA(void) {
    return 1;
}
int funcB(void) {
    return 2;
}
int funcC(void) {
    return 3;
}
int funcD(void) {
    return 4;
}
#包括
#定义NUMFUNCS 4
int funcA(void);
int funcB(void);
int funcC(void);
int funcD(void);
内部主(空){
int action=0;
国际关系;
而(1){
开关(动作){
案例0:res=funcA();
打破
案例1:res=funcB();
打破
案例2:res=funcC();
打破
默认值:res=funcD();
打破
}
printf(“函数返回%d\n”,res);
动作=(动作+1)%NUMFUNCS;
}
返回0;
}
int funcA(void){
返回1;
}
int funcB(void){
返回2;
}
int func(void){
返回3;
}
int funcD(void){
返回4;
}
更复杂的一点是使用函数指针数组。如果要向函数传递参数,还需要更改数组声明。缺点是,除非您有可变函数,否则它们都必须具有相同的参数

#include <stdio.h>

#define NUMFUNCS    4

int funcA(void);
int funcB(void);
int funcC(void);
int funcD(void);

int (*funcarry[NUMFUNCS])(void) = {     // array of function pointers
    funcA, funcB, funcC, funcD
};

int main(void){
    int action = 0;
    int res;
    while(1) {
        res = (*funcarry[action])();
        printf ("Function returned %d\n", res);
        action = (action + 1) % NUMFUNCS;
    }
    return 0;
}

int funcA(void) {
    return 1;
}
int funcB(void) {
    return 2;
}
int funcC(void) {
    return 3;
}
int funcD(void) {
    return 4;
}
#包括
#定义NUMFUNCS 4
int funcA(void);
int funcB(void);
int funcC(void);
int funcD(void);
int(*funcarry[NUMFUNCS])(void)={//函数指针数组
funcA,funcB,funcC,funcD
};
内部主(空){
int action=0;
国际关系;
而(1){
res=(*funcarry[action])();
printf(“函数返回%d\n”,res);
动作=(动作+1)%NUMFUNCS;
}
返回0;
}
int funcA(void){
返回1;
}
int funcB(void){
返回2;
}
int func(void){
返回3;
}
int funcD(void){
返回4;
}

我需要让它经历4个以上不同的事情,并在每个循环中选择一个,所以我不认为还有其他选择。除非我错了。@Warior4356我已经扩展了我的答案。这会起作用,但是如果,你需要使用
else,否则,你将
hold
设置为2,然后下一个
if
将触发并再次设置。