i finally managed to screw up a project so badly, that I seemed worthy to create an account.
I am pretty sure that one of you will easily point me to my stupid mistake.
What happens is that my code uploads to my Arduino Nano just fine.
However, it will result in the arduino just blinking the internal led at 1Hz.
If you comment out the code between the following lines
// start comment out to make it work
// end comment out to make it work
everything works as expected.
I just can't find a mistake in the part of code, so I assume it to be a bit deeper. A memory or timing issues, whatever.
I would highly appreciate any hints.
My search for solutions in this forum has been successful many times. However, this time I couldn't find anything.
Maybe I was searching for wrong terms.
#include "DCF77.h"
#include <Time.h>
#include <TimeLib.h>
#include "DFRobotDFPlayerMini.h"
#include <SoftwareSerial.h>
SoftwareSerial dfSerial(3, 4);
DFRobotDFPlayerMini dfPlayer;
#define xOff 5
#define xKill 6
#define xBusy 7
bool xDone;
#include <Wire.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#define I2C_ADDRESS 0x3C
#define DCF_PIN 2
#define DCF_INTERRUPT 0
time_t time;
int AlarmHour;
int AlarmMinute;
int SongNumber;
int NapTime;
DCF77 DCF = DCF77(DCF_PIN,DCF_INTERRUPT);
SSD1306AsciiWire oled;
String blueToothVal;
unsigned long timestore;
////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT);
pinMode(8, OUTPUT);
DCF.Start();
dfSerial.begin(9600);
dfPlayer.volume(20);
Wire.begin();
Wire.setClock(400000L);
oled.begin(&Adafruit128x32, I2C_ADDRESS);
attachInterrupt(digitalPinToInterrupt(xKill), AlarmOff, FALLING);
attachInterrupt(digitalPinToInterrupt(xOff), AlarmOn, RISING);
attachInterrupt(digitalPinToInterrupt(xBusy), AlarmOn, RISING);
}
////////////////////////////////////////////////////////////////////////////////////////
void loop() {
if(Serial.available()) //wenn Daten empfangen werden...
{
blueToothVal=Serial.readString();//..sollen diese ausgelesen werden
Serial.print(blueToothVal);
if (blueToothVal[0]=='t'){
AlarmHour = (blueToothVal[1]-'0')*10 + (blueToothVal[2]-'0');
AlarmMinute = (blueToothVal[3]-'0')*10 + (blueToothVal[4]-'0');
// Serial.print(AlarmHour+AlarmMinute);
blueToothVal="";
}
// start comment out to make it work
else if (blueToothVal[0]=='n'){
NapTime = (blueToothVal[1]-'0')*10 + (blueToothVal[2]-'0');
Serial.print(NapTime);
blueToothVal="";
}
else if (blueToothVal[0]=='s'){
SongNumber = (blueToothVal[1]-'0')*10 + (blueToothVal[2]-'0');
Serial.print(SongNumber);
blueToothVal="";
}
// end comment out to make it work
}
if (millis() - timestore > 1000 ) {
time_t DCFtime = DCF.getTime(); // ist eine neue DCF77 Zeit vorhanden
if (DCFtime!=0)
{
digitalClockDisplay();
setTime(DCFtime);
}
oled.setFont(lcdnums14x24);
digitalClockDisplay();
timestore = millis();
}
if (((time/60) % 60) == AlarmMinute && ((time/3600) % 24) == AlarmHour && !xOff && !xDone){
dfPlayer.play(SongNumber);
xDone = true;
}
}
////////////////////////////////////////////////////////////////////////////////////////
void digitalClockDisplay(){
oled.setCursor(0,0);
oled.print(hour());
printDigits(minute());
printDigits(second());
}
void printDigits(int digits){ // Ausgabe
oled.print(":");
if(digits < 10)
oled.print('0');
oled.print(digits);
}
void AlarmOff(){
xDone = false;
dfPlayer.stop();
}
void AlarmOn(){
xDone = false;
}
Can you show us the result of the compile & upload ?
Neither can I. The only thing i see is that you are using the String class, but so marginally that it shouldn't be an issue, and definitely not straight away. A solution with c-strings is of course better.
I am suspecting a memory overflow somewhere, but i can not really point my finger on it. You are using libraries that declare their objects on the Heap, so even the result of the compilation may not show you how much memory you are using.
The internal LED blinking, is weird, you have not even set the pinMode to output, are you sure it is not the power Led or the TX led blinking ?
Thank you all! I am truly fascinated by your helpfulness.
I just learned something about interrupts here.
My Arduino Nano has them in pins 2 and 3.
Definitely not the pins I used and also not three of them.
For now, I fix that by commenting them out.
However, I get the same effect.
Here's what I get when uploading with verbose set to on.
Hope that is what was requested.
When compiling I only get a warning due to comparison between signed and unsigned integer expressions, that seems not to be a problem.
avrdude: Version 6.3-20190619
Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
Copyright (c) 2007-2014 Joerg Wunsch
System wide configuration file is "C:\Users\...\AppData\Local\Arduino15\packages\arduino\tools\avrdude\6.3.0-arduino17/etc/avrdude.conf"
Using Port : COM8
Using Programmer : arduino
Overriding Baud Rate : 57600
AVR Part : ATmega328P
Chip Erase delay : 9000 us
PAGEL : PD7
BS2 : PC2
RESET disposition : dedicated
RETRY pulse : SCK
serial program mode : yes
parallel program mode : yes
Timeout : 200
StabDelay : 100
CmdexeDelay : 25
SyncLoops : 32
ByteDelay : 0
PollIndex : 3
PollValue : 0x53
Memory Detail :
Block Poll Page Polled
Memory Type Mode Delay Size Indx Paged Size Size #Pages MinW MaxW ReadBack
----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
eeprom 65 20 4 0 no 1024 4 0 3600 3600 0xff 0xff
flash 65 6 128 0 yes 32768 128 256 4500 4500 0xff 0xff
lfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
hfuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
efuse 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
lock 0 0 0 0 no 1 0 0 4500 4500 0x00 0x00
calibration 0 0 0 0 no 1 0 0 0 0 0x00 0x00
signature 0 0 0 0 no 3 0 0 0 0 0x00 0x00
Programmer Type : Arduino
Description : Arduino
Hardware Version: 2
Firmware Version: 1.16
Vtarget : 0.0 V
Varef : 0.0 V
Oscillator : Off
SCK period : 0.1 us
avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.00s
avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: reading input file "C:\Users\...\AppData\Local\Temp\arduino_build_251293/DCF77.ino.hex"
avrdude: writing flash (13838 bytes):
Writing | ################################################## | 100% 3.94s
avrdude: 13838 bytes of flash written
avrdude: verifying flash memory against C:\Users\...\AppData\Local\Temp\arduino_build_251293/DCF77.ino.hex:
avrdude: load data flash data from input file C:\Users\...\AppData\Local\Temp\arduino_build_251293/DCF77.ino.hex:
avrdude: input file C:\Users\...\AppData\Local\Temp\arduino_build_251293/DCF77.ino.hex contains 13838 bytes
avrdude: reading on-chip flash data:
Reading | ################################################## | 100% 2.96s
avrdude: verifying ...
avrdude: 13838 bytes of flash verified
avrdude done. Thank you.
I included a lot of print statements to see what works and what doesn't.
And found out that the dfplayer mini couldn't establish a serial connection during setup.
That caused the blinking of the internal LED. I did not yet find out why the blinking occured as pin 13 is adressed nowhere.
And I also don't know, why commenting out the marked lines in my sketch made it work all of a sudden. Because the dfplayer still was not ready and therefore should have caused the same problem.
However, once I commented out the whole lines where dfplayer was used it all worked fine.
Why was dfplayer not able to connect?
First I used the 5V pin of the arduino. Needless to say the power was not enough to boost the speakers.
Then I used an external (5V) power source and supplied the arduino on the 5V pin. Still it didn't work until the Arduino itself was also plugged in via the USB cable. Probably the external power source was still too weak. Once I hooked off the speakers it all worked fine.
So one might consider this as solved.
I am still curious about the mysteries stated above. If anybody sees a possible cause I would be grateful to be enlightened.