0

I have had this problem a few times, it seems the Arduino IDE and the serial monitor is incredibly unstable, unless it's user error but judging from the fact that I hadn't even touched anything before it broke, I would say it's not me.

It was working the night before but this morning it just wouldn't print anything to the serial monitor from my code, however if I start one of the communication example codes like the ASCII example, it prints fine...I'm using the arduino uno, but I have a couple of dues too, so I tried the script on the due and it printed fine to the serial monitor.

Here is my code, could someone check my code and see if they can print to the serial monitor or not?

/*
   Pins:

   RTC:
   5V to VCC
   GND to GDN
   A4 to SDA
   A5 to SLC
*/




#include <Wire.h>
#include "RTClib.h"

#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
#define Serial SerialUSB
#endif

RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

//Words     //0      //1     //2    //3     //4     //5     //6    //7      //8      //9     //10   //11      //12      //13        //14        //15       //16       //17         //18        //19        //20      //21       //22    //23     //24                    //25//26 //27
char* w[] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Thirty", "Forty", "Fifty", "Keeg Jo And Mat 2016", "", " ", "Good",
             //28       //29         //30       //31     //32    //33  //34       //35    //36       //37  //38
             "Morning", "Afternoon", "Evening", "It Is", "Past", "To", "Quarter", "Half", "O'Clock", "AM", "PM"
            };

//End Words

int timeType = 0;
int _hour = 0;

int dayInput = 0;
int hourInput = 0;
int minuteInput = 0;

int iDay = 0;
int iHour = 0;
int iMinute = 0;


void setup () {

#ifndef ESP8266
  while (!Serial); // for Leonardo/Micro/Zero
#endif

  Serial.begin(57600);
  pinMode(11, INPUT);//Minutes
  pinMode(12, INPUT);//Hours
  pinMode(13, INPUT);//Day

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    //rtc.adjust(DateTime(2016, 3, 11, 10, 51, 0));
  }

  //rtc.adjust(DateTime(2016, 3, 11, 1, 0, 50));
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}

void loop () {
  DateTime now = rtc.now();

  minuteInput = digitalRead(11);
  hourInput = digitalRead(12);
  dayInput = digitalRead(13);

  if (minuteInput == HIGH) {
    Serial.println("Minute");
  } else if (hourInput == HIGH) {
    Serial.println("Hour");
  } else if (dayInput == HIGH) {
    Serial.println("Day");
  }

  Line();

  if (now.minute() == 0) {
    timeType = 0;
  } else if (now.minute() > 0 && now.minute() <= 20) {
    timeType = 1;
  } else if (now.minute() > 20 && now.minute() < 50 && now.minute() != 30 && now.minute() != 45) {
    timeType = 0;
  } else if (now.minute() >= 50 && now.minute() < 60) {
    timeType = 2;
  } else if (now.minute() == 30) {
    timeType = 3;
  } else if (now.minute() == 45) {
    timeType = 4;
  }

  ps("Time Type: ");
  pi(timeType);

  Line();
  Line();

  //Greeting
  p(27);
  Space();
  if (now.hour() >= 0 && now.hour() < 12) {
    p(28);
  } else if (now.hour() >= 12 && now.hour() < 18) {
    p(29);
  } else if (now.hour() >= 18 && now.hour() < 24) {
    p(30);
  }

  Line();
  Line();

  //Time (It Is) Time Type 0
  if (timeType == 0) {
    Itis();
    hours(0);
    Space();
    _minutes();
    OClock();
  }

  //Time (Past) Time Type 1
  if (timeType == 1) {
    Itis();
    _minutes();
    Past();
    hours(0);
    OClock();
  }

  //Time (To) Time Type 2
  if (timeType == 2) {
    Itis();
    _minutes();
    To();
    hours(0);
    OClock();
  }

  //Time (Half Past) Time Type 3
  if (timeType == 3) {
    Itis();
    p(35);
    Past();
    hours(0);
  }

  //Time (Quarter To) Time Type 4
  if (timeType == 4) {
    Itis();
    p(34);
    To();
    hours(1);
  }

  //AM/PM
  AMPM();

  Line();
  Line();

  //Day
  //Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  ps(daysOfTheWeek[now.dayOfTheWeek()]);

  Line();
  Line();

  pl(24);

  Serial.println("--------------------------------------------------");
  Serial.println("--------------------------------------------------");

  delay(1000);

}






//------------------------------------------------

void Line() {
  pl(25);
}

