该项目是一个基于 Arduino 的警报,用于监控计算机是否正常工作。万一电脑死机,它会发出警告声信号。
该项目由两部分组成:在计算机上运行的程序和连接在 USB 端口中的 Arduino。
计算机程序以 1 秒的间隔向 Arduino 发送一个预定义的字符。Arduino 从计算机发送的数据中读取数据。如果它在 10 秒内没有收到任何字符,它会从连接到引脚 10 的蜂鸣器发出声音。
计算机程序是用 Visual Basic 编写的。它是一个简单的 Windows 窗体应用程序,包含一个窗体 (Form1)、一个标签 (Label1) 和一个计时器 (Timer1)。
Timer1 间隔设置为 1000 ms。
计算机程序的源代码:
Imports System.IO.Ports
Public Class Form1
Dim port As SerialPort
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
port = New SerialPort("COM4", 9600) 'Set your board COM
port.Open()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
port.Close()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
On Error GoTo error1
port.Write("1") 'character to send to COM port, wich the Arduino will expect.
Label1.Text = Now() & " - Sent ""1"" on COM4 baud 9600 - Success!"
Exit Sub
error1:
Label1.Text = Now() & " - " & ErrorToString()
End Sub
End Class
Arduino程序源代码:
char incomingChar = 0; // for incoming serial data
int NoSignalCounter = -60; // delay to allow for computer startup time
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
while (Serial.available() > 0) {
NoSignalCounter = 0;
incomingChar = Serial.read();
if (incomingChar == '1') { // '1' is the character expected from the computer
noTone(10);
}
else {
tone(10, 2400); // alarm: the character sent from the computer is different from the expected character
}
}
NoSignalCounter++;
if (NoSignalCounter >= 10) { // alarm: no character was received from the computer in the last 10 seconds
NoSignalCounter = 0;
tone(10, 2400); //(pin,frequency)
}
delay(1000);
}
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
全部0条评论
快来发表一下你的评论吧 !