I am trying to use the master device to command the slave to vary the blinky speed. However, I did not observe the blinky LED lighting up at all. Can someone help me look at my code to see what is wrong? Many thanks.
master device
void loop()
{
if (Serial.available() > 0) {
x = Serial.read();
Wire.beginTransmission(4); // transmit to device #4
Wire.write("x is "); // sends five bytes
Wire.write(x); // sends one byte
Wire.endTransmission(); // stop transmitting
}
delay(500);
}
slave device
[tr][td]
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
Serial.println(x); // print the integer
switch(x){
case 49:
a=500;
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(a); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(a);
break;
case 50:
a=1000;
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(a); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(a);
break;
}
}
you are right. it doesnt need to. I merely edited from the wire example code given.
#include <Wire.h>
int led = 13;
int a=0;
void setup()
{
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
// Serial.begin(9600); // start serial for output
pinMode(led, OUTPUT);
}
void loop()
{
delay(100);
}
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
// Serial.println(x); // print the integer
switch(x){
case 49:
a=500;
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(a); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(a);
break;
case 50:
a=1000;
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(a); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(a);
break;
}
}
Why? If you can't find anything useful for loop() to, sitting on its hands for long periods of time, while doing nothing, is stupid. Get rid of the delay()!