-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathardi2cslave.ino
75 lines (62 loc) · 1.8 KB
/
ardi2cslave.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
69
70
71
72
73
74
75
/// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
int incomingByte = 0; // for incoming serial data
int bufflen = 0;// count of serial inputs
int mybuff[33]={};
int curpos=0;
int bufffull=0;
void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Wire.onRequest(requestEvent);
Serial.begin(9600); // start serial for output
}
void loop()
{
if (Serial.available()>0) {
if(bufffull==1){
// do nothing
}
else
{
incomingByte=Serial.read();
mybuff[curpos]=incomingByte;
Serial.write(incomingByte);
curpos++;
if(curpos>32) {bufffull=1;}
}
}
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
while(1 < Wire.available()) // loop through all but the last
{
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
if (x!=0){
Serial.write(x); // print the integer as a character
if (x==13) { Serial.write("\r\n"); } //cr and lf
}
}
void requestEvent()
{
for (byte i = 0; i < 32; i = i + 1) {
if(i<=curpos){
Wire.write(mybuff[i]);
mybuff[i]=0;
}
}
curpos=0;
bufffull=0;
}