带有arduino按钮的开/关开关

时间:2017-10-22 17:13:36

标签: arduino

我试图创建一个运行此代码的Arduino电路。基本上,超声波传感器模块,我设置不同的距离范围。 在每个范围内,我选择播放蜂鸣器并点亮带有不同组合的RGB灯。

到目前为止,所有工作都有效,直到我决定添加一个按钮作为开关: 我无法想到一个代码可以打开/关闭"电路。更具体地说,如果电路关闭,我想点亮一个LED(引脚n.11),并在它开启时正常工作,但现在它可以在" on"状态只有当我按下按钮时,如果我松开它,电路会锁定自己"在最后一组频率(用于蜂鸣器)和颜色(用于RGB led)。

//pins for buzzer and ultrasonic sensor
int const trigPin = 10;
int const echoPin = 9;
int const buzzPin = 2;
//pins for rgb led(color changes  as i get closer to an object)
const int VERDE = 3;  
const int BLU = 5;  
const int ROSSO = 6;  
//pin for led representing main state of the circuit
const int accPin = 7;
// pins for button i want to use as an on/off switch
const int buttonPin = 11;
int stato=0;
int old = 0;

const int delayTime = 5; 
void setup()
{
  pinMode(VERDE, OUTPUT);  
  pinMode(BLU, OUTPUT);  
  pinMode(ROSSO, OUTPUT);
  pinMode(accPin,OUTPUT);  

  digitalWrite(VERDE, LOW);  
  digitalWrite(BLU, LOW);  
  digitalWrite(ROSSO, LOW); 
  digitalWrite(accPin,LOW); 

  pinMode(buttonPin,INPUT);

  pinMode(trigPin, OUTPUT); // trig pin will have pulses output
  pinMode(echoPin, INPUT); // echo pin should be input to get pulse width
  pinMode(buzzPin, OUTPUT); // buzz pin is output to control buzzering
}

void loop(){
  stato=digitalRead(buttonPin);

  //if the circuit is off and i turn it on by pressing the button, or if the circuit's already on, then do the things
  if(((stato==1)&&(old==0))||((stato==0)&&(old==1))){

  digitalWrite(accPin,LOW);

  int duration, distance;     // Duration will be the input pulse width and distance will be the distance to the obstacle in centimeters

  digitalWrite(trigPin, HIGH);   // Output pulse with 1ms width on trigPin
  delay(1);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);  // Measure the pulse input in echo pin
  // Distance is half the duration devided by 29.1 (from datasheet)
  distance = (duration/2) / 29.1;

 // the following code is just a test to see get a different frequency from the buzzer and a different color from the rgb led, while moving from the facing object
    if (distance <= 10 && distance >= 0) {
      // Buzz
      tone(buzzPin, 2000);
        digitalWrite(ROSSO, HIGH);
    }
    if (distance <= 20 && distance > 10) {
      // Buzz
      tone(buzzPin, 1750);
        digitalWrite(BLU, HIGH);
        digitalWrite(ROSSO, HIGH);

    }
    if (distance <= 30 && distance > 20) {
      // Buzz
      tone(buzzPin, 1250);
      digitalWrite(BLU, HIGH);
    }
    if (distance <= 40 && distance > 30) {
      // Buzz
      tone(buzzPin, 1000);
      digitalWrite(BLU, HIGH);
      digitalWrite(VERDE, HIGH);
    }
    if (distance <= 80 && distance > 50) {
      // Buzz
      tone(buzzPin, 750);
      digitalWrite(VERDE, HIGH);
    }
    if(distance>=81){
      noTone(buzzPin);// if out of range then shut up
      }

   delay(60);   // Waiting 60 ms won't hurt any one
   digitalWrite(VERDE, LOW);  
   digitalWrite(BLU, LOW);  
   digitalWrite(ROSSO, LOW);
    old=1;
   }

  //if the circuit is on and i press the button, turn it off
  if((old==1)&&(stato=1)){old=0;}


  //if the circuit is off and if i let it like this, don't do nothing, but turn on the a led to make it obvious(pin n.11)
  if((old==0)&&(stato==0)){
    digitalWrite(VERDE, LOW);  
   digitalWrite(BLU, LOW);  
   digitalWrite(ROSSO, LOW);
   noTone(buzzPin);
   digitalWrite(accPin,HIGH);
    }
}

CIRCUIT

1 个答案:

答案 0 :(得分:1)

用整个代码或启动程序的部分创建一个函数,然后将中断附加到按钮针上(您必须更换按钮针)。使用布尔值来切换按钮的按下状态。

相关问题