Last active
December 7, 2021 01:04
-
-
Save assimilat/8bb0921333054be777c6ac16318ad59d to your computer and use it in GitHub Desktop.
sainsmart 8 channel relay control
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
volatile byte relayState = LOW; | |
long lastDebounceTime = 0; | |
long debounceDelay = 1000; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(9600); | |
pinMode(2,OUTPUT); | |
pinMode(3,OUTPUT); | |
pinMode(4,OUTPUT); | |
pinMode(5,OUTPUT); | |
pinMode(6,OUTPUT); | |
pinMode(7,OUTPUT); | |
pinMode(8,OUTPUT); | |
pinMode(9,OUTPUT); | |
digitalWrite(2,HIGH); | |
digitalWrite(3,HIGH); | |
digitalWrite(4,HIGH); | |
digitalWrite(5,HIGH); | |
digitalWrite(6,HIGH); | |
digitalWrite(7,HIGH); | |
digitalWrite(8,HIGH); | |
digitalWrite(9,HIGH); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly | |
String readString; | |
String Q; | |
while (Serial.available()){ | |
delay(1); | |
if(Serial.available()>0){ | |
char c = Serial.read(); | |
if (isControl(c)){ | |
break; | |
} | |
readString += c; | |
} | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == HIGH){ | |
Q = readString; | |
if (Q=="off1"){ | |
digitalWrite(2,HIGH); | |
relayState = LOW; | |
Serial.println("OFF1"); | |
} | |
delay(50); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == LOW){ | |
Q = readString; | |
if (Q=="on1"){ | |
digitalWrite(2,LOW); | |
relayState = HIGH; | |
Serial.println("ON1"); | |
} | |
delay(50); | |
} | |
if (Q=="read1"){ | |
Serial.println (digitalRead(2), DEC); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == HIGH){ | |
Q = readString; | |
if (Q=="off2"){ | |
digitalWrite(3,HIGH); | |
relayState = LOW; | |
Serial.println("OFF2"); | |
} | |
delay(50); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == LOW){ | |
Q = readString; | |
if (Q=="on2"){ | |
digitalWrite(3,LOW); | |
relayState = HIGH; | |
Serial.println("ON2"); | |
} | |
delay(50); | |
} | |
if (Q=="read2"){ | |
Serial.println (digitalRead(3), DEC); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == HIGH){ | |
Q = readString; | |
if (Q=="off3"){ | |
digitalWrite(4,HIGH); | |
relayState = LOW; | |
Serial.println("OFF3"); | |
} | |
delay(50); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == LOW){ | |
Q = readString; | |
if (Q=="on3"){ | |
digitalWrite(4,LOW); | |
relayState = HIGH; | |
Serial.println("ON3"); | |
} | |
delay(50); | |
} | |
if (Q=="read3"){ | |
Serial.println (digitalRead(4), DEC); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == HIGH){ | |
Q = readString; | |
if (Q=="off4"){ | |
digitalWrite(5,HIGH); | |
relayState = LOW; | |
Serial.println("OFF4"); | |
} | |
delay(50); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == LOW){ | |
Q = readString; | |
if (Q=="on4"){ | |
digitalWrite(5,LOW); | |
relayState = HIGH; | |
Serial.println("ON4"); | |
} | |
delay(50); | |
} | |
if (Q=="read4"){ | |
Serial.println (digitalRead(5), DEC); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == HIGH){ | |
Q = readString; | |
if (Q=="off5"){ | |
digitalWrite(6,HIGH); | |
relayState = LOW; | |
Serial.println("OFF5"); | |
} | |
delay(50); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == LOW){ | |
Q = readString; | |
if (Q=="on5"){ | |
digitalWrite(6,LOW); | |
relayState = HIGH; | |
Serial.println("ON5"); | |
} | |
delay(50); | |
} | |
if (Q=="read5"){ | |
Serial.println (digitalRead(6), DEC); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == HIGH){ | |
Q = readString; | |
if (Q=="off6"){ | |
digitalWrite(7,HIGH); | |
relayState = LOW; | |
Serial.println("OFF6"); | |
} | |
delay(50); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == LOW){ | |
Q = readString; | |
if (Q=="on6"){ | |
digitalWrite(7,LOW); | |
relayState = HIGH; | |
Serial.println("ON6"); | |
} | |
delay(50); | |
} | |
if (Q=="read6"){ | |
Serial.println (digitalRead(7), DEC); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == HIGH){ | |
Q = readString; | |
if (Q=="off7"){ | |
digitalWrite(8,HIGH); | |
relayState = LOW; | |
Serial.println("OFF7"); | |
} | |
delay(50); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == LOW){ | |
Q = readString; | |
if (Q=="on7"){ | |
digitalWrite(8,LOW); | |
relayState = HIGH; | |
Serial.println("ON7"); | |
} | |
delay(50); | |
} | |
if (Q=="read7"){ | |
Serial.println (digitalRead(8), DEC); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == HIGH){ | |
Q = readString; | |
if (Q=="off8"){ | |
digitalWrite(9,HIGH); | |
relayState = LOW; | |
Serial.println("OFF8"); | |
} | |
delay(50); | |
} | |
if((millis() - lastDebounceTime) > debounceDelay && relayState == LOW){ | |
Q = readString; | |
if (Q=="on8"){ | |
digitalWrite(9,LOW); | |
relayState = HIGH; | |
Serial.println("ON8"); | |
} | |
delay(50); | |
} | |
if (Q=="read8"){ | |
Serial.println (digitalRead(9), DEC); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment