พอดีผมจะเขียนโค๊ดคู่กับ sensor วัดกระแส ac โดยเมื่อมีการใช้ไฟ เกิน 30 mA relayก็จะทำงาน มากว่า 700mA ก็จะตัดrelay
และเมื่อมีการเสียบปลั๊กเข้า แล้วชักออก 1 ครั้ง จะได้ 0 mA ผมจะเอาค่ามันมาใช้เมื่อมีการชักปลั๊ก 10 ครั้งให้มัน relay ไป10 ซม
ผมเขียนโค๊ดแบบนี้อะคับ
int sensorTA12 = A0; // Analog input pin that sensor is attached to
int relay1 = A2;
int buttonState = 0;
int lastButtonState = 0;
const int sensorMin = 0;
const int sensorMax = 1000;
const int R70 = 70;
const int R0 = 0;
float count = 0;
int i = 1;
float nVPP; // Voltage measured across resistor
float nCurrThruResistorPP; // Peak Current Measured Through Resistor
float nCurrThruResistorRMS; // RMS current through Resistor
float nCurrentThruWire; // Actual RMS current in Wire
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
void setup()
{
pinMode (relay1, HIGH);
Serial.begin(9600);
pinMode(sensorTA12, INPUT);
analogReference(DEFAULT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // adres I2C mojego wyświetlacza
display.clearDisplay();
}
void loop()
{
nVPP = getVPP();
/*
Use Ohms law to calculate current across resistor
and express in mA
*/
nCurrThruResistorPP = (nVPP/200.0) * 1000.0;
nCurrThruResistorRMS = nCurrThruResistorPP * 0.707;
nCurrentThruWire = nCurrThruResistorRMS * 1000 ;
Serial.print("Volts Peak : ");
Serial.println(nVPP,3);
Serial.print("
Peak) : ");
Serial.print(nCurrThruResistorPP,3);
Serial.println(" mA Peak to Peak");
Serial.print("
RMS) : ");
Serial.print(nCurrThruResistorRMS,3);
Serial.println(" mA RMS");
Serial.print("Wire : ");
Serial.print(nCurrentThruWire ,3);
Serial.println(" mA RMS");
Serial.println();
display.clearDisplay();// usatwienia lcd
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(25,0);
display.println("Current");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(28,17);
display.println(nCurrentThruWire ,3);
display.setTextColor(WHITE);
display.setCursor(80,17);
display.println("mA");
display.display();
////////////////////////////////////////////////
int analogValue = analogRead(nCurrentThruWire);{
if (analogValue > 30){
digitalWrite(relay1,LOW);
Serial.println("50");
delay(10000);
return;
}
else { ///////////////// โค๊ดส่วนนี้อะคับที่ รับค่า 0
buttonState = digitalRead(nCurrentThruWire);
if (buttonState == HIGH) {
i = i +1;
if (i > 10) {
i = 1;
}
}
switch (i) {
case 1:
digitalWrite(relay1,HIGH);
Serial.println("001");
delay(5000);
return;
case 2:
digitalWrite(relay1,HIGH);
Serial.println("002");
delay(5000);
break;
case 3:
digitalWrite(relay1,HIGH);
Serial.println("003");
delay(5000);
break;
case 4:
digitalWrite(relay1,HIGH);
Serial.println("004");
delay(5000);
break;
case 5:
digitalWrite(relay1,HIGH);
Serial.println("005");
delay(5000);
return;
case 6:
digitalWrite(relay1,HIGH);
Serial.println("006");
delay(5000);
break;
case 7:
digitalWrite(relay1,HIGH);
Serial.println("007");
delay(5000);
break;
case 8:
digitalWrite(relay1,HIGH);
Serial.println("008");
delay(5000);
break;
case 9:
digitalWrite(relay1,HIGH);
Serial.println("009");
delay(5000);
break;
case 10:
digitalWrite(relay1,HIGH);
Serial.println("0010");
delay(5000);
return;
}
}
if (analogValue > 700){
digitalWrite(relay1,HIGH);
Serial.println("700");
}
} // Analog
/////////////////////////////////////////////////
}// loop
float getVPP()
{
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max valu
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorTA12);
if (readValue > maxValue)
{
maxValue = readValue;
}
}
result = (maxValue * 5.0)/1024.0;
return result;
}
รบกวนดูโค๊ด Arduino ให้หน่อยครับ
และเมื่อมีการเสียบปลั๊กเข้า แล้วชักออก 1 ครั้ง จะได้ 0 mA ผมจะเอาค่ามันมาใช้เมื่อมีการชักปลั๊ก 10 ครั้งให้มัน relay ไป10 ซม
ผมเขียนโค๊ดแบบนี้อะคับ
int sensorTA12 = A0; // Analog input pin that sensor is attached to
int relay1 = A2;
int buttonState = 0;
int lastButtonState = 0;
const int sensorMin = 0;
const int sensorMax = 1000;
const int R70 = 70;
const int R0 = 0;
float count = 0;
int i = 1;
float nVPP; // Voltage measured across resistor
float nCurrThruResistorPP; // Peak Current Measured Through Resistor
float nCurrThruResistorRMS; // RMS current through Resistor
float nCurrentThruWire; // Actual RMS current in Wire
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
void setup()
{
pinMode (relay1, HIGH);
Serial.begin(9600);
pinMode(sensorTA12, INPUT);
analogReference(DEFAULT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // adres I2C mojego wyświetlacza
display.clearDisplay();
}
void loop()
{
nVPP = getVPP();
/*
Use Ohms law to calculate current across resistor
and express in mA
*/
nCurrThruResistorPP = (nVPP/200.0) * 1000.0;
nCurrThruResistorRMS = nCurrThruResistorPP * 0.707;
nCurrentThruWire = nCurrThruResistorRMS * 1000 ;
Serial.print("Volts Peak : ");
Serial.println(nVPP,3);
Serial.print("Peak) : ");
Serial.print(nCurrThruResistorPP,3);
Serial.println(" mA Peak to Peak");
Serial.print("RMS) : ");
Serial.print(nCurrThruResistorRMS,3);
Serial.println(" mA RMS");
Serial.print("Wire : ");
Serial.print(nCurrentThruWire ,3);
Serial.println(" mA RMS");
Serial.println();
display.clearDisplay();// usatwienia lcd
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(25,0);
display.println("Current");
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(28,17);
display.println(nCurrentThruWire ,3);
display.setTextColor(WHITE);
display.setCursor(80,17);
display.println("mA");
display.display();
////////////////////////////////////////////////
int analogValue = analogRead(nCurrentThruWire);{
if (analogValue > 30){
digitalWrite(relay1,LOW);
Serial.println("50");
delay(10000);
return;
}
else { ///////////////// โค๊ดส่วนนี้อะคับที่ รับค่า 0
buttonState = digitalRead(nCurrentThruWire);
if (buttonState == HIGH) {
i = i +1;
if (i > 10) {
i = 1;
}
}
switch (i) {
case 1:
digitalWrite(relay1,HIGH);
Serial.println("001");
delay(5000);
return;
case 2:
digitalWrite(relay1,HIGH);
Serial.println("002");
delay(5000);
break;
case 3:
digitalWrite(relay1,HIGH);
Serial.println("003");
delay(5000);
break;
case 4:
digitalWrite(relay1,HIGH);
Serial.println("004");
delay(5000);
break;
case 5:
digitalWrite(relay1,HIGH);
Serial.println("005");
delay(5000);
return;
case 6:
digitalWrite(relay1,HIGH);
Serial.println("006");
delay(5000);
break;
case 7:
digitalWrite(relay1,HIGH);
Serial.println("007");
delay(5000);
break;
case 8:
digitalWrite(relay1,HIGH);
Serial.println("008");
delay(5000);
break;
case 9:
digitalWrite(relay1,HIGH);
Serial.println("009");
delay(5000);
break;
case 10:
digitalWrite(relay1,HIGH);
Serial.println("0010");
delay(5000);
return;
}
}
if (analogValue > 700){
digitalWrite(relay1,HIGH);
Serial.println("700");
}
} // Analog
/////////////////////////////////////////////////
}// loop
float getVPP()
{
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max valu
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorTA12);
if (readValue > maxValue)
{
maxValue = readValue;
}
}
result = (maxValue * 5.0)/1024.0;
return result;
}