#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// setup the LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// setup constants
const int NOISE_PIN = 0;
const int MAX_PIN = 1023;
const int MEASURES_NUMBER = 4;
void setup() {
// initialize the display and turn on the light
lcd.init();
lcd.backlight();
// get the noise signal from a given pin
const int seed = analogRead(NOISE_PIN);
// transform the integer signal into the voltage
const float voltage = 5 * seed / (float) MAX_PIN;
// print integer and voltage values
Serial.begin(9600);
Serial.print("Seed: "); Serial.print(seed); Serial.print(" ");
Serial.print("("); Serial.print(voltage); Serial.print("V)");
Serial.println();
// setup the random seed
randomSeed(seed);
// print the table head for obtained results
Serial.println("#\tOR\tSE\tEE\tN\tError");
}
// define thresholds for measures
const float OR_THRESHOLD = 0.5;
const float SE_THRESHOLD = 2.5;
const float EE_THRESHOLD = 2.5;
const float TN_THRESHOLD = 31.5;
// define lower bounds for measures
float LOWER_BOUNDS[MEASURES_NUMBER] = {
0, 1, 1, 3
};
// define upper bounds for measures
float UPPER_BOUNDS[MEASURES_NUMBER] = {
OR_THRESHOLD, SE_THRESHOLD, EE_THRESHOLD, TN_THRESHOLD
};
// define error probabilities for measure violations
float ERROR_PROBABILITIES[MEASURES_NUMBER] = {
0.09, 0.07, 0.05, 0.09
};
// define the simulation number
int simulation = 0;
void loop() {
// increase the simulation number
simulation++;
// generate random measures
const int orGateways = random(0, round(OR_THRESHOLD) * 2);
const int startEvents = random(0, round(SE_THRESHOLD) * 2);
const int endEvents = random(0, round(EE_THRESHOLD) * 2);
const int totalNodes = random(0, round(TN_THRESHOLD) * 2);
// organized measures into the array
int measures[MEASURES_NUMBER] = {
orGateways, startEvents, endEvents, totalNodes
};
// the error probability for generated measures
float hasErrors = 1;
/*
Check each measure toward lower and upper bounds;
increase the reverse probability if the measure value does not violate bounds
*/
for (int i = 0; i < MEASURES_NUMBER; i++) {
if (measures[i] < LOWER_BOUNDS[i] || measures[i] > UPPER_BOUNDS[i]) {
hasErrors *= (1 - ERROR_PROBABILITIES[i]);
}
}
// reverse obtained value and find the error probability
hasErrors = 1 - hasErrors;
// the percentage representation of the error probability
float errorPercent = hasErrors * 100;
// clear the display and setup the cursor to the first line
lcd.clear();
lcd.setCursor(0, 0);
// display generated measure
lcd.print("O:"); lcd.print(orGateways); lcd.print(" ");
lcd.print("S:"); lcd.print(startEvents); lcd.print(" ");
lcd.print("E:"); lcd.print(endEvents); lcd.print(" ");
lcd.print("N:"); lcd.print(totalNodes); lcd.print(" ");
// setup the cursor to the second line
lcd.setCursor(0, 1);
// display obtained results
lcd.print("Err:"); lcd.print(errorPercent); lcd.print("%");
lcd.print("("); lcd.print(hasErrors); lcd.print(")");
// print obtained results
Serial.print(simulation); Serial.print("\t");
Serial.print(orGateways); Serial.print("\t");
Serial.print(startEvents); Serial.print("\t");
Serial.print(endEvents); Serial.print("\t");
Serial.print(totalNodes); Serial.print("\t");
Serial.print(hasErrors); Serial.print("\t");
Serial.println();
// wait 2 seconds before the next round
delay(2000);
}