Arduino constantly going off line with simple AC dimmer sketch

Running this simple sketch and monitoring with the serial window the arduino goes off line about every 10-15 seconds and I cannot fathom out why. I have another sketch that is more complex and seems to work which I could use, but I would really like to know what I am doing wrong that is causing the problem for future reference.

All help greatly appreciated!!

Cheers Kev

sketch below


#define BLYNK_PRINT Serial 
#include <BlynkSimpleEthernet.h>

int AC_LOAD = 3;    // Output to Opto Triac pin
unsigned char dimming = 99;  // Dimming level (1-99) 99=off 1=on
char auth[] = "";

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth);
  pinMode(AC_LOAD, OUTPUT);// Set AC Load pin as output
  attachInterrupt(0, zero_crosss_int, RISING);  // zero cross interrupt pin 2 arduino
 }

BLYNK_WRITE(V21)// Slider in Blynk on V21
{
 int dim = param.asInt();
 dimming = dim;
 Serial.println(dimming);
 }

void zero_crosss_int()  //function to be fired at the zero crossing to dim the light
{
  // Firing angle calculation : 1 full 50Hz wave =1/50=20ms 
  // Every zerocrossing thus: (50Hz)-> 10ms (1/2 Cycle) 
  // For 60Hz => 8.33ms (10.000/120)
  // 10ms=10000us
  // (10000us - 10us) / 98 = 102 (Approx) For 60Hz =>65

  int dimtime = (102*dimming);    // For 60Hz =>65    
  delayMicroseconds(dimtime);    // Wait till firing the TRIAC
  digitalWrite(AC_LOAD, HIGH);   // Fire the TRIAC
  delayMicroseconds(10);         // triac On propogation delay (for 60Hz use 8.33)
  digitalWrite(AC_LOAD, LOW);    // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
}

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

post serial output

Hi Dave1829,

I didnt post the serial output as there is nothing to see. All that happens is the slider stops responding on blynk and it says its offline. Then on the serial interface you just see it connect to the server again until it goes offline again. and repeat ad infinitum!!

Cheers

Kev

Hi @newdos,
Check this post about my AC dimmer, I remember it was really stable…

Regards

Hi psoro,

I have seen this sketch and yes it does work fine. My query is, I want to know for future reference why my sketch causes the arduino to go offline all of the time, as I can’t see anything wrong with it???

I don’t think I am getting flood errors but obviously something isn’t quite right.

Thanks for your help

Cheers

Kev

1 Like

What is the hardware you are using?

arduino Uno R3 and ethernetshield W5100

Cheers

Kev

Not a good mix IMHO.
Have you thought about different hardware?
Perhaps send your data to Serial Monitor to be sure you don’t have any bugs i.e. dimtime is in the range you AND the hardware expects etc.

the sketch runs perfect with that hardware combi when blynk is not involved. Ie just the arduino an ethernetshield and replacing blynk with a pot on an analog pin

All my other blynk sketches also run perfect on that hardware combo. I can leave them running for days on end with no offline dropouts at all. Its only this one sketch that is causing me an issue.

So in a nutshell I dont believe it is the hardware

thanks again

EDIT forgot to say the sketch PSORO mentions above also runs perfectly on my hardware without issue so I think that pretty much rules out the hardware as well. I just want to kno what is causing the issue with that sketch!!

kev

#define BLYNK_PRINT Serial 
#include BlynkSimpleEthernet.h>

@newdos do you think a single define and a single include is enough?

Any reason you didn’t start with the W5100 sketch provided by Blynk and then add your stuff?

Hi Costas,

No, none at all. I lifted and changed this from another dimmer sketch. Are you saying I am missing something here ???

Cheers

Kev

Yes, compare Blynk’s W5100 sketch and yours.
It’s in the IDE but also shown below:

/**************************************************************
 * 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
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 *
 **************************************************************
 *
 * This example shows how to use Arduino Ethernet shield (W5100)
 * to connect your project to Blynk.
 * Feel free to apply it to any other example. It's simple!
 *
 * NOTE: Pins 10, 11, 12 and 13 are reserved for Ethernet module.
 *       DON'T use them in your sketch directly!
 *
 * WARNING: If you have an SD card, you may need to disable it
 *       by setting pin 4 to HIGH. Read more here:
 *       https://www.arduino.cc/en/Main/ArduinoEthernetShield
 *
 **************************************************************/

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "YourAuthToken";

#define W5100_CS  10
#define SDCARD_CS 4

void setup()
{
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  Serial.begin(9600);
  Blynk.begin(auth);
  // You can also specify server.
  // For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example
  //Blynk.begin(auth, "your_server.com", 8442);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8888);
}

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

ok I have had the SPI.h and ethernet.h libraries in my sketch and they made no difference. Why would the two define statements have any effect as this is the original code from the blynk getting started page that works just fine ???(shown below)

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

char auth[] = "YourAuthToken";

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth);  // Here your Arduino connects to the Blynk Cloud.
}

void loop()
{
  Blynk.run(); // All the Blynk Magic happens here...
}
Auth Token

Cheers again would love to get to the bottom of this!!!

Kev

My advice is to use the Blynk sketch for W5100 including disabling the SD card and add your bits.

Hi Costas,

Done that exactly as per sketch made no difference

here is a screenshot of the serial monitor as you can see takes for ever to connect then after a couple of goes of the slider it disconnects and then reconnects and so on…

have you disabled SD card.

2.5 minutes to get connected is not good and this suggests a problem at your side.

Is your slider on V21 set as ON for SEND ON RELEASE?
Do you have any other widgets in your project?

hi costas

thanks for your help here

yes disabled sd card yes no v21 set as off so fades as it slides.

still dont under stand though why the basic test sketch loads instantly and works perfect for weeks on end with out a singles disconnect - the ONLY difference is my code added to basic sketch???

tearing hair out!!!

cheers

kev

Probably your problem. Try it ON not OFF.

Haven’t tried this yet Costas but I have made a bit of a discovery. If I switch the mains of to the dimmer it stops crashing ??? the circuit is very standard and the only connection to the mains that involves the arduino is D2 which is interrupt 0, which in turn is read and does the zero crossing detect part of the circuit. SO, it would appear that it something to do with this??? Again at least we can tell it’s isolated to a bit of code reading pin 2 but why on earth does it matter if pin2 detects zero crossing or not??? I guess it’s because it wont be calling the zero_cross_int procedure as there is no power on so is it somethinh within this loop that is causing the issue ???

Cheers

kev

Does it still take 2.5 minutes to connect to Blynk with the dimmer power off?