Newbie Needs More Help Please

Hi all,

I could use a little help please,

I have two virtual pins that are being set from a Blynk app (V7 & V8), I want them to be able to turn on outputs 7 & 8 respectively when “timeState” is false but this is obviously causing a conflict as “timeState” is turning the same outputs off.

The section that is causing me problems is Line 172 onwards,

Thanks in advance,

Mick.

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  WARNING!
    It's very tricky to get it working. Please read this article:
    http://help.blynk.cc/hardware-and-libraries/arduino/esp8266-with-at-firmware

  This example shows how value can be pushed from Arduino to
  the Blynk App.

  NOTE:
  BlynkTimer provides SimpleTimer functionality:
    http://playground.arduino.cc/Code/SimpleTimer

  App project setup:
    Value Display widget attached to Virtual Pin V5
 *************************************************************/

/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial
#include <LiquidCrystal.h> // include the LCD library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // control the LCD via Arduino pins 12, 11, 5, 4, 3 & 2
int timeState = 0; // ensure timer input is off on power up
int overRide = 0; // ensure the override input is off during power up
#include <ESP8266_Lib.h> // include the ESP8266 wifi chip library
#include <BlynkSimpleShieldEsp8266.h> include the Blinky wifi chip library
#include <TimeLib.h>
#include <WidgetRTC.h>
const int tempPin = A0; // create an integer called tempPin & asign it to Arduino analog input 1


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "?!?!>"; // authorisation token from Bylnk app

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "??????????"; // wifi network name
char pass[] = "!!!!!!!!!!!!"; // wifi network password

// Hardware Serial on Mega, Leonardo, Micro...
//#define EspSerial Serial1 // remove initial // if communicating with wifi chip via hardwired serial

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h> // communicate with wifi chip via software serial
SoftwareSerial EspSerial(A4, A5); // communicate with wifi chip using Arduino pins A4 (Rx) & A5 (Tx)
#include<SPI.h> // include library SPI, WidgetLED commands below don't work without this library
WidgetLED led1(V2); // asign led1 to Blynk virtual pin 2
WidgetLED led2(V3); // asign led2 to Blynk virtual pin 3

// Your ESP8266 baud rate:
#define ESP8266_BAUD 19200 // set wifi chip baud rate to 19,200

ESP8266 wifi(&EspSerial); // fuck knows

BlynkTimer timer;
WidgetRTC rtc;

void clockDisplay()
{
  // you can call hour(), minute(), ... at any time
  // please see Time library examples for details

  String currentTime = String(hour()) + ":" + minute() + ":" + second();
  String currentDate = String(day()) + " " + month() + " " + year();
  Serial.print("Current time: ");
  Serial.print(currentTime);
  Serial.print(" ");
  Serial.print(currentDate);
  Serial.println();
  // Send time to the App
  Blynk.virtualWrite(V4, currentTime);
  // Send date to the App
  Blynk.virtualWrite(V5, currentDate);
}
void blinkLedWidget() //
{
  if (digitalRead(7) == LOW) { // if digital output 7 is off
    led1.off(); // turn off led1 on Blynk app display
  } else {
    led1.on(); // if digital output 7 is on turn on led1 on the Blynk app display
  }
{ if (digitalRead(8) == LOW) { // if digital output 8 is off
      led2.off(); // turn off led2 on Blynk app display
    } else {
      led2.on(); // if digital output 8 is on  turn on led 2 on the Blynk app display
    }
  }
}
void myTimerEvent()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V1, millis() / 1000); // write to the Blynk app display every second via virtual pin 1
}
void temperature()
{
  int tempRaw = analogRead(tempPin);
  float temp = (tempRaw / 1024.0) * 5.0;
  int Temperature = (temp - .5) * 100; // convert the floating value (3 deciaml places) to an integer (0 decimal places) for the Blynk app display
  Blynk.virtualWrite(V0, Temperature); // write the value of Temperature to the Blynk app via virtual pin 0
}


void setup()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8442);
  //Begin synchronizing time
  rtc.begin();
  Blynk.run();
  // Setup a function to be called every second
  timer.setInterval(1000L, clockDisplay);
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(6000L, temperature);
  timer.setInterval(1000L, blinkLedWidget);
  pinMode(6, INPUT); // Pin 6 is a spare digital input (was for timer)
  pinMode(7, OUTPUT); // Pin 7 is output for heat lamp 1
  pinMode(8, OUTPUT); // Pin 8 is output for heat lamp 2
  pinMode(9, INPUT); // Pin 9 is for the override switch
  lcd.begin(16, 2);
}

void loop()
{
  int tempRaw = analogRead(tempPin); // tempRaw is unprocessed value of the analog input
  float temp = (tempRaw / 1024.0) * 5.0;
  int Temperature = (temp - .5) * 100; // convert the float value (3 decimal places) to an integer (0 decimal Places) for the LCD
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  overRide = digitalRead(9); // if the override switch is on
  if (overRide == HIGH) {
    lcd.setCursor(0, 0);
    lcd.print("System is in      "); // display this message on the LCD
    lcd.setCursor(0, 1);
    lcd.print("override!     ");
    digitalWrite(7, HIGH); // turn on heat lamp 1
    digitalWrite(8, HIGH); // turn on heat lamp 2
  }
  else { // if the the override swich is off run this script on both serial & LCD & execute the program
    lcd.setCursor(0, 0);
    lcd.print("Temperature is: ");
    lcd.setCursor(0, 1);
    lcd.print(Temperature);
    lcd.setCursor(2, 1);
    lcd.print(" Degrees");
    timeState = (hour() == 5 && minute() >= 30)  || (hour() >= 6 && hour() < 18);
    if (timeState == false) { // if it's night time
      digitalWrite(7, LOW); // Turn off heat lamp 1
      digitalWrite(8, LOW);
    }// Turn off heat lamp 2
    else { // if it's day time
      if (Temperature >= 30) { // if the temperatue is above 5 degrees
        digitalWrite(7, LOW); // turn off heat lamp 1
        digitalWrite(8, LOW); // turn off heat lamp 2
      }
      if (Temperature < 28) { // if the temperature is below 5 degrees
        digitalWrite(7, HIGH); // turn on heat lamp 1
        digitalWrite(8, LOW); // turn off heat lamp 2
      }
      if (Temperature < 27) { // if the temperature is below 1 degree
        digitalWrite(7, HIGH); // turn on heat lamp 1
        digitalWrite(8, HIGH); // turn on heat lamp 2
      }
    }


  }
}

Sorry. That code looks like crap. The usual practice is to put all the #include statements at the top, followed by some white space.

Then, declare the global variables, followed by some white space. Group like types together, such as all class instances, then all ints, then all floats.

It just makes it look like you know what you are doing, even when you don't.

ESP8266 wifi(&EspSerial); // fuck knows

Is that comment really necessary?

{ if (digitalRead(8) == LOW) { // if digital output 8 is off

{What's {that {first {curly {brace {for {?

It is much easier to compare the number of minutes since midnight that it is now to the number of minutes since midnight that an event is supposed to happen.

     if (Temperature >= 30) { // if the temperatue is above 5 degrees

When you are going to have useless comments, they MUST match the code. Otherwise, you don't look like the sharpest crayon in the box.

I don't see where Blynk does anything to make the physical pins that tempState is controlling turn on or off. Did I miss something?

If you're new to using Blynk (as your previous post implies), then you should start with a much simpler example.

Thanks for the critique chaps,

As you can tell I've never written code before but I am learning so your criticism is welcomed.