-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbrain.ino
65 lines (58 loc) · 1.33 KB
/
brain.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
62
63
64
65
#include <Wire.h>
#include <stdio.h>
void setup() {
Serial.begin(9600);
Wire.begin();
pinMode(2, OUTPUT);
digitalWrite(2, LOW);
delay(100);
// Turn the first node in the chain on
digitalWrite(2, HIGH);
delay(100);
Serial.println("Locating nodes..");
}
uint8_t find_nodes = 1;
int currentNode = 1;
int bufloc = 0;
char val[255];
void rec() {
bufloc=0;
while(Wire.available()) {
byte i = Wire.read();
val[bufloc] = i;
bufloc++;
}
val[bufloc] = 0;
}
void print_rec() {
char buf[4];
int i=0;
for (i; i<bufloc; i++) {
sprintf(buf,"0x%02x", val[i]);
Serial.print(buf);
}
}
void loop() {
if (find_nodes) {
Wire.beginTransmission(0x00); // Give the node at #1 new addy
Wire.write(currentNode); // new address
int res = Wire.endTransmission(); // stop transmitting
delay(70);
Wire.requestFrom(currentNode, 1, 1);
rec();
Serial.println(val);
if (res > 0 || val[0] != currentNode) {
Serial.print("found ");
Serial.print(currentNode-1);
Serial.println(" nodes ");
find_nodes = 0;
} else {
Serial.print("found node @ ");
Serial.print(currentNode);
Serial.print(" verify=");
print_rec();
currentNode++;
}
}
// TODO: parser for controlling different buttons and collecting data from sensors
}