Skip to content

Instantly share code, notes, and snippets.

@HorlogeSkynet
Last active August 21, 2017 07:54
Show Gist options
  • Save HorlogeSkynet/0b723f68da6dcf4c4fcd to your computer and use it in GitHub Desktop.
Save HorlogeSkynet/0b723f68da6dcf4c4fcd to your computer and use it in GitHub Desktop.
Random number generation on Arduino helped by many extern sensors
#include "math.h"
#include "Wire.h"
#include "LCD4884.h"
#include "Adafruit_BMP085.h"
#include "TinyDHT.h"
#include "DS1302.h"
#include "Ultrasonic.h"
#define TIME 300000
#define ECHO_P 8
#define TRIG_P 9
#define DHT_P 10
#define MIC A2
#define LUM A3
#define SDA A4
#define SCL A5
Adafruit_BMP085 bmp;
ds1302_struct rtc;
DHT dht(DHT_P, DHT11);
Ultrasonic ultrasonic(TRIG_P, ECHO_P, TIME);
void setup()
{
Serial.begin(9600);
lcd.init();
lcd.clear();
lcd.turnBacklightOn(false);
dht.begin();
while(!bmp.begin())
{
Serial.println("Error with BMP085 !");
delay(1500);
}
}
void loop()
{
float temp = 0, hygro = 0, pressure = 0, luminosity = 0, distance = 0;
int seconds = 0;
int somme = 0, n = 1;
double ecarType = 0;
uint32_t seed = 0;
char seedX[10] = {0}, ecarTypeX[10] = {0};
for( ; ; delay(100))
{
while(analogRead(MIC) > 400.0) //Generate a number if a big sound is detected
{
temp = (bmp.readTemperature() + dht.readTemperature(0)) / 2.0;
hygro = dht.readHumidity();
pressure = (bmp.readPressure() / 100.0);
luminosity = (100.0 * analogRead(LUM)) / 900.0 ;
distance = ultrasonic.Ranging(CM);
DS1302_clock_burst_read((uint8_t *) &rtc);
seconds = 10 * rtc.Seconds10 + rtc.Seconds;
seed = hash(temp, hygro, pressure, luminosity, distance, seconds, n);
ecarType = dispersion(seed, n);
n++;
lcd.clear();
dtostrf(seed, 1, 0, seedX);
dtostrf(ecarType, 1, 0, ecarTypeX);
lcd.writeString(0, 0, "Random:", MENU_NORMAL);
lcd.writeString(0, 2, seedX, MENU_NORMAL);
lcd.writeString(0, 4, "Dispersion:", MENU_NORMAL);
lcd.writeString(15, 5, ecarTypeX, MENU_NORMAL);
}
}
}
uint32_t hash(float temp, float hygro, float pressure, float luminosity, float distance, int seconds, int n)
{
uint16_t faible = 0, fort = 0;
fort = (int)abs(cos(temp*100 - hygro*100) + log((pressure*100)) + distance / luminosity*100 + seconds);
faible = (int)abs((temp*100 + hygro*100 + 1 / pow(pressure*100, n) + sin(luminosity*100)) * exp(1 / (seconds + 1)));
return (((uint32_t)fort << 16) | faible);
}
double dispersion(int seed, int n)
{
static int somme = 0;
static double nVariance = 0;
somme += seed;
nVariance += pow(seed - (somme / (float)n), 2);
return sqrt(nVariance / (float)n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment