Hello,
I need some help with my code. The sourcecode is based on the Garduino sketch written by: Ben Selkirk (JaminNZx).
I have a Wemos D1 mini with a shift register 74HC595 connected. At the shift register i have connected for now one led at position 1.
I have a Blynk project with a button at V1 (and V2 but not operational yet). The main goal is when i press V1 the led at postion 1 is turning on/off.
For test i have also a led connected directly to the Wemos. When i press V1 the led direct to the Wemos is working fine but the led at the shift register is turning on/off random.
For me that indicated the wiring is fine but it is something in the code.
I think i miss the final part.
/**************************************************************
Garduino
An Arduino Controlled Irrigation System using
Wifi connected moisture sensors and integrated
with the Blynk Mobile App for input and control.
Github: https://github.com/jaminNZx/Garduino-ESP-Blynk
Written by: Ben Selkirk (JaminNZx)
Official Blynk Support Forum:
http://community.blynk.cc/t/garduino-the-solar-wifi-sensor-driven-irrigation-system/11150
**************************************************************/
//#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); // Test led on Wemos pin
shiftOut(dataPin,clockPin,0xFF); // go to shift function value is register position. 0xFF is an array value. for test i use static values
Serial.println("V1 pressed");
}
BLYNK_WRITE(V2)
{
// Get the state of the VButton
relayVButton = param.asInt();
relayControl(relayVButton, 16); // Test led on Wemos pin
returnToShiftRegister(0x02); // go to shift function value is register position
Serial.println("V2 pressed");
}
void setup() {
//Binary notation as comment
dataArray[0] = 0xFF; //0b11111111
dataArray[1] = 0xFE; //0b11111110
dataArray[2] = 0xFC; //0b11111100
dataArray[3] = 0xF8; //0b11111000
dataArray[4] = 0xF0; //0b11110000
dataArray[5] = 0xE0; //0b11100000
dataArray[6] = 0xC0; //0b11000000
dataArray[7] = 0x80; //0b10000000
dataArray[8] = 0x00; //0b00000000
dataArray[9] = 0xE0; //0b11100000
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){
if(onoff == 1) // Virtual button set to on
{
digitalWrite(relayNumber, HIGH);
relayState = 1;
}
else
{
digitalWrite(relayNumber, LOW);
relayState = 0;
}
}
void returnToShiftRegister(int value)
{
// take the latchPin low so
// the LEDs don't change while you're sending in bits:
digitalWrite(latchPin, LOW);
// shift out the bits:
shiftOut(dataPin, clockPin, MSBFIRST, value); // (shift left)
//take the latch pin high so the LEDs will light up:
digitalWrite(latchPin, HIGH);
}
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
//internal function setup
int i=0;
int pinState;
//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
//cycle down through byte
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
//if the value passed to myDataOut and a bitmask result
// true then set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}
//stop shifting
digitalWrite(myClockPin, 0);
}