ArduinoGeneral

DFRduino M0 Mainboard Issues with delay(), mills() and maybe other functions!

userHead jacob.kamminga 2018-01-12 03:08:50 2815 Views4 Replies
Hi
I bought the DFRDuino M0 mainboard and Wifi Bee ESP V1.0 to work on a project to control a robot arm.

I first tested the Wifi Bee ESP according to the example in the products WiKi on an Arduino UNO, which worked fine.
When I the tried to replicate it using the DFRDuino M0 mainboard I got in to trouble. The board did not seeming the work.
I created a Heartbeat library using a class in other projects to blink a led using millis(), just to indicate the loop is executing.
When I used the Library on the DFRDuino M0 It did't work, no blinking HeartBeat Led.
Changed the board to an UNO and downloaded the exact same code and yes my Heartbeat was blinking. Is my M0 board a DOA?
I then went on to add another LED on the M0 board using the ordinary delay() function as in the Blink example. Surprise surprise I had a blinking LED. So my M0 board was working.
Now I copied the class and definition in the main file to see if it was a Linker issue on the M0 Board. Result NO Blinking Heartbeat LED.
Downloaded it to the UNO board both leds blinking!

So some further trouble shooting. I removed the Class and copied the exact code in the Sketch on the M0 Board and again surprise surprise both LEDs are blinking.

What is going on here?

So with that knowledge went on to examine the EDS8266 example code placed serial.print statements and came to the point that the code inside the DFRobot EDS8266 library stopped at a delay(xx) statement.
So I circumvented the delay(xx) statements at various places in the code by creating delays, using a for loop.
Result --> I got the Wifi Bee ESP V1.0 working.

Not A stable environment.

Can you please advise, is this a malfunctioning M0 board?

I could not attach files to this message (get the error code: wrong extension!).
So I add the source files in additional comments!
2018-01-12 03:44:16 Last are code snippets from the adapted ESP8266 Library

Created a methode nop.

void Esp8266::nop (unsigned int timeout)
{
for (int i ; i < timeout ; i++);
}

here the adapted code for checkMode:

char Esp8266::checkMode() {
//Serial.println ("\n_cM1 start");
clearBuf();
//Serial.println ("_cM2 buf cleared");
write ("AT+CWMODE?");
//delay(200); //this causes a freeze
nop (2000); // This delays and doesn't cause a freeze
//Serial.println ("_cM3 AT mode requested");
String str = readData();
//Serial.println("_cM4 String is: " + str);
if (str.indexOf('1') > 0 )
return '1';
else if (str.indexOf('2') > 0)
return '2';
else if (str.indexOf('3') > 0)
return '3';
else
return '0';
}

And here the adapted code for setMux:

bool Esp8266::setMux (int flag) {
const int timeout = 1000;

//Serial.println ("\n_setMux1 start");
String str;
clearBuf();
//Serial.println ("_setMux2 After clearbuf");
write ("AT+CIPMUX="+String(flag));
//Serial.println ("_setMux3 After write");

for (int i ; i < timeout ; i++);

//delay (100);

//Serial.println ("_setMux4 Before ReadData");
str = readData ();
//Serial.println ("_setMux5 String is: " + str);


if (str.indexOf("OK")>0 || str.indexOf("link is builded")>0)
return true;
else
return false;
}
userHeadPic jacob.kamminga
2018-01-12 03:37:16 This the last one Had both LED's blinking on both MO and UNO.

//
// BlinkCrossTest.cpp(ino)
//

#include <Arduino.h>


#define PERIOD 1000

#define BLINKER 11
#define ANOTHERLED 10

uint8_t __tickerPin = BLINKER;
unsigned int __deltaT = PERIOD;
bool __light = LOW;
unsigned long __prevHBT = 0; // Previous Heart Beat time
unsigned int __cumN = 0; // number of samples in cumLT



void heartBeat ()
{
unsigned long currentT = millis(); // Take time time sample
if ( currentT - __prevHBT >= __deltaT) // Delta time passed?
{
__prevHBT = currentT; // Reset previous measure
__light = (__light) ? LOW : HIGH; // toggle light
digitalWrite (__tickerPin, __light); // and display
}
}


void setup() {
pinMode (BLINKER, OUTPUT);
pinMode (ANOTHERLED, OUTPUT);
}

void loop() {

heartBeat ();

digitalWrite (ANOTHERLED, HIGH);
delay (400);
digitalWrite (ANOTHERLED, LOW);
delay (400);
}

//---[ end of file ]---
userHeadPic jacob.kamminga
2018-01-12 03:33:48 This one is with Class copied in the source file.
M0 didn't work on heartbeat LED did on the another LED.
UNO both LEDS blink.

