-
Notifications
You must be signed in to change notification settings - Fork 28
/
Neuroduino_Example.pde
132 lines (105 loc) · 2.83 KB
/
Neuroduino_Example.pde
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <Neuroduino.h>
#define NUM_LAYERS 2
#define ETA 0.01
#define THETA 0.0
#define DEBUG false
// used for get_free_memory()
extern int __bss_end;
extern void *__brkval;
int netArray[NUM_LAYERS] = {8,8};
int inputArray[] = {1, -1, 1, -1, -1, 1, -1, 1};
int trainArray[] = {-1, 1, -1, -1, 1, -1, 1, -1};
int pinState;
int lastRead = HIGH;
int netMem;
Neuroduino myNet;
// free memory check
// from: http://forum.pololu.com/viewtopic.php?f=10&t=989#p4218
int get_free_memory(){
int free_memory;
if((int)__brkval == 0)
free_memory = ((int)&free_memory) - ((int)&__bss_end);
else
free_memory = ((int)&free_memory) - ((int)__brkval);
return free_memory;
}
byte arrayToByte(int arr[], int len){
// Convert -1 to 0 and pack the array into a byte
int i;
byte result = 0;
for(i=len-1; i>=0; i--){
if(arr[i] == -1){
result &= ~(0 << i);
} else {
result |= (1 << i);
}
}
return result;
}
void byteToArray(byte in, int out[]){
int i, temp;//, out[8];
for(i=0; i<8; i++){
temp = (in >> i) & 1;
if(temp == 0) temp = -1;
out[i] = temp;
}
}
void printArray(int arr[], int len){
int i;
Serial.print("[");
for(i=0; i<len; i++){
if (arr[i] == 1) {
Serial.print(" "); // Leading space to maintain alignment
}
Serial.print(arr[i], DEC);
if(i != len-1) Serial.print(", ");
}
Serial.println("]");
}
int checkMem(){
Serial.print("Free memory: ");
Serial.println(get_free_memory(), DEC);
}
void setup(){
srand(analogRead(0));
//srand(4711); // for testing
Serial.begin(9600);
netMem = get_free_memory();
checkMem();
// Neuroduino params: (network array, number of layers, Eta, Theta, debug)
myNet = Neuroduino(netArray, NUM_LAYERS, ETA, THETA, DEBUG);
netMem = netMem - get_free_memory();
Serial.print("Net size: ");
Serial.print(netMem);
Serial.print(" / Free memory: ");
Serial.println(get_free_memory());
myNet.randomizeWeights();
pinMode(8, INPUT);
digitalWrite(8, HIGH);
printArray(inputArray, netArray[0]);
byte temp = arrayToByte(inputArray, netArray[0]);
Serial.println(temp, BIN);
int outputArray[8];
byteToArray(temp, outputArray);
printArray(outputArray, 8);
// Test activation of initial (randomized) weights
Serial.print("Current activation: ");
printArray(myNet.simulate(inputArray), netArray[1]);
Serial.print("Target activation: ");
printArray(trainArray, netArray[1]);
Serial.println();
}
void loop(){
pinState = digitalRead(8);
if(pinState == LOW && lastRead == HIGH){
// switch just closed
myNet.train(inputArray, trainArray);
myNet.printNet();
Serial.print("Current activation: ");
printArray(myNet.simulate(inputArray), netArray[1]);
Serial.print("Target activation: ");
printArray(trainArray, netArray[1]);
Serial.println();
}
lastRead = pinState;
}