My code doesn't open all four channels in relay module - arduino

I am making a Home automation. I have used arduino-uno, HC-05 bluetooth module and four channel relay module. I am currently stuck when I enter 'z' from my mobile my all channels of relay won't act as open switch.
I don't know why all the switches can't be opened simultaneously. Is it a problem of arduino or relay or the code ?
String inputs;
#define relay1 2 //Connect relay1 to pin 2
#define relay2 3 //Connect relay2 to pin 3
#define relay3 4 //Connect relay3 to pin 4
#define relay4 5 //Connect relay4 to pin 5
int val1=0;
int val2=0;
int val3=0;
int val4=0;
int val5=0;
void setup()
{
Serial.begin(9600); //Set rate for communicating with phone
pinMode(relay1, OUTPUT); //Set relay1 as an output
pinMode(relay2, OUTPUT); //Set relay2 as an output
pinMode(relay3, OUTPUT); //Set relay1 as an output
pinMode(relay4, OUTPUT); //Set relay2 as an output
digitalWrite(relay1, HIGH); //Switch relay1 off
digitalWrite(relay2, HIGH); //Swtich relay2 off
digitalWrite(relay3, HIGH); //Switch relay3 off
digitalWrite(relay4, HIGH); //Swtich relay4 off
}
void loop()
{
while(Serial.available()) //Check if there are available bytes to read
{
delay(10); //Delay to make it stable
char c = Serial.read(); //Conduct a serial read
if (c == '#'){
break; //Stop the loop once # is detected after a word
}
inputs += c; //Means inputs = inputs + c
}
if (inputs.length() >0)
{
Serial.println(inputs);
if(inputs == "a")
{
if(val1==0)
{
digitalWrite(relay1,LOW);
val1=1;
}
else
{
digitalWrite(relay1,HIGH);
val1=0;
}
delay(100);
}
else if(inputs == "b")
{
if(val2==0)
{
digitalWrite(relay2,LOW);
val2=1;
}
else
{
digitalWrite(relay2,HIGH);
val2=0;
}
delay(100);
}
else if(inputs == "c")
{
if(val3==0)
{
digitalWrite(relay3,LOW);
val3=1;
}
else
{
digitalWrite(relay3,HIGH);
val3=0;
}
delay(100);
}
else if(inputs == "d")
{
if(val4==0)
{
digitalWrite(relay4,LOW);
val4=1;
}
else
{
digitalWrite(relay4,HIGH);
val4=0;
}
delay(100);
}
else if(inputs == "z")
{
if(val5==0)
{
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
digitalWrite(relay3,LOW);
digitalWrite(relay4,LOW);
val5=1;
}
else
{
digitalWrite(relay1,HIGH);
digitalWrite(relay2,HIGH);
digitalWrite(relay3,HIGH);
digitalWrite(relay4,HIGH);
val5=0;
}
delay(100);
}
inputs="";
}
}

Many times relays use much current. And arduino has limited current capacity maybe these is the reason why they can't be opened simultaneously.

Related

Arduino Uno and HC-05: Not showing any output on serial monitor

Arduino Uno - HC-05
Connections are: TX-RX, RX-TX, LED-D13, 5V-5V+
For this project, we can supply power to the Arduino through any +5V power source. You can use a USB port from your computer to power the Arduino, but in this project I used my laptop.
while (Serial.available()) is returning 0 and Serial.read() is returning -1.
Need Help!
Used Bluetooth voice recognition tool from playstore-"Arduino Voice Control"
#include <SoftwareSerial.h> //Replace (' ') with (< >)
SoftwareSerial BLU(0,1);
String voice;
int Green = 13; //Connect To Pin #13
//int Yellow = 2; //Connect To Pin #2
//int Red = 3; //Connect To Pin #3
void allon() {
//digitalWrite(Red, HIGH);
//digitalWrite(Yellow, HIGH);
Serial.print("start");
digitalWrite(Green, HIGH);
}
void alloff() {
//digitalWrite(Red, LOW);
//digitalWrite(Yellow, LOW);
digitalWrite(Green, LOW);
}
void setup() {
Serial.begin(9600);
BLU.begin(9600);
//pinMode(Red, OUTPUT);
//pinMode(Yellow, OUTPUT);
pinMode(Green, OUTPUT);
}
void loop() {
//Serial.print("start loop");
//Serial.print(Serial.available());
while (Serial.available()) { //Check if there is an available byte to read
//Serial.print("start");
delay(10); //Delay added to make thing stable
char c = Serial.read(); //Conduct a serial read
//Serial.print(Serial.read());
if (c == '#') {
break; //Exit the loop when the # is detected after the word
}
//Serial.print(c);
voice += c;
//Serial.print(voice+"\n");
}
if (voice.length() > 0) {
Serial.print("Start");
Serial.print(voice);
if (voice == "*turn on all LED") {
allon();
}
else if (voice == "*turn off all LED") {
alloff();
}
/*else if(voice == "*switch on red") {
digitalWrite(Red,HIGH);
}
else if(voice == "*switch on yellow") {
digitalWrite(Yellow,HIGH);
}*/
else if(voice == "*switch on green") {
digitalWrite(Green,HIGH);
}
/*else if(voice == "*switch off red") {
digitalWrite(Red,LOW);
}
else if(voice == "*switch off yellow") {
digitalWrite(Yellow,LOW);
}*/
else if(voice == "*switch off green") {
digitalWrite(Green,LOW);
}
voice=""; //Reset variable
}
}
You need to check for the app output first. If you already know it then mention that in comment otherwise do the following for printing app output first:-
const int LED = 5;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
while(Serial.available()>0){
switchstate = Serial.read();
Serial.print(switchstate); // First check what output are you getting from the application
Serial.print("\n");
delay(15);
if(switchstate == '1'){ // Compare your app output accordingly
digitalWrite(5, HIGH);
}
else if(switchstate == '0'){
digitalWrite(5, LOW);
}
}
}

