首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Arduino一键开/关控制

Arduino一键开/关控制
EN

Stack Overflow用户
提问于 2012-05-23 01:00:45
回答 2查看 831关注 0票数 0

我正在尝试找到一种使用一个按钮来启用/禁用命令的方法。(实际上,这是一个多色LED,我正在尝试开始/停止转换颜色)

我的代码在下面,但它不能工作,如果有人能告诉我哪里出了问题,我看不到它……

代码语言:javascript
复制
int red = 0;
int redPin = 9;
int blue = 0;
int bluePin = 11;
int green = 0;
int greenPin = 10;
int state = 0;
int stateModulo = 0;
void setup() {
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(2, INPUT);
}


void checkButton(int var, int result) {
  if (digitalRead(2) == HIGH) {
      var++;
      result = var%2;
    }
}

void changecolor(int startColor,int endColor,int startPin,int endPin,int delayTime)
{
  for (endColor = 0; endColor <= 255; endColor++)
  {
     checkButton(state,stateModulo);
     if (stateModulo == 0) {
       startColor = 255 - endColor;
       analogWrite(endPin, endColor);
       analogWrite(startPin, startColor);
       delay(delayTime);
     }
  }
}

void loop() {
   changecolor(red,green,redPin,greenPin,10);
   changecolor(green,blue,greenPin,bluePin,10);
   changecolor(blue,red,bluePin,redPin,10);
 }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-23 01:34:55

在下面的代码中,result不是通过引用传递的,也不返回。同样的道理也适用于var,在你修改它之后,你会丢弃你所做的任何编辑。

代码语言:javascript
复制
void checkButton(int var, int result) {
  if (digitalRead(2) == HIGH) {
      var++;
      result = var%2;
    }
}

我可以向你推荐一本好的C++书,从中可以学习一些语言基础知识。我的建议:在C++中思考,作者: Bruce Eckel。

票数 0
EN

Stack Overflow用户

发布于 2016-09-16 10:33:06

请考虑以下内容:

代码语言:javascript
复制
//Buttons

int button1 = 7;
int button2 = 6;
int button3 = 4;
int button4 = 2;


//Relays
int rl1 = 13;
int rl2 = 12;
int rl3 = 11;
int rl4 = 8;


//States for Relay and Button (1)

int state1 = HIGH;      // the current state of the output pin
int reading1;           // the current reading from the input pin
int previous1 = LOW;    // the previous reading from the input pin

//States for Relay and Button (2)

int state2 = HIGH;      // the current state of the output pin
int reading2;           // the current reading from the input pin
int previous2 = LOW;    // the previous reading from the input pin

//States for Relay and Button (3)

int state3 = HIGH;      // the current state of the output pin
int reading3;           // the current reading from the input pin
int previous3 = LOW;    // the previous reading from the input pin

//States for Relay and Button (4)

int state4 = HIGH;      // the current state of the output pin
int reading4;           // the current reading from the input pin
int previous4 = LOW;    // the previous reading from the input pin




// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time1 = 0;          // the last time the output pin was toggled
long time2 = 0;
long time3 = 0;
long time4 = 0;

long debounce1 = 200;   // the debounce time, increase if the output flickers
long debounce2 = 200;
long debounce3 = 200;
long debounce4 = 200;


void setup()
{
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);

  pinMode(rl1, OUTPUT);
  pinMode(rl2, OUTPUT);
  pinMode(rl3, OUTPUT);
  pinMode(rl4, OUTPUT);

}

void loop() {

  reading1 = digitalRead(button1);
  reading2 = digitalRead(button2);
  reading3 = digitalRead(button3);
  reading4 = digitalRead(button4);


  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  //Condition Relay 1
  if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce1) {
    if (state1 == HIGH)
      state1 = LOW;
    else
      state1 = HIGH;

    time1 = millis();    
  }

  //Condition Relay 2
    if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce2) {
    if (state2 == HIGH)
      state2 = LOW;
    else
      state2 = HIGH;

    time2 = millis();    
  }

  //Condition Relay 3
    if (reading3 == HIGH && previous3 == LOW && millis() - time3 > debounce3) {
    if (state3 == HIGH)
      state3 = LOW;
    else
      state3 = HIGH;

    time3 = millis();    
  }

  //Condition Relay 4
    if (reading4 == HIGH && previous4 == LOW && millis() - time4 > debounce4) {
    if (state4 == HIGH)
      state4 = LOW;
    else
      state4 = HIGH;

    time4 = millis();    
  }




  digitalWrite(rl1, state1);
  digitalWrite(rl2, state2);
  digitalWrite(rl3, state3);
  digitalWrite(rl4, state4);


  previous1 = reading1;
  previous2 = reading2;
  previous3 = reading3;
  previous4 = reading4;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10706624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档