Sử dụng như một Bluetooth điều khiển từ xa cho Arduino.
Mã cho Arduino;
#include
const int sensorIn = A0;
int mVperAmp = 66; // sử dụng 100 cho 20A Module và 66 cho 30A Mô-đun
int port1 = 2, port2 = 3, port3 = 4, port4 = 5, port5 = 6, port6 = 7, port7 = 8, port8 = 9;
đôi điện áp = 0;
Vrms đôi = 0;
AmpsRMS đôi = 0;
khoảng trống setup () {
Serial.begin (9600);
pinMode (port1, OUTPUT);
pinMode (port2, OUTPUT);
pinMode (port3, OUTPUT);
pinMode (port4, OUTPUT);
pinMode (port5, OUTPUT);
pinMode (port6, OUTPUT);
pinMode (port7, OUTPUT);
pinMode (port8, OUTPUT);
}
khoảng trống loop () {
while (Serial.available ()> 0) {
int inByte = Serial.read ();
switch (inByte) {
trường hợp 'a':
digitalWrite (port1, CAO);
phá vỡ;
trường hợp 'b':
digitalWrite (port1, LOW);
phá vỡ;
trường hợp 'c':
digitalWrite (port2, CAO);
phá vỡ;
trường hợp 'd':
digitalWrite (port2, LOW);
phá vỡ;
trường hợp 'e':
digitalWrite (port3, CAO);
phá vỡ;
trường hợp 'f':
digitalWrite (port3, LOW);
phá vỡ;
trường hợp 'g':
digitalWrite (port4, CAO);
phá vỡ;
trường hợp 'h':
digitalWrite (port4, LOW);
phá vỡ;
trường hợp 'i':
digitalWrite (port5, CAO);
phá vỡ;
trường hợp 'j':
digitalWrite (port5, LOW);
phá vỡ;
trường hợp 'k':
digitalWrite (port6, CAO);
phá vỡ;
trường hợp 'l':
digitalWrite (port6, LOW);
phá vỡ;
trường hợp 'm':
digitalWrite (port7, CAO);
phá vỡ;
trường hợp 'n':
digitalWrite (port7, LOW);
phá vỡ;
trường hợp 'o':
digitalWrite (port8, CAO);
phá vỡ;
trường hợp 'p':
digitalWrite (port8, LOW);
phá vỡ;
}
}
Điện áp = getVPP ();
Vrms = (Voltage / 2.0) * 0,707;
AmpsRMS = (Vrms * 1000) / mVperAmp;
Serial.print ( "\ n");
}
nổi getVPP ()
{
nổi kết quả;
int readValue; // giá trị đọc từ cảm biến
int maxvalue = 0; // lưu trữ giá trị max đây
int MINVALUE = 1024; // lưu trữ giá trị min đây
uint32_t start_time = millis ();
while ((millis () - start_time) <1000) // mẫu cho 1 Sec
{
readValue = analogRead (sensorIn);
// xem nếu bạn có một maxvalue mới
if (readValue> MAXVALUE)
{
/ * Ghi lại các giá trị cảm biến tối đa * /
maxvalue = readValue;
}
if (readValue {
/ * Ghi lại các giá trị cảm biến tối đa * /
MINVALUE = readValue;
}
}
// Trừ phút từ max
result = ((MAXVALUE - MINVALUE) * 5.0) /1024.0;
return result;
}
Bluetooth terminal to controll Arduino for Home Automation