//
// BlinkCrossTestM0_class.cpp(ino)
//

#include <Arduino.h>

#define PERIOD 1000 // heartBeat interval in milliseconds is 1 second

#define BLINKER 11
#define ANOTHERLED 10


uint8_t __tickerPin;
bool __light = LOW;
unsigned long __prevHBT = 0; // Previous Heart Beat time
unsigned int __cumLT = 0; // Cumulative Loop time
unsigned int __cumN = 0; // number of samples in cumLT


class Utils
{
public:
Utils (uint8_t _pin);
Utils (uint8_t _pin, unsigned int _deltaT);
void heartBeat ();

private:
unsigned int __deltaT = 1000; // Time past before we change, default = 1000 1 sec.
};
//---[ End class Utils ]---

Utils util (BLINKER, PERIOD);

void setup() {
// nop
pinMode (ANOTHERLED, OUTPUT);
}

void loop() {
delay (10); // Delay 10 to not have heartBeat executed constantly.
util.heartBeat ();
digitalWrite (ANOTHERLED, HIGH);
delay (400);
digitalWrite (ANOTHERLED, LOW);
delay (400);
}



Utils::Utils (uint8_t _pin)
{
__tickerPin = _pin;
pinMode (__tickerPin, OUTPUT); // Initialise the Heartbeat.
}
//===[ end Utils ]===

Utils::Utils (uint8_t _pin, unsigned int _deltaT)
{
__tickerPin = _pin;
pinMode (__tickerPin, OUTPUT); // Initialise the Heartbeat.
__deltaT = _deltaT; // Set the Delta time measure
}
//===[ end Utils ]===


void Utils::heartBeat ()
{
unsigned long currentT = millis(); // Take time time sample
if ( currentT - __prevHBT >= __deltaT) // Delta time passed?
{
__prevHBT = currentT; // Reset previous measure
__light = (__light) ? LOW : HIGH; // toggle light
digitalWrite (__tickerPin, __light); // and display
}
}
//---[ end of file ]---
userHeadPic jacob.kamminga
2018-01-12 03:29:37 First attempt with blinking led and Library!
On the MO the ANOTHER LED was blinking the heartbeat LED did NOT
On the UNO both LEDs were blinking.

The Main File.
//
// BlinkCrossTestM0.cpp(ino)
//
#include <Arduino.h>
#include "heartbeat.h"

#define PERIOD 1000 // heartBeat interval in milliseconds is 1 second

#define BLINKER 11
#define ANOTHERLED 10

Utils util (BLINKER, PERIOD);

void setup() {
pinMode (ANOTHERLED, OUTPUT);
}

void loop() {
delay (10); // Delay 10 to not have heartBeat executed constantly.
util.heartBeat ();
digitalWrite (ANOTHERLED, HIGH);
delay (400);
digitalWrite (ANOTHERLED, LOW);
delay (400);


}

//---[ end of file ]---

The header file

/*
* heartbeat.h
*/
#ifndef UTILS_LIB_H_
#define UTILS_LIB_H_
#include <Arduino.h>

class Utils
{
public:
Utils (uint8_t _pin);
Utils (uint8_t _pin, unsigned int _deltaT);
void heartBeat ();

private:
unsigned int __deltaT = 1000; // Time past before we change, default = 1000 1 sec.
};
//---[ End class Utils ]---

#endif /* UTILS_LIB_H_ */


The cpp file

/*
* heartbeat.cpp
*
*/

#include "heartbeat.h"

uint8_t __tickerPin;
bool __light = LOW;
unsigned long __prevHBT = 0; // Previous Heart Beat time
unsigned int __cumLT = 0; // Cumulative Loop time
unsigned int __cumN = 0; // number of samples in cumLT

Utils::Utils (uint8_t _pin)
{
__tickerPin = _pin;
pinMode (__tickerPin, OUTPUT); // Initialise the Heartbeat.
}
//===[ end Utils ]===

Utils::Utils (uint8_t _pin, unsigned int _deltaT)
{
__tickerPin = _pin;
pinMode (__tickerPin, OUTPUT); // Initialise the Heartbeat.
__deltaT = _deltaT; // Set the Delta time measure
}
//===[ end Utils ]===

void Utils::heartBeat ()
{
unsigned long currentT = millis(); // Take time time sample
if ( currentT - __prevHBT >= __deltaT) // Delta time passed?
{
__prevHBT = currentT; // Reset previous measure
__light = (__light) ? LOW : HIGH; // toggle light
digitalWrite (__tickerPin, __light); // and display
}
}

//===[ end of file: heartbeat.cpp ]===
userHeadPic jacob.kamminga