Hello. I'm trying to use 3 Arduinos (1 master 2 slaves). First, one slave keeps track of the time and sends it to the master. The master prints it and interprets when the time is right to send a signal to the other slave to activate a servo. The 2nd slave then gets this message and activates the servo, and that's it. I am calling the slaves "drones" and the master "queen" to clarify.
As of now, it immediately begins to move the servo's position up in very small increments until the servo runs out of degrees of movement. The serial monitor just prints "waiting". Any ideas?
Master's Code:
#include <Wire.h>
#define clockDroneAddress 12
#define workingDroneAddress 13
int byteCount;
int seconds;
int minutes;
int hours;
int days;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
}
void loop() {
// put your main code here, to run repeatedly:
sortClockIncoming();
checkTime();
Serial.print("seconds: ");
Serial.print(seconds);
Serial.print(" minutes: ");
Serial.print(minutes);
Serial.print(" hours: ");
Serial.print(hours);
Serial.print(" days: ");
Serial.println(days);
}
void checkTime() {
if (seconds >= 15) {
Wire.beginTransmission(workingDroneAddress);
Wire.write(1);
Wire.endTransmission();
} else {
Wire.beginTransmission(workingDroneAddress);
Wire.write(0);
Wire.endTransmission();
}
//other stuff here too like check time for lighting
}
void sortClockIncoming() {
while (readI2C(clockDroneAddress) != 255) {
Serial.println("Waiting");
}
for (byteCount = 0; byteCount < 4; byteCount++) {
switch (byteCount) {
case 0:
seconds = readI2C(clockDroneAddress);
break;
case 1:
minutes = readI2C(clockDroneAddress);
break;
case 2:
hours = readI2C(clockDroneAddress);
break;
case 3:
days = readI2C(clockDroneAddress);
break;
}
}
}
byte readI2C(int address) {
byte byteVal;
long entry = millis();
Wire.requestFrom(address, 1);
while (Wire.available() == 0 && (millis() - entry) < 100) {
Serial.println("Waiting");
}
if ((millis() - entry) < 100) {
byteVal = Wire.read();
}
return byteVal;
}
Clock Slave:
#include <Wire.h>
#define droneAddress 12
int byteCount = 0;
int seconds = 0;
int minutes = 0;
int hours = 0;
int days = 0;
int delaySecond = 1000;
void setup() {
// put your setup code here, to run once:
Wire.begin(droneAddress);
Wire.onRequest(requestEvent);
}
void requestEvent() {
byte byteVal;
switch(byteCount) {
case 0:
byteVal = 255;
break;
case 1:
byteVal = seconds;
break;
case 2:
byteVal = minutes;
break;
case 3:
byteVal = hours;
break;
case 4:
byteVal = days;
break;
}
Wire.write(byteVal);
byteCount++;
if (byteCount > 4) {
byteCount = 0;
}
}
void count() {
delay(delaySecond);
seconds++;
if (seconds == 60) {
seconds = 0;
minutes++;
}
if (minutes == 60) {
minutes = 0;
hours ++;
}
if (hours == 24) {
hours = 0;
days++;
}
}
void loop() {
// put your main code here, to run repeatedly:
count();
}
Servo Slave:
#include <Wire.h>
#include <Servo.h>
#define droneAddress 13
Servo servo;
int receivedData;
int servoPn = 2;
void setup() {
// put your setup code here, to run once:
Wire.begin(droneAddress);
Wire.onReceive(receiveEvent);
pinMode(servoPn, OUTPUT);
servo.attach(servoPn);
}
void loop() {
// put your main code here, to run repeatedly:
if (receivedData == 1) {
servo.write(180);
} else if (receivedData == 0) {
servo.write(0);
}
}
void receiveEvent() {
receivedData = Wire.read();
}