Hi guys. Can you shed some light on what I am doing wrong here?
My Slave code is perfectly working and it is based on masterWriter tutorial. However, when I incorporated it with a loop, it doesn't return the right output I want it to.
Basically, when it receives the analog data from Master (which is sound sensor btw), the Slave will stop the reading of data, stops the current loop of the system then changes it to another loop ( i dunno if that made sense).
Here is the complete code of my Slave:
#include <Wire.h>
int North[] = {2, 3, 4};
int West[] = {5, 6, 7};
int South[] = {8, 9, 10};
int East[] = {11, 12, 13};
int redDelay = 5000;
int yellowDelay = 2000;
void setup()
{
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
for (int i = 0; i < 3; i++) {
pinMode(North[i], OUTPUT);
pinMode(West[i], OUTPUT);
pinMode(South[i], OUTPUT);
pinMode(East[i], OUTPUT);
}
}
int threshold = 100;
void loop()
{
// Making Green LED at signal 1 and red LED's at other signal HIGH
digitalWrite(North[2], HIGH);
digitalWrite(North[0], LOW);
digitalWrite(West[0], HIGH);
digitalWrite(South[0], HIGH);
digitalWrite(East[0], HIGH);
delay(redDelay);
// Making Green LED at signal 1 LOW and making yellow LED at signal 1 HIGH for 2 seconds
digitalWrite(North[1], HIGH);
digitalWrite(North[2], LOW);
delay(yellowDelay);
digitalWrite(North[1], LOW);
// Making Green LED at signal 2 and red LED's at other signal HIGH
digitalWrite(North[0], HIGH);
digitalWrite(West[2], HIGH);
digitalWrite(West[0], LOW);
digitalWrite(South[0], HIGH);
digitalWrite(East[0], HIGH);
delay(redDelay);
// Making Green LED at signal 2 LOW and making yellow LED at signal 2 HIGH for 2 seconds
digitalWrite(West[1], HIGH);
digitalWrite(West[2], LOW);
delay(yellowDelay);
digitalWrite(West[1], LOW);
// Making Green LED at signal 3 and red LED's at other signal HIGH
digitalWrite(North[0], HIGH);
digitalWrite(West[0], HIGH);
digitalWrite(South[2], HIGH);
digitalWrite(South[0], LOW);
digitalWrite(East[0], HIGH);
delay(redDelay);
// Making Green LED at signal 3 LOW and making yellow LED at signal 3 HIGH for 2 seconds
digitalWrite(South[1], HIGH);
digitalWrite(South[2], LOW);
delay(yellowDelay);
digitalWrite(South[1], LOW);
//Serial.println(LastGreen);
// Making Green LED at signal 4 and red LED's at other signal HIGH
digitalWrite(North[0], HIGH);
digitalWrite(West[0], HIGH);
digitalWrite(South[0], HIGH);
digitalWrite(East[2], HIGH);
digitalWrite(East[0], LOW);
delay(redDelay);
// Making Green LED at signal 4 LOW and making yellow LED at signal 4 HIGH for 2 seconds
digitalWrite(East[1], HIGH);
digitalWrite(East[2], LOW);
delay(yellowDelay);
digitalWrite(East[1], LOW);
}
boolean stp = false;
int x;
void receiveEvent(int howMany)
{
while(1 < Wire.available()) // loop through all but the last
{
char c = Wire.read();
Serial.print(c);
}
if ( stp == false ){
x = Wire.read();
Serial.println(x);
}
if (x >= threshold) {
while ( stp = true) {
digitalWrite(North[0], LOW);
//digitalWrite(North[1], LOW);
digitalWrite(North[2], LOW);
digitalWrite(West[0], LOW);
//digitalWrite(West[1], LOW);
digitalWrite(West[2], LOW);
digitalWrite(South[0], LOW);
//digitalWrite(South[1], LOW);
digitalWrite(South[2], LOW);
digitalWrite(East[0], LOW);
//digitalWrite(East[1], LOW);
digitalWrite(East[2], LOW);
delay(1000);
digitalWrite(North[1], HIGH);
digitalWrite(West[1], HIGH);
digitalWrite(South[1], HIGH);
digitalWrite(East[1], HIGH);
delay(1000);
digitalWrite(North[0], HIGH);
digitalWrite(North[1], LOW);
digitalWrite(West[0], HIGH);
digitalWrite(West[1], LOW);
digitalWrite(South[0], HIGH);
digitalWrite(South[1], LOW);
digitalWrite(East[0], HIGH);
digitalWrite(East[1], LOW);
}
}
}