arduino代码通过串口指令来控制13号IO口输出高电平,或者高低电平循环。
具体用法和指令可以看注释:
/*
*Date: 2020/5/11
* Usage:
* A 1 // set ON 1 second
* R 0.5 // cycling test, relay turns 1 second ON, 0.5 second OFF.
*/
int ledPin = LED_BUILTIN;// the number of the LED pin 13#
bool pinSet = false;
// Variables will change:
int ledState = LOW; // ledState used to set the LED
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
long INTERVAL = 1000; // interval at which to blink (milliseconds)
long elapsed_time = INTERVAL;
String string_interval = String(INTERVAL);
String inString = ""; // string to hold input
bool working = false;
bool repeatable = false;
long recycles = 0;
long stop_time = 0;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("1. Press 'A + {ms}' to start the relay.");
Serial.println("2. Press 'B' to pause the relay.");
Serial.println("3. Press 'R + [ms]' to repeat testing");
Serial.println("4. Press 'C' to exchange output pin from pin 13# and pin 12#.");
Serial.println("Default interval set to 1 second.");
Serial.println("Default Pin 13# will drag to HIGH until time reached.");
Serial.println("\n");
}
void loop() {
if (Serial.available() > 0) {
char inChar = Serial.read();
if (inChar != '\r') {
inString += (char)inChar;
}else {
if (inString.startsWith("A")){
String str = inString.substring(2);
if (str != ""){
elapsed_time = INTERVAL * str.toFloat();
string_interval = String(elapsed_time);
}
Serial.println("Reset timestamp, Relay START to work!!") ;
Serial.println("Interval:"+ string_interval + "ms");
inString = "";
working = true;
pinSet = false;
previousMillis = millis();
}
else if (inString == String('B')){
inString = "";
working = false;
pinSet = false;
repeatable = false;
recycles = 0;
Serial.println("Relay PAUSED, Repeatable test PAUSED!!!") ;
digitalWrite(ledPin, LOW);
}
else if (inString == String('C')){
if (12 == ledPin){
digitalWrite(ledPin, LOW);
ledPin = 13;
Serial.println("Set output Pin to 13#") ;
}else{
digitalWrite(ledPin, LOW);
ledPin = 12;
Serial.println("Set output Pin to 12#") ;
}
inString = "";
working = false;
pinSet = false;
digitalWrite(ledPin, LOW);
}
else if (inString.startsWith("R")){
String str = inString.substring(2);
if (str != ""){
stop_time = str.toFloat() * INTERVAL;
Serial.println("Stop time: " + String(stop_time));
repeatable = true;
pinSet = false;
}
else{
repeatable = false;
Serial.println("R command error!!!");
}
inString = "";
recycles = 0;
}
}
}
if (true == working){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis <= elapsed_time) {
if (false == pinSet){
digitalWrite(ledPin, HIGH);
pinSet = true;
Serial.println("Pin 13# was dragged to HIGH");
Serial.println("");
}
}else{
digitalWrite(ledPin, LOW);
Serial.println("Time reached, Relay PAUSED!! Pin was dragged to LOW") ;
Serial.println("");
working = false;
previousMillis = millis();
}
}else {
if (true == repeatable){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= stop_time) {
recycles += 1;
Serial.println("Recycles:" + String(recycles)+ "#");
working = true;
pinSet = false;
previousMillis = millis();
}
}
}
}