-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneruon_model.ino
More file actions
205 lines (189 loc) · 3.94 KB
/
neruon_model.ino
File metadata and controls
205 lines (189 loc) · 3.94 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Model Neuron
* Kyle Ramey
* Created February 2015
*/
#include <elapsedMillis.h>
#include <Servo.h>
#include <Adafruit_NeoPixel.h>
//soma button pins and thresholds
int soma0btn = A0;
int soma0thold = 400;
int soma1btn = A1;
int soma1thold = 400;
int soma2btn = A2;
int soma2thold = 400;
//time to reach threshold input, in ms
int reactionTime = 5000;
//# of LEDs per axon and LED pin
int axonLeds = 4;
int axonPin = 3;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(20, axonPin, NEO_GRB + NEO_KHZ_800);
//soma light pins
int soma0light = 4;
int soma1light = 5;
int soma2light = 6;
//servo
Servo servo;
int servoPin = 7;
int startPos = 0;
int endPos = 60;
int currPos = 0;
volitle boolean start = false;
int startBtn = 8;
void setup(){
pinMode(axonPin, OUTPUT);
pinMode(soma1light, OUTPUT);
pinMode(soma2light, OUTPUT);
pinMode(soma2light, OUTPUT);
pinMode(motor, OUTPUT);
servo.attach(7);
servo.write(startPos);
strip.begin();
strip.show();
attachInterrupt(0, startNeuron, RISING); //interrupt 0 is on Digital Pin 2
}
void loop(){
//wait for start threshold at first soma
if(analogRead(soma1btn > 100)){
start = true;
}
if(!start){
waitingAnimation();
}else{
if(soma(soma0btn, soma0thold)){
sendSignal(0);
if(soma(soma1btn, soma1thold)){
sendSignal(1);
if(soma(soma2btn, soma2thold)){
sendSignal(2);
fireMotor();
}
}
}
//YOU DIED. INSERT COIN TO TRY AGAIN.
for(int i=0; i<3; i++){
for(int n=0; n<axonLeds*4; n++){
strip.setPixelColor(n, 255,0,0);
}
strip.show();
delay(1000);
for(int n=0; n<axonLeds*4; n++){
strip.setPixelColor(n, 0,0,0);
}
strip.show();
}
}
}
boolean soma(btn, thold){
elapsedMillis timeElapsed;
while(timeElapsed < reactionTime){
if(analogRead(btn) < thold){
//threshold reached - go to the next soma
return true;
}
}
//threshold not reached in time
return false;
}
void sendSignal(int neuron){
//neopixel library
int startLed = neuron*axonLeds;
int endLed = (neuron+1)*axonLeds;
for(int i = startLed; i<endLed; i++){
strip.setPixelColor(i, 0,255,0);
if(i >= startLed + 2){
strip.setPixelColor(i-2, 0,0,0);
}
strip.show();
delay(500);
}
}
void fireMotor(){
if(currPos == startPos){
servo.write(endPos);
}else if(currPos == endPos){
servo.write(startPos);
}
}
void startNeuron(){
start = true;
detachInterrupt(0);
}
void waitingAnimation(){
//start at 255,0,0
int r = 255, g = 0, b = 0;
elapsedMillis timeElapsed;
//go to 255,255,0
for(g=0; g<=255; g++){
for(int n=0; n<axonLeds*4; n++){
strip.setPixelColor(n, r,g,b);
}
strip.show();
timeElapsed = 0;
while(timeElapsed < 100){
if(start)
return;
}
}
//go to 0,255,0
for(r=255; r>=0; r--){
for(int n=0; n<axonLeds*4; n++){
strip.setPixelColor(n, r,g,b);
}
strip.show();
timeElapsed = 0;
while(timeElapsed < 100){
if(start)
return;
}
}
//go to 0,255,255
for(b=0; b<=255; b++){
for(int n=0; n<axonLeds*4; n++){
strip.setPixelColor(n, r,g,b);
}
strip.show();
timeElapsed = 0;
while(timeElapsed < 100){
if(start)
return;
}
}
//go to 0,0,255
for(g=255; g>=0; g--){
for(int n=0; n<axonLeds*4; n++){
strip.setPixelColor(n, r,g,b);
}
strip.show();
timeElapsed = 0;
while(timeElapsed < 100){
if(start)
return;
}
}
//go to 255,0,255
for(r=0; r<=255; r++){
for(int n=0; n<axonLeds*4; n++){
strip.setPixelColor(n, r,g,b);
}
strip.show();
timeElapsed = 0;
while(timeElapsed < 100){
if(start)
return;
}
}
//go to 255,0,0
for(b=255; b>=0; b--){
for(int n=0; n<axonLeds*4; n++){
strip.setPixelColor(n, r,g,b);
}
strip.show();
timeElapsed = 0;
while(timeElapsed < 100){
if(start)
return;
}
}
}