![]() ![]() My actual code gets more involved, but simplified version below for demonstration: //declare variable HERE, not in setup() so it will be global, but don't try to set it's value here. I thought the value for any global variable HAD to be set before setup, and wouldn't get updated and carried forward later. What I didn't realise was that if I define the variable pre setup(), but then update its value within setup(), that it will now carry across to loop(). I thought the 2 were mutually exclusive, and that pre-setup I needed to set the value for the variable, not just declare it. I was struggling to use variables determined in setup(), in loop() due to scoping issues. I have added the signature bytes to the sketch.īy the way, I think that there is no reason to calculate random before setup(). Unsigned long newRandom = random( 4000000000L) Store a new random value for the next time. Start the random sequence with this seed number. Unsigned long combinedRandom = noise ^ storedRandom ^ signatureRandom I use binairy xor, but the values can be xor'd or added or subtracted or something else SignatureRandom += boot_signature_byte_get( i) I choose to read up to 64 byte, but the signature area size is different for different chips. This creates more random between different Arduino boards. Use the signature bytes of a AVR microcontroller. At the next restart, this is used to create a new seed number. The first 4 bytes of the EEPROM are used to store a random number. the analog values are spread over the 32-bits,įor( int i = A0 i <= A7 i++) // select the analog pins that your board supports By shifting each value from analogRead() some to the left, The randomSeed() function takes a 32 bit unsigned long parameter. to create a different random sequence each #include // for boot_signature_byte_get() function Perhaps I can add the signature bytes to make it three-quarter overdone. Something else should be added, for example the time that someone presses a button. I call it "half-overdone", because the EEPROM could be the same and all the analog inputs could be connected to GND. With the motto "if you can do it, you can overdo it", I made the next sketch. I don't know if Arduino is fully initialized at that point (hardware and software) as already mentioned. Everything that runs code to initialize something is executed before setup(). There can be initialization of a object before setup(), which can be a lot of code. ![]() The code below just returns the same number every restart? int func(void) I can't seed the RNG in setup, because I need the variable to be global, as it will be used in both setup() and again in loop(), so somehow I need to do it before setup(), get a random value each time the arduino starts, and use that value in both setup() and again in loop(). Another suggestion I found showed this code below, but every time I start the arduino it returns the same value, not a random one each time. What I'm ultimately wanting to achieve is to run setup, and as part of that create a random value that is part of a multistep function within the setup loop, that I can then use in loop() but I have read the doco on scopes and understand variables are not passed between them and they are separate scopes, so I need it as a global variable, defined before setup().īut I seem to keep getting the same random number each time the arduino starts, so have investigated solutions. ![]() Can someone please advise why this code always returns the same number regardless of how many times I power the arduino off and on? ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |