Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: N5110 PinDetect PowerControl mbed
Diff: main.cpp
- Revision:
- 14:b4fed570abaf
- Parent:
- 13:7ab71c7c311b
- Child:
- 17:d6a3b29cab31
diff -r 7ab71c7c311b -r b4fed570abaf main.cpp --- a/main.cpp Sat May 09 13:56:14 2015 +0000 +++ b/main.cpp Sat May 09 14:39:48 2015 +0000 @@ -48,8 +48,34 @@ StateManager* fsm; +AnalogIn randomPin(p18); // Unconnected pin => noise, used for generating a random seed. +AnalogIn randomPin2(p19); // Unconnected pin + +/// Function for generating a random seed by using a unconnected analog input pin +// Problem: Not trully random as pin is fairly stable. It is unfortunately connected to the ground layer (often returning 0). Would be better to connect it to a "loose wire"/bad connection +unsigned int getRandSeed() +{ + unsigned int randNum = 0; + unsigned int lsb; // least significant bit + + for (unsigned int i = 0; i < 16; ++i) + { + lsb = 1 & (randomPin.read_u16()); // Read least significant bit in randomInput - value depends on noise + randNum |= (lsb << i); // Set bit nr.i to lsb in randomInput + + lsb = 1 & (randomPin2.read_u16()); + randNum |= (lsb << (16+i)); + } + + return randNum; +} + +Serial term(USBTX, USBRX); // TO DELETE - DEBUGGING ONLY + int main() -{ +{ + srand(getRandSeed()); + init(); Timer timer;