Master - Slave using SPI communication (Tinkercad)

Need your help again: I'm doing this time Master - Slave Using SPI communication, there is no error in the code when I simulate the code but the LED won't turn on.
The supposed outcome that should happen is that when I push the push button on master board the LED on the slave board will turn on.
Master code:
// Master Board
#include <SPI.h>
#define button1 4
#define SS 10
int buttonvalue;
int x;
void setup(void) {
Serial.begin(115200); //set baud rate to 115200 for usart
digitalWrite(SS, HIGH); // disable Slave Select
SPI.begin ();
SPI.setClockDivider(SPI_CLOCK_DIV8); //divide the clock by 8
}
void loop(void) {
digitalWrite(SS, LOW);
buttonvalue = digitalRead(button1);
if (buttonvalue == HIGH) {
x = 1;
} else {
x = 0;
}
digitalWrite(SS, HIGH);
delay(1000);
}
Slave code:
// Slave Board
#include <SPI.h>
#define led1 2
volatile byte Slavereceived;
volatile boolean received;
int x;
void setup(void) {
Serial.begin(115200);
pinMode(2, OUTPUT);
pinMode(MISO,OUTPUT);
SPCR |= _BV(SPE);
received = false;
SPI.attachInterrupt();
}
ISR (SPI_STC_vect) {
Slavereceived = SPDR;
received = true;
}
void loop() {
if (received) {
if (Slavereceived == 1) {
digitalWrite(led1, HIGH);
} else {
digitalWrite(led1, LOW);
}
delay(1000);
}
}
I too was stuck in the same situation, there is no support for the SPI library in tinkercad, you can include it without errors, and even use it, but any useful command will let the code stuck at that command
Sorry, but there no much you can do
this link if for a tinkercad forum, where one of the people said SPI library amoung two others are not supported
Add SPI.transfer(x); below the if else to your master code.
The master code will look somewhat like this:
// Master Board
#include <SPI.h>
#define button1 4
#define SS 10
int buttonvalue;
int x;
void setup(void) {
Serial.begin(115200); //set baud rate to 115200 for usart
digitalWrite(SS, HIGH); // disable Slave Select
SPI.begin ();
SPI.setClockDivider(SPI_CLOCK_DIV8); //divide the clock by 8
}
void loop(void) {
digitalWrite(SS, LOW);
buttonvalue = digitalRead(button1);
if (buttonvalue == HIGH) {
x = 1;
} else {
x = 0;
}
SPI.transfer(x);
digitalWrite(SS, HIGH);
delay(1000);
}

How to merge two arduino programs on Bluetooth and Fingerprint?

