Serial print in I2C command

hi guys,

I am trying to implement my UNO (slave) to access a EEPROM in 1 of its 12C command and print out the data in 3 addresses.

This is my code:

void requestEvent()
{
switch (x){

case 0x30://ASCII_0_Payload_Data
digitalWrite(MUXS1, HIGH);
digitalWrite(MUXS2, LOW);
for(int k=0;k<3;k++){
value[k] = EEPROM.read(k);}
value[3]='0';
Serial.println(value);

digitalWrite(MUXS1, HIGH);
digitalWrite(MUXS2, HIGH);
for(int k=0;k<3;k++){
value[k] = EEPROM.read(k);}

value[3]='0';
Serial.println(value);

break;}

However, the code simply stopped at the 4th line of code. The LED turn hign and low respectively but nothing was printed out on serial lines. The LEDs did not turn high and high.

Can someone help?

thanks

If you'd like some help, please post your ENTIRE code in code tags, not quote tags. Use auto-format in the IDE, copy your code, paste it here, highlight it, and click on the # icon above the editor.

The reason I ask for your entire code is that we have no way of knowing how you declared variables, etc. For example, it seems, from the snippet you posted, that you are trying to Serial.print value. We can't tell what value is, can we?

Edit: Forgot to ask... Why do you say "the code simply stopped at the 4th line of code". You have no way of knowing that.

oh ... thanks for the advise ... i'm a newbie in the forum.

But here goes:

#include <Time.h>
#include <stdlib.h>
#include <Wire.h>
#include <String.h>
#include <wdt.h>
#include <avr/wdt.h> 
#include <EEPROM.h>

char value[4];
int address=0,MUXS1=8,MUXS2=9,MUXU1=5,MUXU2=6,led=13,x=999,y=0,z=0,w=0,q=0;
char v[4],u[4],r[4],t[12];

void setup()
{
  Wire.begin(4); 
  Wire.onRequest(requestEvent);  // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(115200);           // start serial for output
  pinMode(MUXU1, OUTPUT);
  digitalWrite(MUXU1,LOW);
  pinMode(MUXU2, OUTPUT);
  digitalWrite(MUXU2,LOW);
  pinMode(MUXS1, OUTPUT);
  digitalWrite(MUXS1,LOW);
  pinMode(MUXS2, OUTPUT);
  digitalWrite(MUXS2,LOW);
  watchdogStart(); 
}

void watchdogStart(void)
{
  cli();  // disable all interrupts 
  wdt_reset(); // reset the WDT timer
  // Enter Watchdog Configuration mode: 
  WDTCSR |= (1<<WDCE) | (1<<WDE); // Set Watchdog settings:
  WDTCSR = (0<<WDIE) | (1<<WDE) | (1<<WDP3) | (0<<WDP2) | (1<<WDP1) | (1<<WDP0); //8s WDT
  sei();
}

void loop()
{
  wdt_reset();
  watchdogStart();
}

void receiveEvent(int howMany)
{
  //int y=0,z=0,w=0,q=0;
  if(1 < Wire.available()) // loop through all but the last
  {
    while(0<Wire.available()){
      char c = Wire.read(); // receive byte as a character
      Serial.println(c);
    }         // print the character
  }
  else{
    x = Wire.read();
  }
  Serial.println(x);         // print the integer
}

void requestEvent()
{
  switch (x){

  case 0x30://ASCII_0
    digitalWrite(MUXS1, HIGH);
    digitalWrite(MUXS2, LOW);
    for(int k=0;k<3;k++){
      value[k] = EEPROM.read(k);
    }
    value[3]='0';
    Serial.println(value);
    digitalWrite(MUXS1, HIGH);
    digitalWrite(MUXS2, HIGH);
    for(int k=0;k<3;k++){
      value[k] = EEPROM.read(k);
    }
    value[3]='0';
    Serial.println(value);

    break;

  case 0x31://ASCII_1
    digitalWrite(MUXS1, LOW);
    digitalWrite(MUXS2, HIGH);
    z=analogRead(1);
    z=z>>2;
    Wire.write(z);
    break;

  case 0x32://ASCII_2
    itoa(analogRead(0),v,10);//Voltage
    itoa(analogRead(1),u,10); //Current
    itoa(analogRead(2),r,10);//Temperature
    strcat(t, v);
    strcat(t, u);
    strcat(t, r);
    Wire.write(t);
    break;

  case 0x33://ASCII_3
    digitalWrite(MUXS1, LOW);
    digitalWrite(MUXS2, HIGH);
    Serial.println("Shello");//uC_CMD_SEND_STRING
    break;

  case 0x34://ASCII_4_ADCS_Data
    digitalWrite(MUXS1, LOW);
    digitalWrite(MUXS2, HIGH);
    Serial.println("Shello");//uC_CMD_SEND_STRING
    break;

  case 0x35://ASCII_5
    Serial.println("Shello");//uC_CMD_SEND_STRING
    break;
  }
  x=999;
  delay(100);
}

The LEDs turn High and low respectively, then timed out and both turned low. I was able to send another command to it soon after.

Could you add a few lines in receivedEvent() , as follows? Let us know what it prints out.

void receiveEvent(int howMany)
{
  //int y=0,z=0,w=0,q=0;
  int count = 0;                                 //*** add this line
  if(1 < Wire.available()) // loop through all but the last
  {
    while(0<Wire.available()){
      char c = Wire.read(); // receive byte as a character
      Serial.println(c);
      count++;                                      // *** add this line
    }         // print the character
    Serial.println(count);                 // *** add this line
  }

Oh, and you can delete the lines as follows. There's no difference, but they are unneeded (put any back in if you find it necessary later, except for String.h, of course, it's a memory hog, and you already know about char arrays.

//#include <Time.h>
//#include <stdlib.h>
#include <Wire.h>
//#include <String.h>
//#include <wdt.h>
#include <avr/wdt.h> 
#include <EEPROM.h>

hi, thanks for your comment. I have added that 3 lines and it printed out "abc0" /n "abc0"
running other non-relavant commands on it and running command '0' again prints "abc0W" /n "abc0W"

void receiveEvent(int howMany)
{
...
  Serial.println(x);         // print the integer
}

void requestEvent()
{
...
    Serial.println(value);

Don't do serial prints inside an ISR (both of those are ISRs).

Hi Nick,

Thanks for the information. However, in that case, how do I achieve my aim to read and print EEPROM data via a I2C command? Any suggestions?

BTW ... the serial prints in receive event is for debugging purposes. while i really need to execute serial prints in request events

while i really need to execute serial prints in request events

That is certainly sad.

However your loop() function isn't doing much. Can't you set a flag, and then print stuff there?

Hi Nick, I am quite a newbie at programming.

If i got what you meant correctly, I should insert a variable in my requestevent and change its value in request event.

Then I do a switch case for that variable in the main loop to do serial prints.

Is that what you mean?

There is no shame in being a newbie. We were all newbies once.

However you might want to consider doing a C tutorial, and working through some of the simpler examples.

Then I do a switch case for that variable in the main loop to do serial prints.

Broadly speaking, yes.

well, i guess that's the only way to solve the problem for now. Thanks Nick, you're great.