-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_3DTTT_SLAVE.ino
executable file
·68 lines (60 loc) · 1.63 KB
/
_3DTTT_SLAVE.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
66
67
68
#include <TinyWire.h>
#define blue_led_pin 3
#define orange_led_pin 4
#define buttonPin 1
boolean buttonPressed = false;
byte address = 33;
byte command;
void setup() {
pinMode(blue_led_pin, OUTPUT);
pinMode(orange_led_pin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
digitalWrite(blue_led_pin, LOW);
digitalWrite(orange_led_pin, LOW);
TinyWire.begin( address );
TinyWire.onReceive( onI2CReceive );
TinyWire.onRequest( onI2CRequest );
}
void loop() {
if(digitalRead(buttonPin) == LOW){
buttonPressed = true;
}
}
/*
I2C Slave Receive Callback:
Note that this function is called from an interrupt routine and shouldn't take long to execute
*/
void onI2CReceive(int howMany){
// loops, until all received bytes are read
while(TinyWire.available()>0){
// toggles the led everytime, when an 'a' is received
byte recieved = TinyWire.read();
switch(recieved){
case 'a': //Turn ON Blue Led
digitalWrite(blue_led_pin, HIGH);
break;
case 'b': //Turn OFF Blue LED
digitalWrite(blue_led_pin, LOW);
break;
case 'c': //Turn ON Orange LED
digitalWrite(orange_led_pin, HIGH);
break;
case 'd': //Turn OFF Orange LED
digitalWrite(orange_led_pin, LOW);
break;
case 'e': //RESET Button Flag
buttonPressed = false;
break;
}
}
}
void onI2CRequest() {
// sends one byte with content 'b' to the master, regardless how many bytes he expects
// if the buffer is empty, but the master is still requesting, the slave aborts the communication
// (so it is not blocking)
if(buttonPressed)
TinyWire.send('o');
else{
TinyWire.send('f');
}
}