Arduino constantly going off line with simple AC dimmer sketch

yes it does and so does the blynk test sketch but both now stay online once connected???

I have also been noticing similar issues with involving delayMicroseconds() to control a servo (also on an UNO R3) while using Blynk… Where the same sketch works perfectly outside of Blynk. I came up with a band-aid fix using Blynk.syncVirtual() but that only worked if the delayMicroseconds() call was in a Blynk Loop… and it brought other issues, so not really recommended :wink:

As my Ethernet shield died awhile ago, I have been using Blynk’s USB link, and thought that might have been part of the issue, but now I think not so much :slight_smile:

Basically I suspect delayMicroseconds() might be getting interrupted at times due internal Blynk timing.

Also, while you don’t appear to be using the analog pins, a known issue with some Ethernet shield clones was that the REF pin was shorted to either ground or 5V and thus caused unreliable analog reads. I had solved that issue by just bending the REF pin out of the way.

We use delayMicroseconds() with our Blynk connected 433MHz RF transmitters. Never had a problem but we don’t use Arduinos.

Exactly… may be limited to the core board libraries.

There were timer or interrupt conflicts that have since been resolved as of Arduino 0018, but who knows what else lurks deep in the core :slight_smile:

Either way, there seems to be a clear issue involveing Arduino (at least the UNO so far) and delayMicroseconds() that cause timing/dropout issues in Blynk. Forgoing use of all arduinos for ESP’s may be a solution (hint taken :wink: ) but not ideal for everyone.

Ok… Let’s go
Test this code with your hardware (Uno R3 and ethernet shield W5100)

#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include  <TimerOne.h>          // Avaiable from http://www.arduino.cc/playground/Code/Timer1
volatile int i=0;               // Variable to use as a counter volatile as it is in an interrupt
volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 3;                // Output to Opto Triac
int brightness = 128;           // Dimming level (0-128)  0 = on, 128 = 0ff
int freqStep = 75;    // This is the delay-per-brightness step in microseconds.

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

#define W5100_CS  10
#define SDCARD_CS 4


void setup()
{
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
  attachInterrupt(0, zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) 
  Timer1.initialize(freqStep);                      // Initialize TimerOne library 
  Timer1.attachInterrupt(dim_check, freqStep);   
  
  
  Serial.begin(9600);
  Blynk.begin(auth);

}

BLYNK_WRITE(V1)//      slider brillo
{
 int brillo = param.asInt(); 
 brightness=brillo;
}

void zero_cross_detect() {    
  zero_cross = true;               
  i=0;
  digitalWrite(AC_pin, LOW);       // turn off TRIAC (and AC)
}
// Turn on the TRIAC at the appropriate time
void dim_check() {                   
  if(zero_cross == true) {              
    if(i>=brightness) {                     
      digitalWrite(AC_pin, HIGH); // turn on light       
      i=0;  // reset time step counter                         
      zero_cross = false; //reset zero cross detection
    } 
    else {
      i++; // increment time step counter                     
    }                                
  }                                  
}                               


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

Serial output:

[0] Getting IP...
[6484] IP:xxx.xxx.x.xx
[6486] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.4 on Arduino Uno

[6596] Connecting to xxx.xxx.xxx.xxx
[6838] Ready (ping: 4ms).

Dashboard: http://tinyurl.com/zn8rqyg

It works like a charm…

OK, blond moment here… as I left my TRIAC board in my other jacket pocket, I was going to monitor the output on my DSO (AKA little toy oscilloscope)… and was getting very strange readings until it dawned on me what zero-crossing detection meant (Ah Hah! those YouTube videos I watch all the time are starting to pay off).

So, no Ethernet shield, no TRIAC and no sine wave generator… I am out of this game :stuck_out_tongue:

But while digging through the code and looking at the picture, I notice you have something going to PIN 2 and I am guessing that is the actual interrupt pin? or is Pin 3 actually used for both functions? (I don’t fully understand how to use interrupts yet - no judging :slight_smile: )

Oh… Sorry, it’s Pin 2 in the Arduino Uno, the original code is for a Pro Micro board and for the Pro Micro the Int 0 is Pin 3…

Comment changed at the other post!

Thanks @Gunner!

Hi Psoro,

I don’t need to try this code as I know it works perfectly on my setup(which is what I stated in my original post that I had another slightly more complex sketch that works fine). I will resort to this if all else fails, but the point of my post was to find the issue that is causing this problem so I can avoid it in future and help others.

Out of interest which boards do you tend to use for these types of applications with Blynk

Cheers Kev

PS sorry for the late reply - it wouldnt let me reply for 2 hours as it was my first day being a newbie and all that lol!!!

That’s fine… the point was clear… and to be honest I didn’t pay too much attention to your code as, according your comments, it doesn’t work properly…That’s why I offered you the piece of code that works fine with or without Blynk…
I prefer to play with stuff that works and change it according my project doing it bigger and bigger instead of keep struggling with faulty code…

I used to play with Arduino and the ESP-01 as shield at the beginning and, after several time, I moved to ESP’s like WeMos Mini D1 or NodeMCU… A lot of fun here with Blynk.

Kind regards!

I agree… sometimes finding the root issue is just as important as an finding an alternative way.

I think it just comes down to the angle of the tongue at times :slight_smile: … That and competing and/or conflicting timer/interrupt issues withing Blynk libraries and Aduino core… at least when using certain methods (and apparently UNO’s :wink: ) that were not really intended for most IoT applications; of which Blynk is.

Yeah agree Gunner and thanks to Psoro for all of his help. I guess it is down the delay and issue and I will just avoid those in future. I did do a lot of reading today on the arduino site and it does mention a lot of issues with interrupts and delays, and as you pointed out is this is fixed in 018 onwards.

I will park it there guys. Thanks for all your help and lets hope it does get fixed good and proper some day!!

Cheers

kev

1 Like