我把一个蜂鸣器连接到13号针和在我的Arduino上。
“闪烁”示例工作正常,蜂鸣器按预期每秒关闭和开启。
然而,当我尝试使用我的代码执行相同的动态更新时,我无法对特定事件进行嗡嗡声。这是一个本土的安全系统 - 当门被打开时,我希望Arduino检查一个PHP页面,如果系统已经布防,它将返回“武装”。
除了蜂鸣器部分外,其他一切似乎都有效。
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xD3, 0x6C };
char serverName[] = "mysite.com";
String currentLine = "";
String armed = "No";
int nFrontWindow = 0;
int sFrontWindow = 1;
int kitchenWindow = 2;
int bedroomWindow = 3;
int frontDoor = 4;
int val0 = 0;
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;
int threshold0 = 20;
int threshold1 = 20;
int threshold2 = 20;
int threshold3 = 20;
int threshold4 = 20;
int breach0 = 0;
int breach1 = 0;
int breach2 = 0;
int breach3 = 0;
int breach4 = 0;
int alarm0 = 0;
int alarm1 = 0;
int alarm2 = 0;
int alarm3 = 0;
int alarm4 = 0;
EthernetClient client;
void setup() {
pinMode(13, OUTPUT);
// initialize serial communications at 9600 bps:
Serial.begin(9600);
if(Ethernet.begin(mac) == 0) { // start ethernet using mac & IP address
Serial.println("Failed to configure Ethernet using DHCP");
while(true) // no point in carrying on, so stay in endless loop:
;
}
delay(1000); // give the Ethernet shield a second to initialize
}
void loop() {
// read the analog in values:
val0 = analogRead(nFrontWindow);
val1 = analogRead(sFrontWindow);
val2 = analogRead(kitchenWindow);
val3 = analogRead(bedroomWindow);
val4 = analogRead(frontDoor);
// print the analog in values:
if (val0 > threshold0)
{
Serial.print("nFrontWindow: ");
Serial.print(val0);
Serial.print("\n");
if (alarm0)
{
if (breach0 < 10)
breach0++;
}
else
{
if (breach0 > 9)
{
alarm0 = 1;
send_alert(0);
}
else
breach0++;
}
}
else
{
if (alarm0)
{
if (breach0 > 0)
breach0--;
else
{
alarm0 = 0;
send_alert(10);
}
}
else
{
if (breach0 > 0)
breach0--;
}
}
if (val1 > threshold1)
{
Serial.print("sFrontWindow: ");
Serial.print(val1);
Serial.print("\n");
if (alarm1)
{
if (breach1 < 10)
breach1++;
}
else
{
if (breach1 > 9)
{
alarm1 = 1;
send_alert(1);
}
else
breach1++;
}
}
else
{
if (alarm1)
{
if (breach1 > 0)
breach1--;
else
{
alarm1 = 0;
send_alert(11);
}
}
else
{
if (breach1 > 0)
breach1--;
}
}
if (val2 > threshold2)
{
Serial.print("kitchenWindow: ");
Serial.print(val2);
Serial.print("\n");
if (alarm2)
{
if (breach2 < 10)
breach2++;
}
else
{
if (breach2 > 9)
{
alarm2 = 1;
send_alert(2);
}
else
breach2++;
}
}
else
{
if (alarm2)
{
if (breach2 > 0)
breach2--;
else
{
alarm2 = 0;
send_alert(12);
}
}
else
{
if (breach2 > 0)
breach2--;
}
}
if (val3 > threshold3)
{
Serial.print("bedroomWindow: ");
Serial.print(val3);
Serial.print("\n");
if (alarm3)
{
if (breach3 < 10)
breach3++;
}
else
{
if (breach3 > 9)
{
alarm3 = 1;
send_alert(3);
}
else
breach3++;
}
}
else
{
if (alarm3)
{
if (breach3 > 0)
breach3--;
else
{
alarm3 = 0;
send_alert(13);
}
}
else
{
if (breach3 > 0)
breach3--;
}
}
if (val4 > threshold4)
{
Serial.print("frontDoor: ");
Serial.print(val4);
Serial.print("\n");
if (alarm4)
{
if (breach4 < 10)
breach4++;
}
else
{
if (breach4 > 9)
{
alarm4 = 1;
send_alert(4);
}
else
breach4++;
}
}
else
{
if (alarm4)
{
if (breach4 > 0)
breach4--;
else
{
alarm4 = 0;
send_alert(14);
}
}
else
{
if (breach4 > 0)
breach4--;
}
}
delay(100);
}
void send_alert(int pin)
{
if (client.connect(serverName, 80)>0) {
client.flush();
Serial.print("\nconnected... ");
String link = "GET [PHP FILE GOES HERE]";
link += pin;
link += " HTTP/1.1";
Serial.print("Sending alert code: ");
Serial.print(pin);
Serial.print("\n\n");
client.println(link);
client.println("Host: mysite.com");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
delay(3000);
} else {
Serial.println("connection failed");
//handle this
}
Serial.print("Server response:\n");
Serial.print("----------------\n");
while(client.available()){
char c = client.read();
Serial.print(c);
currentLine += c;
if (c == '\n') {
currentLine = "";
}
if (currentLine.endsWith("armed"))
{
if (pin == 4 || pin == 3 || pin == 2 || pin == 1 || pin == 0)
{
Serial.print("\nBZZZZZZZZZZ\n");
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
}
}
Serial.print("\n----------------\n");
//else
// Serial.println("result not found");
client.stop();
client.flush();
}
答案 0 :(得分:1)
我认为您的问题是以太网屏蔽使用了引脚13。引脚10,11和12也是如此。所以请使用其他引脚。
答案 1 :(得分:0)
我认为你的问题是你没有产生方波来使声音成为蜂鸣器。 由于你使用的是Arduino,你可以使用tone()和noTone()函数,在Arduino site中有很好的解释。