Time.h library not returning values

I know this has to be so easy, that I am just missing something stupid, but I can't seem to get any of the variables to return anything. They are all 0. Here is a super simple sample that I wrote just to test it. The goal was to create a random number seed using the now(), since that should always be different. I tried printing several different time variables to the LCD.

Thanks again.

#include <Time.h>
#include <SoftwareSerial.h>

time_t t = now();
long CurrentTime;
SoftwareSerial LCD(11, 10); // RX, TX
int rcode;


void setup()  
{
 

  // set the data rate for the SoftwareSerial port
  LCD.begin(9600);
  
  
  
  LCD.write(0xFE);
  LCD.write(0x01); 
  
  LCD.write("Time is ");
   CurrentTime = now();
   randomSeed(CurrentTime);
   LCD.print(t);
   LCD.print(" ");
   LCD.print(hour());
   LCD.print(" ");
   rcode = random(9999);
   LCD.print(rcode);
  
}


void loop()
{
  while(1);
}

Have you tried:

"If it is important for a sequence of values generated by random() to differ, on subsequent executions of a sketch, use randomSeed() to initialize the random number generator with a fairly random input, such as analogRead() on an unconnected pin."

Yes, I saw that, but isn't reading an unused pin going to return 1 or 0? I figured that using the Time library, the random seed would always be significantly different. Instead of just option one or two, which means the random number will be X or Y to start.

analogRead()

An floating anlalog input will be different each time it is read (it will not be the same) and is between 0 - 1023.

Your now() found in setup is always the same each time the Arduino is restarted.

How is now() going to know the real time? There isn't an onboard clock.

Good question, Nick. :slight_smile: The description of the Time.h library suggests that the "new" coding does not need any external equipment beyond the Arduino. So are you saying this can't really be done with the Time.h library then?

Larry, I agree with that. That is why I define CurrentTime specifically as that, but then I try to call it again later to examine the change, sort of like you would use mills(). So is there a specific input I should be looking at for this? Do I need to do anything in the header for variables, or is it really that random every time the unit powers on? And if it is that random every time, why is it?

Which library are you using exactly? The one I have says this in the readme file:

A primary goal was to enable date and time functionality that can be used with a variety of external time sources with minimum differences required in sketch logic.

(My emphasis).

So are you saying this can't really be done with the Time.h library then?

Excluding magic, no.

"The Time library adds timekeeping functionality to Arduino with or without external timekeeping hardware."

That was the first line of the page at Arduino Playground - Time. Obviously, I was mistaken, however.

I think they mean "time management".

Anyway, it looks like now() returns the time as far as it knows it. You would use setTime to tell the library the time you currently have (eg. from a RTC clock). Once you do that it would count upwards without needing to be set again.

Well, that makes the Time idea fairly pointless. So in regards to Larry's response about using the input value as a random seed. It sounds like that's the only way to go here.

Is there a specific input I should be looking at for this? Do I need to do anything in the header for variables, or is it really that random every time the unit powers on? And if it is that random every time, why is it?

This has been covered fairly heavily in the past, as you might imagine. Try Googling "arduino random number seed".

Do I need to do anything in the header for variables, or is it really that random every time the unit powers on?

It's not exactly "random" in the true sense of the word.

See this:
"Only the last line changes each time the sketch starts, because it sets the seed to a different value by reading it from an unconnected analog input port as a seed to the randomSeed function. If you are using analog port 0 for something else, change the argument to analogRead to an unused analog port."
https://www.inkling.com/read/arduino-cookbook-michael-margolis-2nd/chapter-3/recipe-3-11

BTW this is a good book.

Just for fun I tried a sketch that read an unconnected analog port. Here is what I got (removing duplicates and sorting):

307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
326
327
328
329
331
333
335
336
339
340
343
345
347
350
353
356
358
362
365

So, some variance but not a staggering amount.

I have. As Larry pointed out, this is the suggestion. It's very simple obviously. He had just mentioned that the analogRead has a value from 0-1023. As Larry just responded, it sounds like any unused analog pin will do. I was just curious how it gets that value.

long randNumber;

void setup(){
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop(){
  randNumber = random(300);
  Serial.println(randNumber);

PS: Larry, I was just scanning that one. I had seen it before, but I may have to check it out in more detail.

Raw data (unsorted) from a subsequent test:

297
297
297
297
298
298
299
299
299
299
300
300
300
301
300
300
300
300
300
300
300
299
299
299
298
298
298
298
297
297
296
296
296
296
296
296
296
296
296
296
296
297
296
297

So if you want to seed the generator by 296 or thereabouts, that technique is fine. :stuck_out_tongue:

Firestorm:
He had just mentioned that the analogRead has a value from 0-1023.

Yes, when connected to voltages in the range 0 to 5V. An unconnected pin will float somewhere in the middle, possibly depending on where your hand is.

My test, in case you want to try it:

void setup() 
{
  Serial.begin(115200);
}

void loop()
{
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  delay(100);        
}

Nick how about adding a 6" wire to the pin.

:sleeping: night.

I'll give that a try. I will also try a pin connected to voltage to see if it has a wider range of values. It's just for a random code generator for that project I am working on. As long it's not the same time every time, I can live with that. If I can increase that "randomness" to 100 or so, even better.

Thanks for the help as always!

If you are trying to randomize input to a game, or simulation, the simplest thing would be to time some interval created by humans. For example, if you say "press the button to continue" and then time the time it takes to do that, in microseconds, you will probably get a pretty random result.