-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQRNG.ino
60 lines (50 loc) · 1.2 KB
/
QRNG.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// (c) Trin Wasinger 2023
// Code for an Arduino QRNG
namespace Quantum {
const int LASER_DIODE_PIN = 12, VERTICAL_PIN = A0, HORIZONTAL_PIN = A1;
const int DELAY = 125; // Adjust as needed
void init() {
pinMode(LASER_DIODE_PIN, OUTPUT);
}
byte rand_bit() {
float v, h;
while(true) {
digitalWrite(LASER_DIODE_PIN, HIGH);
delay(DELAY);
v = analogRead(VERTICAL_PIN);
h = analogRead(HORIZONTAL_PIN);
delay(DELAY);
digitalWrite(LASER_DIODE_PIN, LOW);
// Vertical -> 0, Horizontal -> 1
if(v > h) {
return 0;
} else if(h > v) {
return 1;
} else {
continue;
}
}
}
template<typename T>
T rand() {
T t = T();
for(size_t i = 0; i < sizeof(T)*8; i++) {
t<<=1;
t|=rand_bit();
}
return t;
}
template<>
bool rand() {
return rand_bit();
}
}
void setup() {
Serial.begin(9600);
while(!Serial);
Quantum::init();
}
void loop() {
Serial.print(Quantum::rand_bit());
delay(1000);
}