Bluetooth and fingerprint are not responding simultaneously.
Bluetooth is used for controlling "Find me alarm" and Fingerprint is used to control the open and close motors.
The bluetooth is used to trigger the buzzer to make a buzzing sound. The fingerprint is used to control the four motors which is used to open and close the door
How to merge these two programs?
Fingerprint :
#include <FPS_GT511C3.h>
#include <SoftwareSerial.h>
FPS_GT511C3 fps(5, 4);
int buttonstate = 0;
void setup()
{
Serial.begin(9600);
delay(100);
fps.Open();
fps.SetLED(true);
pinMode(8,OUTPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(13,INPUT);
pinMode(16,OUTPUT);
pinMode(17,OUTPUT);
pinMode(18,OUTPUT);
pinMode(19,OUTPUT);
}
void loop()
{
buttonstate=digitalRead(13);
if (fps.IsPressFinger())
{
fps.CaptureFinger(false);
int id = fps.Identify1_N();
if (id <200)
{
Serial.print("Verified ID:");
Serial.println(id);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
delay(1000);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(100);
digitalWrite(16,HIGH);
digitalWrite(17,LOW);
digitalWrite(18,HIGH);
digitalWrite(19,LOW);
}
else
{
Serial.println("Finger not found");
}
}
else
{
Serial.println("Please press finger");
}
if(buttonstate == HIGH)
{
digitalWrite(8,LOW);
digitalWrite(9,HIGH);
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
delay(1000);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
digitalWrite(10,LOW);
digitalWrite(11,LOW);
delay(100);
digitalWrite(16,LOW);
digitalWrite(17,LOW);
digitalWrite(18,LOW);
digitalWrite(19,LOW);
}
delay(100);
}
Bluetooth :
#include<SoftwareSerial.h>
SoftwareSerial BT(14,15);
String readdatta;
void setup() {
BT.begin(9600);
Serial.begin(9600);
pinMode(12,OUTPUT);
}
void loop() {
char ch;
while(ch!=0)
{
ch=BT.read();
delay(100);
if(ch == '1')
{
digitalWrite(12,HIGH);
}
else
{
digitalWrite(12,HIGH);
}
}
ch = ' ';
}

Arduino wifi > firebase and more than 1 LED control

Can you please help me with below code. It works with 1 LED but not wit 2 LEDs / pins. On pin 3 works, but not on pin 5. Any suggestions?
I tried many different things but am stuck. Something isn't right inside loop. I think that will help many persons here on forum.
Snippet correction would be great or just tell me what to do for solving that problem.
Arduino code:
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
// Set these to run example.
#define FIREBASE_HOST "test839785093353.firebaseio.com"
#define FIREBASE_AUTH "685g4d65d4g65d4g654TESTsf354s6f531sf531s"
//Change line with your WiFi router name and password
#define WIFI_SSID "Internet"
#define WIFI_PASSWORD "password123123"
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
// connect to wifi.
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected: ");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
// ob povezavi inicializiras status
Firebase.set("LED_STATUS", 0);
Firebase.set("LED2_STATUS", 0);
}
int led1 = 0;
int led2 = 0;
// int led3 = 0;
void loop() {
// get value
led1 = Firebase.getInt("LED_STATUS");
led2 = Firebase.getInt("LED2_STATUS");
// handle error
if (led1==1) {
Serial.println("LED 1 ON");
digitalWrite(3,HIGH);
return;
delay(10);
}
else {
Serial.println("LED 1 OFF");
digitalWrite(3,LOW);
return;
}
// se en if..else stavek
if (led2==1) {
Serial.println("LED 2 ON");
digitalWrite(5,HIGH);
return;
delay(10);
}
else {
Serial.println("LED 2 OFF");
digitalWrite(5,LOW);
return;
}
}
The reason for this is the if else seatement that you have used for LED1.
If led1 is 1, it turns on led1 and exits the loop() function.
If led1 is 0, it turns off led1 and exits the loop() function.
The code never reaches led2.
Try this change
if (led1==1) {
Serial.println("LED 1 ON");
digitalWrite(3,HIGH);
delay(10);
}
else {
Serial.println("LED 1 OFF");
digitalWrite(3,LOW);
}
// se en if..else stavek
if (led2==1) {
Serial.println("LED 2 ON");
digitalWrite(5,HIGH);
delay(10);
}
else {
Serial.println("LED 2 OFF");
digitalWrite(5,LOW);
}

ARDUINO : Hold Values & Serial monitor

i would like to ask .
I am doing LED project using serial monitor.
if key for serial monitor 1 is pressed one time , LED 1 will light up until i pressed the 2nd key for serial monitor 2 and LED 2 will light up .
how can i do that?
by delay() ? or hold () ?
You can't use delay() because the time taken until next the next keypress cannot be determined.
You can use flags, as demonstrated below
int ledPin1 = 8, ledPin2 = 9;
boolean flag1, flag2;
void setup()
{
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop()
{
if(Serial.available())
{
int c = Serial.read();
if(c == 49) { // 1 is pressed
flag1 = true;
flag2 = false;
}
else if(c == 50) { // 2 is pressed
flag2 = true;
flag1 = false;
}
}
if(flag1) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
}
if(flag2) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
}
delay(50);
}
Hope this helps.

Resources