void Itis() {
  p(31);
  Space();
}

void Past() {
  Space();
  p(32);
  Space();
}

void To() {
  Space();
  p(33);
  Space();
}

void Space() {
  p(26);
}

void OClock() {
  DateTime now = rtc.now();
  if (now.minute() == 0 || now.minute() == 60) {
    Space();
    p(36);
  }
}

void AMPM() {
  Space();

  DateTime now = rtc.now();

  bool am = false;

  if (now.hour() > 12) {
    am = false;
  } else {
    am = true;
  }

  if (am) {
    p(37);
  } else {
    p(38);
  }
}

void p(int i) {
  Serial.print(w[i]);
}

void pl(int i) {
  Serial.println(w[i]);
}

void ps(char* i) {
  Serial.print(i);
}

void pi(int i) {
  Serial.print(i);
}

void _minutes() {
  DateTime now = rtc.now();
  int minutes = 0;

  if (timeType == 2) {
    minutes = 60 - now.minute();
  } else {
    minutes = now.minute();
  }

  int i = minutes;

  if (i <= 20 && i != 0) {
    p(i);
  } else if (i > 20 && i < 30) {
    p(20);
    Space();
    p(i - 20);
  } else if (i > 30 && i < 40) {
    p(21);
    Space();
    p(i - 30);
  } else if (i > 40 && i < 50 && i != 45) {
    p(22);
    Space();
    p(i - 40);
  } else if (i > 50 && i < 60) {
    p(23);
    Space();
    p(i - 50);
  } else if (i == 30) {
    p(35);
  } else if (i == 40) {
    p(22);
  } else if (i == 50) {
    p(23);
  }

}

void hours(int m) {
  DateTime now = rtc.now();

  if (now.hour() > 12) {
    _hour = now.hour() - 12;
  } else {
    _hour = now.hour();
  }

  _hour = _hour + m;

  p(_hour);
}
1
  //rtc.adjust(DateTime(2016, 3, 11, 1, 0, 50));
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

Every time you reset the Arduino, the RTC clock will be set back to the date/time you compiled the sketch. Surely you don't want that?


The serial monitor is not at all unstable, as you agree when you try a different sketch. Therefore your sketch has a bug, which you need to find.

One possible issue is hardware. The default Wire library will hang if communications are lost when doing a read. Can you describe your clock hardware? Is it a chip you soldered yourself? A board? If so, which one? Do you have pull-up resistors on SDA/SCL?

There is an alternate I2C library that you could try.


Your method of printing is certainly unusual. You call, for example:

Line ();

When you could just as easily (and a lot more readably) call:

Serial.println ();

Similarly for Space and so on.


Still, despite the very hard-to-read code, I can't see any loops that might cause printing to fail. I suggest you comment out the calls to rtc.now() and test it. If it works, that confirms you have a clock issue, not a printing issue.

I note that you have multiple calls to:

DateTime now = rtc.now();

... sprinkled throughout your code. Surely you only need to read the time once, per loop?

Put the now variable outside loop, and call it once only, like this:

DateTime now; 

void loop () {
  now = rtc.now();

Then remove the other calls to rtc.now().


I found when I tested (without a RTC connected) that it did indeed hang. But that was the rtc.now() call failing. If you comment that out, it prints fine.

eg.

Minute

Time Type: 0

Good Morning

It Is Six Twenty Eight AM



Keeg Jo And Mat 2016
--------------------------------------------------
--------------------------------------------------
Minute

Time Type: 0

Good Morning

It Is Six Twenty Eight AM



Keeg Jo And Mat 2016
--------------------------------------------------
--------------------------------------------------

That shows it is definitely a clock issue, not a serial printing issue.

| improve this answer | |
  • Hi Nick, I just noticed something, when I pulled the clock module out of the board to get the name of the board it started printing again the serial monitor...then when I put it back in it started printing the same as what you put in above, however it's printing 'It Is Zero O'Clock AM'... – mr-matt Mar 12 '16 at 21:27
  • Think I may have found the problem, one of the pins has come loose from the board, the solder has come right off, I'll try re soldering it and see if that fixes it – mr-matt Mar 12 '16 at 21:28
  • Oh and as for those unusual printing methods, I'm just trying to keep my code as simple as possible, I'm working with some inexperienced coders, not that I'm particularly experienced though, besides, I actually quite like it! – mr-matt Mar 12 '16 at 21:29
  • All fine now, must have been the soldering... – mr-matt Mar 12 '16 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.