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);
}
"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.
Good question, Nick. 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?
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?
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
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.
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.
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.