// unsigned long Received = 0xC18D81;
#define RANDOM A0 // Analogue pin as a source of noise.
union Convert
{
unsigned long longVar;
uint8_t inByte[4];
} ;
Convert RFcode;
char ChallResp[11];
String Second;
bool Armed;
bool Escalate;
char RandomNumber[2];
void setup() {
pinMode(RANDOM, INPUT); // switch on the analogue pin for random number
Serial.begin(115200);
// RFcode.longVar = 0xC18D81;
RFcode.longVar = 12684673;
Armed = false;
Escalate = true;
/*
Serial.print("Original var: ");
Serial.println(RFcode.longVar, HEX);
Serial.print("Byte 1: ");
Serial.println(RFcode.inByte[0], HEX);
Serial.print("Byte 2: ");
Serial.println(RFcode.inByte[1], HEX);
Serial.print("Byte 3: ");
Serial.println(RFcode.inByte[2], HEX);
Serial.print("Byte 4: ");
Serial.println(RFcode.inByte[3], HEX);
*/
Serial.println(String(ChallResp).substring(2, 5));
String Converted = String(ChallResp);
Second = Converted.substring(3, 5);
Serial.println(Second);
Converted.toUpperCase();
Serial.println(Converted);
// if (!Armed && Escalate) {
// Serial.println("Armed ~ false, Escalate ~ true");
// }
uint32_t RandomSeed = generateRandomSeed(1);
Serial.print("Generated random seed: ");
Serial.println(RandomSeed);
randomSeed(RandomSeed);
// ChallResp[0] = '\0';
// if (ChallResp[0] == 0) {
// Serial.println("Character array is empty");
// }
ultoa(RandomSeed, ChallResp, 10);
Serial.print("Character array generated: ");
Serial.println(ChallResp);
for (uint8_t i = 0; i < 7; i++) {
// randomSeed(generateRandomSeed(6));
if (ChallResp[i] == '0') {
ChallResp[i] = random(49, 58);
}
// Serial.print(ChallResp[i]);
}
Serial.print("Character array after removing nils: ");
Serial.println(ChallResp);
uint8_t NilPosition = random(3, 7);
Serial.print("Nil position is: ");
Serial.println(NilPosition);
ChallResp[NilPosition] = '0';
Serial.print("Character array after overwriting with 0: ");
Serial.println(ChallResp);
strlcpy(ChallResp, ChallResp, NilPosition + 1);
Serial.print("The right response is: ");
Serial.println(ChallResp);
// for (byte i = 0; i < 10; i++) {
// Serial.print("Reading from input: ");
// Serial.println(analogRead(RANDOM));
// delay(300);
// }
}
void loop() {}
uint32_t generateRandomSeed(byte RandomSeedDelay) { // Author: Daniel Hienzsch, rheingoldheavy.com
uint8_t seedBitValue = 0;
uint8_t seedByteValue = 0;
uint32_t seedWordValue = 0;
for (uint8_t wordShift = 0; wordShift < 4; wordShift++) // 4 bytes in a 32 bit word
{
for (uint8_t byteShift = 0; byteShift < 8; byteShift++) // 8 bits in a byte
{
for (uint8_t bitSum = 0; bitSum <= 8; bitSum++) // 8 samples of analog pin
{
seedBitValue = seedBitValue + (analogRead(RANDOM) & 0x01); // Flip the coin eight times, adding the results together
}
delay(RandomSeedDelay); // Delay a milliseconds to allow the pin to fluctuate
seedByteValue = seedByteValue | ((seedBitValue & 0x01) << byteShift); // Build a stack of eight flipped coins
seedBitValue = 0; // Clear out the previous coin value
}
seedWordValue = seedWordValue | (uint32_t) seedByteValue << (8 * wordShift); // Build a stack of four sets of 8 coins (shifting right creates a larger number so cast to 32bit)
seedByteValue = 0; // Clear out the previous stack value
}
return (seedWordValue);
}