-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.ino
61 lines (57 loc) · 1.42 KB
/
random.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
61
#include <TrueRandom.h>
char input;
String command;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("init");
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()){
input = Serial.read();
switch(input){
case '\n':
executeCommand();
command = "";
default:
if(input != '\n'){
command += input;}
break;
}
}
}
void executeCommand(){
if(command.endsWith("bit")){
Serial.print(TrueRandom.randomBit());
}
else if (command.endsWith("byte")){
Serial.print(TrueRandom.randomByte());
}
else if (command.startsWith("range")){
// Serializing range command:
// \nrange{size1}{size2}{num1}{num2}
// e.g. range135593
int size1 = String(command.charAt(5)).toInt();
int size2 = String(command.charAt(6)).toInt();
int r1;
int r2;
if(size1>1){
r1 = String(command.substring(7, 7+size1)).toInt();}
else{
r1 = String(command.charAt(7)).toInt();
}
if(size2>1){
r2 = String(command.substring(7+size1, 7+size1+size2)).toInt();}
else{
r2 = String(command.charAt(7+size1)).toInt();
}
Serial.print(TrueRandom.random(r1,r2));
}
else{
Serial.println(command);
}
}