Hello all,
I am currently working on a project that uses relays, a little different than the one found in your article here. http://waihung.net/bluetooth-controlled-relay/
I've attached a picture below of both the relay module and the bluetooth (JY-MCU HC 05). I am using the Arduino Uno as of now and the IN1 and IN2 from the relay module are in pins 6 and 7 respectfully. I modified the code that was provided in order to control 2 relays instead of one. I've attached the code also.
As of now, I can get the IN1 connected relay to turn on and off but not the 2nd relay, I am wondering what I am missing in my code that would make this work.
Additionally, I had to split the wires leading from both the relay module and the bluetooth module in order to connect both to the 5V pin on the Arduino. Is there a better way to do this? because both need a source of power.
Thanks!
#include <SoftwareSerial.h>
SoftwareSerial mySerial(6,7);
//----Global variables for the pins---//
int pin1 = 6;
int pin2 = 7;
//Initialise for the Arduino setup for OUTPUT to the pins designated as ints
void setup()
{
Serial.begin(9600);
pinMode(pin1,OUTPUT);
pinMode(pin2,OUTPUT);
digitalWrite(pin1,LOW);
digitalWrite(pin2,LOW);
}
void loop()
{
if (Serial.available()) {
delay(100);
while(Serial.available() > 0){
{
if((Serial.read())=='1')
{
if(digitalRead(pin1)==LOW)
{
digitalWrite(pin1,HIGH);
Serial.println("TURNED OFF Relay 1");
}
else
{
digitalWrite(pin1,LOW);
Serial.println("TURNED ON Relay 1");
}
if((Serial.read())=='2')
{
if(digitalRead(pin2)==LOW)
{
digitalWrite(pin2,HIGH);
Serial.println("Turned OFF Relay 2");
}
else
{
digitalWrite(pin2,LOW);
Serial.println("Turned ON Relay 2");
}
}
}
}
}
}
}