[SOLVED] Button will not work... Sometimes

Ok, So I am working on a project that uses six DS18B20 temperature sensors. I am trying to incorporate a light into the program. I have all six temperature probes outputing their data to the blynk app with no issues, all six show up fine using the “value display” widget. But when I add a button widget and set it to D22 to turn on a LED on D22 it will not turn the LED on. But I can load the BlynkBlink sample program and it will turn the LED on D22 on… Im running this on an Arduino Mega 2560, and the app is on iPhone.

Here is the code I am running that works on everything but the light.

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2



OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";



void setup()
{
  sensors.begin();
  Serial.begin(9600);
  Blynk.begin(auth);
   
}


void loop()
{ 
  
  Blynk.run();
 
  sensors.requestTemperatures();
  Blynk.virtualWrite(0, sensors.getTempFByIndex(0));
  Blynk.virtualWrite(1, sensors.getTempFByIndex(1));
  Blynk.virtualWrite(2, sensors.getTempFByIndex(2));
  Blynk.virtualWrite(3, sensors.getTempFByIndex(3));
  Blynk.virtualWrite(4, sensors.getTempFByIndex(4));
  Blynk.virtualWrite(5, sensors.getTempFByIndex(5)); 
 
}

The code above will not turn on the LED But this code will, What is the conflict that I am having?

#define BLYNK_PRINT Serial 
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
}

void loop()
{
  Blynk.run();
}

I think you may be flooding the server with your virtualWrite calls being in the loop(). Use SimpleTimer and move the virtualWrites into a separate function that is called at a set interval.

Check out the PushData examples included in the blynk library to see how the timer is used.

Awesome thanks, Ill give it a look

Edit:
Changed the code as you suggested, I think I got it correct. Everything seems to be working now Thanks!
Here is what I changed the code to, did I use it correctly?

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
#include <SimpleTimer.h>


OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxx";

SimpleTimer timer;

void setup()
{
  sensors.begin();
  Serial.begin(9600);
  Blynk.begin(auth);
  timer.setInterval(1000L, sendUptime);
}

void sendUptime()
{
  sensors.requestTemperatures();
  Blynk.virtualWrite(0, sensors.getTempFByIndex(0));
  Blynk.virtualWrite(1, sensors.getTempFByIndex(1));
  Blynk.virtualWrite(2, sensors.getTempFByIndex(2));
  Blynk.virtualWrite(3, sensors.getTempFByIndex(3));
  Blynk.virtualWrite(4, sensors.getTempFByIndex(4));
  Blynk.virtualWrite(5, sensors.getTempFByIndex(5)); 
 
}

void loop()
{ 
  
  Blynk.run();
  timer.run(); 
 
}

That’s it! The main code of my 600 lines domotic stuff contain just those two lines in the void loop(). It should be enough to make everything run smoothly.

The timer object can also be instantiated multiple times, so you can actually run multiple timers and turn them on and off. I’d highly recommend looking at the documentation of the simpletimer library. It is a very nice and elegant thing to have and saves so much headaches… :smiley: