Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1]   Go Down
Author Topic: I2C Slave command  (Read 698 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
[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;
 }
}
« Last Edit: February 24, 2013, 11:17:33 pm by Bandofcs » Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 535
Posts: 19769
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

http://gammon.com.au/electronics
Please post technical questions on the forum - not by personal message. Thanks!

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 535
Posts: 19769
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

http://gammon.com.au/electronics
Please post technical questions on the forum - not by personal message. Thanks!

Offline Offline
Newbie
*
Karma: 1
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 535
Posts: 19769
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
  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.
Logged

http://gammon.com.au/electronics
Please post technical questions on the forum - not by personal message. Thanks!

Offline Offline
Newbie
*
Karma: 1
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Nick,

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

Code:
#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;
 }
}
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 535
Posts: 19769
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  delay(a);               // wait for a second

Also don't do delays inside an ISR.

So what is this "x" you are sending?
Logged

http://gammon.com.au/electronics
Please post technical questions on the forum - not by personal message. Thanks!

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 535
Posts: 19769
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Scroll down to: "Hints for writing ISRs"
Logged

http://gammon.com.au/electronics
Please post technical questions on the forum - not by personal message. Thanks!

Offline Offline
Newbie
*
Karma: 1
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 535
Posts: 19769
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Instead of:

Code:
case 49:

How about:

Code:
case '1':

Easier to read don't you think?
Logged

http://gammon.com.au/electronics
Please post technical questions on the forum - not by personal message. Thanks!

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 671
Posts: 51669
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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()!
Logged

Pages: [1]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: