【发布时间】:2016-10-12 06:28:44
【问题描述】:
我也是……我是 Arduino 的新手,我目前正在努力完成这项工作……但我已经做了一个小时了,运气不在我身边…… 以下是我正在做的事情的总结:我有一个 Gizduino + 644(在 Phil 有一个带有 ATmega 644 的 Arduino 副本),一个 IR 接近传感器(3 PIN - VCC,GRND,OUT),2 个 LED(红色和黄色)和 2 100ohms 电阻器。
到目前为止,这是我能做的:
- 在 Arduino IDE 中,如果我键入“QRIN”- 接近和红色 LED 将打开...如果接近感应到其范围内的某些东西.. 黄色 LED 将打开。如果我输入“QROUT” - Proximity 将立即关闭,红色 LED 将打开 10 秒然后关闭..
这就是问题所在:
- 如果是第一次运行,黄色 LED 总是会亮起(我的意思是我只需单击 IDE 中的上传按钮)......这是一个非常大的问题......它只会在我输入案例时关闭: 'QRIN' 和 'QROUT'..
在我的代码中,名称如下:
- 红色 LED - 锁定
- 黄色 LED - PROX_SENSOR_LED
- 接近度 - PROX
这是我在 IDE 中的代码:
int LOCK = 13; //RED LED, in pin 13
int PROX = 12; //PROXIMITY, in pin 12
int ANALOG = 0; //OUT of Proximity, in Analog 0
int PROX_SENSOR_LED = 7; //Yellow LED, in pin 7
int val = 0; //value to store
void setup()
{
Serial.begin(9600);
pinMode(LOCK, OUTPUT); //set the pin # as output (VCC of the hardware)
pinMode(PROX, OUTPUT); //set the pin # as output (VCC of the hardware)
pinMode(PROX_SENSOR_LED, OUTPUT); //set the pin # as output (VCC of the hardware)
}
void loop()
{
digitalWrite(PROX_SENSOR_LED, LOW); //sets the output pin initially to LOW (but doesnt work.. T_T)
val = analogRead(ANALOG); //read the input pin 0 to 1023
if (val > 800) //if the sensor value is higher threshold set OUTPUT HIGH
{
digitalWrite(PROX_SENSOR_LED, HIGH); //sets output pin HIGH
delay(100); //waits for .1 second
}
char data = Serial.read(); //read 9600
switch (data) //start of case... like 'ON' 'OFF'
{
case 'QRIN': //this is my 'ON'
digitalWrite(PROX, HIGH); //turn the proximity to ON
digitalWrite(LOCK, HIGH); //turn the lock to ON
break;
case 'QROUT': //this is my off 'OFF'
digitalWrite(PROX, LOW); //turn the proximity to OFF
digitalWrite(LOCK, HIGH); //turn the lock to ON
delay(10000); //for 10 seconds
digitalWrite(LOCK, LOW); //then off
if (ANALOG = HIGH) // I need this 'if' condition because if
{ //this is not here... the Yellow LED is turn ON...
digitalWrite(PROX_SENSOR_LED, LOW); //I don't know why.. T_T
}
break;
}
}
【问题讨论】:
标签: c arduino embedded arduino-ide