8 Channel Bluetooth Relay NEED HELP

hi, my name is jordy. i'm quiet new in arduino and only taught the basic of C++ from my old teacher. i have a physics project. i want to make a bluetooth controlable relay with arduino. i've tried some code for turning it on and off.

at first, it can't work with more than 2 relay. then, i found out that i forgot to use 'else if' statement, so i change it. after that, it work fine for controlling 3 relay. then, i decide to add all the other 5 relay. it failed. the arduino can't receive my code. i use:

q = Relay 1
w = Relay 2
e = Relay 3
r = Relay 4
t = Relay 5
y = Relay 6
u = Relay 7
i = Relay 8

it can only receive my 'q' input. i use 'else' to control relay 8. so this is my if statement:

q = Relay 1
w = Relay 2
e = Relay 3
r = Relay 4
t = Relay 5
y = Relay 6
u = Relay 7
else = Relay 8

everytime i input other alphabet beside 'q', the arduino read my input as 'else' so it's not w/e/r/t/y/u.

i attach my 3 relay code. can someone please check this code? what did i miss?
Thanks a lot for your answer. I really appreciate that.

int r1 = 2;
int r2 = 3;
int r3 = 4;
int r4 = 5;
int r5 = 6;
int r6 = 7;
int r7 = 8;
int r8 = 9;

void setup() {
Serial.begin(9600);
pinMode(r1,OUTPUT);
pinMode(r2,OUTPUT);
pinMode(r3,OUTPUT);
pinMode(r4,OUTPUT);
pinMode(r5,OUTPUT);
pinMode(r6,OUTPUT);
pinMode(r7,OUTPUT);
pinMode(r8,OUTPUT);
digitalWrite(r1,HIGH);
digitalWrite(r2,HIGH);
digitalWrite(r3,HIGH);
digitalWrite(r4,HIGH);
digitalWrite(r5,HIGH);
digitalWrite(r6,HIGH);
digitalWrite(r7,HIGH);
digitalWrite(r8,HIGH);
}

void loop() {
if (Serial.available()) {
delay(100);

while (Serial.available() > 0)
{
if((Serial.read())=='q')
{
if(digitalRead(r1)==HIGH)
{
digitalWrite(r1,LOW);
Serial.println("Turned ON!");
}
else
{
digitalWrite(r1,HIGH);
Serial.println("Turned OFF!");
}
}
else if((Serial.read())=='w')
{
if(digitalRead(r2)==HIGH)
{
digitalWrite(r2,LOW);
Serial.println("Turned ON!");
}
else
{
digitalWrite(r2,HIGH);
Serial.println("Turned OFF!");
}
}
else
{
if(digitalRead(r3)==HIGH)
{
digitalWrite(r3,LOW);
Serial.println("Turned ON!");
}
else
{
digitalWrite(r3,HIGH);
Serial.println("Turned OFF!");
}
}
}
{
}
}
}NN

BLUETOOTHH.ino (1.3 KB)

I'll explain what your problem appears to be - then you can make the changes to fix it, k?

So - you do a serial read - and check its value for "q" - well, that works ok - but what happens if you press another character?

Well - you again do the same - check for "q" (when you pressed "w") - but now your serial buffer is empty...

So - following the code - you ultimately will hit your "else" statement - because you have nothing left in your buffer after the first check!

To fix this, you need to perform your serial read and store the result in a variable - then inside each of your if statements, check the value of that variable and take action on the result.

Good luck. :smiley:

cr0sh:
I'll explain what your problem appears to be - then you can make the changes to fix it, k?

So - you do a serial read - and check its value for "q" - well, that works ok - but what happens if you press another character?

Well - you again do the same - check for "q" (when you pressed "w") - but now your serial buffer is empty...

So - following the code - you ultimately will hit your "else" statement - because you have nothing left in your buffer after the first check!

To fix this, you need to perform your serial read and store the result in a variable - then inside each of your if statements, check the value of that variable and take action on the result.

Good luck. :smiley:

well thanks. i understand now my problem. but how do i perform a serial read and store the result in a variable? thank you for your reply