【发布时间】:2017-10-22 17:13:36
【问题描述】:
我正在尝试创建一个运行此代码的 Arduino 电路。基本上,使用超声波传感器模块,我设置了不同的距离范围。 在每个范围内,我选择播放蜂鸣器并点亮具有不同组合的 RGB LED。
到目前为止一切正常,直到我决定添加一个按钮作为开关: 我想不出一个“打开/关闭”电路的代码。更具体地说,如果电路关闭,我想点亮一个 LED(引脚 n.11),并在它打开时正常工作,但现在它只有在我按住按钮时才能在“打开”状态下工作,并且如果我松开它,电路会“锁定”最后一组频率(用于蜂鸣器)和颜色(用于 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);
}
}
【问题讨论】:
标签: arduino