Its driving me crazy for some couple of days now and i don't get the answer. I'm busy to setup a irrigation system with a shift register 74HC595 and a 8 channel relay board. I see a lot of sketches where it looks like you can turn a specific relay on but i dont get the clue.
I like to turn on a specific relay based on a button press. Is it possible to turn on a specific relay based on the representing button?
Im using the shiftOut option. The code im using now is :
//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <ESP_SSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility -> https://github.com/somhi/ESP_SSD1306
#include <Adafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file) --> https://github.com/adafruit/Adafruit-GFX-Library
#include <SPI.h> // For SPI comm (needed for not getting compile error)
#include "settings_base.h"
#include "globals.h"
//#include "functions.h"
//#include "blynk_writes.h"
//Pin connected to ST_CP of 74HC595
const int latchPin = 15; //D8
//Pin connected to SH_CP of 74HC595
const int clockPin = 14; //D5
//Pin connected to DS of 74HC595
const int dataPin = 13; // D7
int totalRelays = 2; // Number of relays
int relayState = 0; // default state = off
int relayArr[] = {12, 16}; // Pins on the Wemos
char vbutton1 = V1; char vbutton2 = V2;
char relayVButtons[] = {V1,V2};
int zone = 0;
int relayVButton = 0;
byte data;
byte dataArray[10];
/*
SETUP
*/
BLYNK_WRITE(V1)
{
// Get the state of the VButton
relayVButton = param.asInt();
relayControl(relayVButton, 12, 49); // Test led on Wemos pin
//int bitToSet = 49;
Serial.println("V1 pressed");
}
BLYNK_WRITE(V2)
{
// Get the state of the VButton
relayVButton = param.asInt();
relayControl(relayVButton, 16, 50); // Test led on Wemos pin
Serial.println("V2 pressed");
}
void setup() {
pinMode(dataPin, OUTPUT); // All 3 pins are output
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
// Do some loop through all the relays and buttons for pinMode
for (int i=0; i<=totalRelays; i++){
pinMode(relayArr[i], OUTPUT);
digitalWrite(relayArr[i], 0);
pinMode(relayVButtons[i], INPUT);
}
// COMMUNICATIONS
Serial.begin(115200);
WiFi.mode(WIFI_STA);
// CONNECT TO BLYNK
Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS);
while (Blynk.connect() == false) {}
// OVER THE AIR UPDATES
ArduinoOTA.setHostname(OTA_HOSTNAME);
ArduinoOTA.begin();
Serial.println(F("Blynk v" BLYNK_VERSION ": Device started"));
}
/*
LOOP
*/
void loop() {
Blynk.run();
ArduinoOTA.handle();
timer.run();
}
void relayControl(int onoff, int relayNumber, int bitToSet){
if(onoff == 1) // Virtual button set to on
{
digitalWrite(relayNumber, HIGH); // set Wemos pin on HIGH (for test)
registerWrite(bitToSet, HIGH);
relayState = 1;
}
else
{
digitalWrite(relayNumber, LOW); // set Wemos pin on HIGH (for test)
registerWrite(bitToSet, LOW);
relayState = 0;
}
}
void registerWrite(int whichPin, int whichState) {
// the bits you want to send
byte bitsToSend = 0;
// turn off the output so the pins don't light up
// while you're shifting bits:
digitalWrite(latchPin, LOW);
// turn on the next highest bit in bitsToSend:
bitWrite(bitsToSend, whichPin, whichState);
// shift the bits out:
shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
// turn on the output so the LEDs can light up:
digitalWrite(latchPin, HIGH);
}
Sander
i've change the code. Now the result is the led is turning on but only one led. After pressing led 2 the other one is turning on.. Im not able to turn them off.
//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <ESP_SSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility -> https://github.com/somhi/ESP_SSD1306
#include <Adafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file) --> https://github.com/adafruit/Adafruit-GFX-Library
#include <SPI.h> // For SPI comm (needed for not getting compile error)
#include "settings_base.h"
#include "globals.h"
//#include "functions.h"
//#include "blynk_writes.h"
//Pin connected to ST_CP of 74HC595
const int latchPin = 15; //D8
//Pin connected to SH_CP of 74HC595
const int clockPin = 14; //D5
//Pin connected to DS of 74HC595
const int dataPin = 13; // D7
int totalRelays = 2; // Number of relays
int relayState = 0; // default state = off
int relayArr[] = {12, 16}; // Pins on the Wemos
char vbutton1 = V1; char vbutton2 = V2;
char relayVButtons[] = {V1,V2};
int zone = 0;
int relayVButton = 0;
/*
SETUP
*/
BLYNK_WRITE(V1)
{
// Get the state of the VButton
relayVButton = param.asInt();
relayControl(relayVButton, 12, 1); // Test led on Wemos pin
//int bitToSet = 49;
Serial.println("V1 pressed");
}
BLYNK_WRITE(V2)
{
// Get the state of the VButton
relayVButton = param.asInt();
relayControl(relayVButton, 16, 4); // Test led on Wemos pin
Serial.println("V2 pressed");
}
void setup() {
pinMode(dataPin, OUTPUT); // All 3 pins are output
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
// Do some loop through all the relays and buttons for pinMode
for (int i=0; i<=totalRelays; i++){
pinMode(relayArr[i], OUTPUT);
digitalWrite(relayArr[i], 0);
pinMode(relayVButtons[i], INPUT);
}
// COMMUNICATIONS
Serial.begin(115200);
WiFi.mode(WIFI_STA);
// CONNECT TO BLYNK
Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS);
while (Blynk.connect() == false) {}
// OVER THE AIR UPDATES
ArduinoOTA.setHostname(OTA_HOSTNAME);
ArduinoOTA.begin();
Serial.println(F("Blynk v" BLYNK_VERSION ": Device started"));
}
/*
LOOP
*/
void loop() {
Blynk.run();
ArduinoOTA.handle();
timer.run();
}
void relayControl(int onoff, int relayNumber, int bitToSet){
if(onoff == 1) // Virtual button set to on
{
digitalWrite(relayNumber, HIGH); // set Wemos pin on HIGH (for test)
registerWrite(bitToSet);
Serial.println(bitToSet);
relayState = 1;
}
else
{
digitalWrite(relayNumber, LOW); // set Wemos pin on HIGH (for test)
registerWrite(bitToSet);
relayState = 0;
}
}
void registerWrite(int whichPin) {
// the bits you want to send
uint8_t data = 1<<whichPin ;
// turn off the output so the pins don't light up
// while you're shifting bits:
digitalWrite(latchPin, LOW);
// shift the bits out:
shiftOut(dataPin, clockPin, MSBFIRST, data);
// turn on the output so the LEDs can light up:
digitalWrite(latchPin, HIGH);
}
Im using the shiftOut option. The code im using now is :
//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
#include <ESP_SSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility -> https://github.com/somhi/ESP_SSD1306
#include <Adafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file) --> https://github.com/adafruit/Adafruit-GFX-Library
#include <SPI.h> // For SPI comm (needed for not getting compile error)
#include "settings_base.h"
#include "globals.h"
//#include "functions.h"
//#include "blynk_writes.h"
//Pin connected to ST_CP of 74HC595
const int latchPin = 15; //D8
//Pin connected to SH_CP of 74HC595
const int clockPin = 14; //D5
//Pin connected to DS of 74HC595
const int dataPin = 13; // D7
int totalRelays = 2; // Number of relays
int relayState = 0; // default state = off
int relayArr[] = {12, 16}; // Pins on the Wemos
char vbutton1 = V1; char vbutton2 = V2;
char relayVButtons[] = {V1,V2};
int zone = 0;
int relayVButton = 0;
byte data;
byte dataArray[10];
/*
SETUP
*/
BLYNK_WRITE(V1)
{
// Get the state of the VButton
relayVButton = param.asInt();
relayControl(relayVButton, 12, 49); // Test led on Wemos pin
//int bitToSet = 49;
Serial.println("V1 pressed");
}
BLYNK_WRITE(V2)
{
// Get the state of the VButton
relayVButton = param.asInt();
relayControl(relayVButton, 16, 50); // Test led on Wemos pin
Serial.println("V2 pressed");
}
void setup() {
pinMode(dataPin, OUTPUT); // All 3 pins are output
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
// Do some loop through all the relays and buttons for pinMode
for (int i=0; i<=totalRelays; i++){
pinMode(relayArr[i], OUTPUT);
digitalWrite(relayArr[i], 0);
pinMode(relayVButtons[i], INPUT);
}
// COMMUNICATIONS
Serial.begin(115200);
WiFi.mode(WIFI_STA);
// CONNECT TO BLYNK
Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS);
while (Blynk.connect() == false) {}
// OVER THE AIR UPDATES
ArduinoOTA.setHostname(OTA_HOSTNAME);
ArduinoOTA.begin();
Serial.println(F("Blynk v" BLYNK_VERSION ": Device started"));
}
/*
LOOP
*/
void loop() {
Blynk.run();
ArduinoOTA.handle();
timer.run();
}
void relayControl(int onoff, int relayNumber, int bitToSet){
if(onoff == 1) // Virtual button set to on
{
digitalWrite(relayNumber, HIGH); // set Wemos pin on HIGH (for test)
registerWrite(bitToSet, HIGH);
relayState = 1;
}
else
{
digitalWrite(relayNumber, LOW); // set Wemos pin on HIGH (for test)
registerWrite(bitToSet, LOW);
relayState = 0;
}
}
void registerWrite(int whichPin, int whichState) {
// the bits you want to send
byte bitsToSend = 0;
// turn off the output so the pins don't light up
// while you're shifting bits:
digitalWrite(latchPin, LOW);
// turn on the next highest bit in bitsToSend:
bitWrite(bitsToSend, whichPin, whichState);
// shift the bits out:
shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
// turn on the output so the LEDs can light up:
digitalWrite(latchPin, HIGH);
}
shiftOut()
function, or SPI, or something else?