Go Down

Topic: I2C Slave command (Read 706 times) previous topic - next topic

Bandofcs

Feb 25, 2013, 05:01 am Last Edit: Feb 25, 2013, 05:17 am by Bandofcs Reason: 1
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
Code: [Select]

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
Code: [Select]
[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;
}
}

Nick Gammon

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
http://www.gammon.com.au/electronics

Nick Gammon

Don't do serial prints inside an interrupt service routine, such as receiveEvent.
http://www.gammon.com.au/electronics

Bandofcs

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?

Nick Gammon

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

Code: [Select]

  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.
http://www.gammon.com.au/electronics

Bandofcs

Hi Nick,

you are right. it doesnt need to. I merely edited from the wire example code given.

Code: [Select]

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

Nick Gammon

Code: [Select]

  delay(a);               // wait for a second


Also don't do delays inside an ISR.

So what is this "x" you are sending?
http://www.gammon.com.au/electronics

Nick Gammon

http://www.gammon.com.au/interrupts

Scroll down to: "Hints for writing ISRs"
http://www.gammon.com.au/electronics

Bandofcs

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

Nick Gammon

Instead of:

Code: [Select]
case 49:


How about:

Code: [Select]
case '1':


Easier to read don't you think?
http://www.gammon.com.au/electronics

PaulS

Code: [Select]
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()!

Go Up