I tried to make a program that can control a 4 channel relay (with LEDs as indicators) with a serial monitor. First time I tried to program it using a char
command to make it turn on only, it worked. Then I tried to make it off only, it also worked, but when I tried to combine them, to control it to On or Off in the same program, it didn't work.
this is my code
#define CH1 2
#define CH2 3
#define CH3 4
#define CH4 5
void setup(){
Serial.begin(9600);
pinMode(CH1, OUTPUT);
pinMode(CH2, OUTPUT);
pinMode(CH3, OUTPUT);
pinMode(CH4, OUTPUT);
digitalWrite(CH1,HIGH);
digitalWrite(CH2,HIGH);
digitalWrite(CH3,HIGH);
digitalWrite(CH4,HIGH);
}
void loop (){
if (Serial.available()) {
char ser = Serial.read();
switch(ser){
case 'on1':
onPin1();
break;
case 'on2':
onPin2();
break;
case'on3':
onPin3();
break;
case 'on4':
onPin4();
break;
case 'off1':
offPin1();
break;
case 'off2':
offPin2();
break;
case'off3':
offPin3();
break;
case 'off4':
offPin4();
break;
}
}
}
void onPin1(){
digitalWrite(CH1, LOW);
}
void onPin2(){
digitalWrite(CH2, LOW);
}
void onPin3(){
digitalWrite(CH3, LOW);
}
void onPin4(){
digitalWrite(CH4, LOW);
}
void offPin1(){
digitalWrite(CH1, HIGH);
}
void offPin2(){
digitalWrite(CH2, HIGH);
}
void offPin3(){
digitalWrite(CH3, HIGH);
}
void offPin4(){
digitalWrite(CH4, HIGH);
}
with that code, I got "duplicate value", maybe because the pin is double-called. But when I tried to make it in separate swicth case condition, no error but couldn't respond to the command char