Maybe not a big deal but i dont find the right way.
My Master sends High "H" and Low "L" to turn on an off leds. thats works so far.
but i want my slave to run a "Blink" if the Master sends the "H" and quit after a few blinks.
i tried already different methods but i don´t find out how to get what i want. Maybe i´m on the complete different way. somwehre Lost in the woods.
Hopefully someone here can help me out
Master Code:
#include <Wire.h>
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
void setup()
{
Wire.begin();
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
Wire.beginTransmission(1);
Wire.write('H');
Wire.endTransmission();
delay(500);
Wire.beginTransmission(1);
Wire.write('L');
Wire.endTransmission();
delay(500);
Wire.beginTransmission(1);
Wire.write('H');
Wire.endTransmission();
delay(500);
Wire.beginTransmission(1);
Wire.write('L');
Wire.endTransmission();
delay(500);
Wire.beginTransmission(2);
Wire.write('H');
Wire.endTransmission();
delay(1000);
Wire.beginTransmission(2);
Wire.write('L');
Wire.endTransmission();
} else {
Wire.beginTransmission(1);
Wire.write('L');
Wire.endTransmission();
Wire.beginTransmission(2);
Wire.write('L');
Wire.endTransmission();
}
}
Slave2 Code:
#include <Wire.h>
const byte slaveId = 2;
void setup()
{
Wire.begin(slaveId);
Wire.onReceive(receiveEvent);
pinMode(13, OUTPUT);
}
void loop()
{
}
void receiveEvent(int howMany)
{
char inChar;
while (Wire.available() > 0)
{
inChar = Wire.read();
if (inChar == 'H')
{
digitalWrite(13, HIGH);
delay (100);
digitalWrite(13, LOW);
delay (100);
digitalWrite(13, HIGH);
delay (100);
digitalWrite(13, LOW);
delay (100);
digitalWrite(13, HIGH);
delay (100);
digitalWrite(13, LOW);
delay (100);
}
if (inChar == 'L')
{
digitalWrite(13, LOW);
}
}
}