Need help with complete my code.

my idea is to be able to open and close 8 relays threw my surftablet in Blynk.
I'm not a programmer and not good at it. Thats why I ask you for help :slight_smile:

for now i can only open one relay with the following code,

int state = 0;
int incomingByte;

void setup(){
pinMode(12, OUTPUT);
Serial.begin(9600);
}
void loop(){
if(Serial.available()>0){
incomingByte = Serial.read();
if(incomingByte=='1'){
digitalWrite(12, HIGH);
delay(0);
Serial.println("ON");
state=1;
}
else if(incomingByte=='2'){
digitalWrite(12,LOW);
delay(0);
Serial.println("OFF");
state=0;
}
}
}

would be very grateful if someone could tell me how I should continue with the code :slight_smile:

thanks!!

First, please read the post at the top of the Forum by Nick Gammon on how to use this Forum, especially the use of code tags when posting code. Second, use Ctrl-T in the IDE to reformat your code before posting. Third, take a look on how to use the switch/case statement block, which is a better way to approach your solution than a cascading if/else statement block.

  delay(0);I'd lose those.

You should look into using arrays and loops as they will vastly reduce the amount of code I believe you're about to write.

You could also probably use serial values 0 - 7 to 'select' the relay being acted upon with 'f' or 't' as off vs on!

how I should continue with the code

You should NOT continue with the code until you have some requirements. If you do have requirements, it does not help if you ask us how to write code to implement them without you having shared them.