I2C Slave command

Hi all,

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;
 } 
}

Read this before posting a programming question

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

Read this: http://www.gammon.com.au/i2c

Don't do serial prints inside an interrupt service routine, such as receiveEvent.

Hi Nick, thanks for your input.

I have tried commenting off all serial codelines including that in setup and it still doesnt work. Are there other methods for debugging?

Post the whole amended receiver sketch please. I'd like to see the whole context. And what is this about?

  Wire.write("x is ");        // sends five bytes
  Wire.write(x);              // sends one byte

That means the receiving end has to handle getting "x is ". Is it? It doesn't look like it to me.

Hi Nick,

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;
 } 
}
  delay(a);               // wait for a second

Also don't do delays inside an ISR.

So what is this "x" you are sending?

Scroll down to: "Hints for writing ISRs"

i am sending '1' and '2' which corresponds to 49 and 50

Instead of:

 case 49:

How about:

 case '1':

Easier to read don't you think?

void loop()
{
  delay(100);
}

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()!