All boards communicate with each other via I2C. The main board is the master and all others are the slaves.
Scann I2C Devices
nDevices = 0;
display.clearDisplay();
for(address = 1; address < 127; address++ )
{
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
device[address] = 1;
display.setCursor(3,nDevices * 10);
display.setTextSize(1);
display.print(address);
display.print(" ");
if(namen[address] == "0"){
display.println("unknown");
}else{
display.println(namen[address]);
}
display.display();
nDevices++;
}
}
byte myArray[2];
void onI2CRequest() {
setNum(analogRead(A0));
TinyWire.write(myArray[0]);
TinyWire.write(myArray[1]);
setNum(analogRead(A1));
TinyWire.write(myArray[0]);
TinyWire.write(myArray[1]);
setNum(analogRead(A2));
TinyWire.write(myArray[0]);
TinyWire.write(myArray[1]);
}
void setNum(int n){
myArray[0] = (n >> 8) & 0xFF;
myArray[1] = n & 0xFF;
}
byte myArray[2];
void onI2CRequest() {
int it =2;
int count = 0;
for(int i = 0 ; i < it; i++){
digitalWrite(trigger, LOW);
delay(5); //Dauer: 5 Millisekunden
digitalWrite(trigger, HIGH);
delay(10);
digitalWrite(trigger, LOW);
dauer = pulseIn(echo, HIGH);
count = count + (dauer/2) * 0.03432;
delay(50);
}
entfernung = count / it;
setNum(entfernung);
TinyWire.write(myArray[0]);
TinyWire.write(myArray[1]);
}
void setNum(int n){
myArray[0] = (n >> 8) & 0xFF;
myArray[1] = n & 0xFF;
}
void stopAll(){
for (int i = 0; i < 4 ; i++){
digitalWrite(m[i], LOW);
}
}
void setSped(int t){
analogWrite(s[0],t);
analogWrite(s[1],t);
}
//richtung 0->stop 1->forward 2->left 3->back 4->right
//speed
void fahren(int r, int sp){
setSped(sp);
if(r == 0){
stopAll();
}else if(r == 1){
digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
}else if(r == 2){
digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
}else if(r == 3){
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
}else if(r == 4){
digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
}
}
void onI2CRequest() {
while (TinyWire.available()) {
int r = TinyWire.read();
int sp = TinyWire.read();
fahren(r,sp);
TinyWire.send(r);
}
}