I2C Master send reset command to the slave counter My name is Gianni, I live in Olbia Sardinia Italy and I have some experience in electronics.

For several years I have been passionate about Arduino.

They are now engaged in a project to counter with hall sensors.

The slave sends data to the master via I2C, for my project. I'm using the sketch of Gammon which uses a structure enum to send a request to the slave.

I have the need to send a reset command from master to slave.

I edited the sketch and I can see the command on the slave but will not let me use it to reset the counter.



Master code section

   void RstCounter() 
  {

  Serial.println ("funzione azzera attivata");  
   Serial.println (sensor);
  char key = keypad.getKey();   
  if (key !='A')  
  {

   char cmdrst = 'a';
   //set=key; 

     if (sensor==11)
    {
    sendCommand (CMD_WRITE_D2, 1);

    Wire.beginTransmission(SLAVE_ADDRESS); // testiamo questa istruzione
    Wire.write(cmdrst);    
    Wire.endTransmission();

    delay(100);
    Serial.println (cmdrst);  
    }

   else 
  {
  m=0;
  }

   azzera =true; 
  }
   else if (key=='A') 
    { 
   Serial.println("");
   Serial.println("AZZERA close");
   azzera = false;
   }
     delay(100);
    }




SLAVE code section

#include &lt;Wire.h&gt;

const byte MY_ADDRESS = 42; // address in valore esadecimale

/************  VARIABILI SENSORI **********************/

#define Sensor11 2  
int lastValueSensor11 =0;
int ValueSensor11=0;
int contatore11=0;
int counter11;



// various commands we might get
/************  VARIABILI ENUMERATORE **********************/
enum {
    CMD_ID = 1,
    CMD_READ_D2  = 2,
    CMD_READ_D3 = 3,
    CMD_READ_D4 = 4,
    CMD_WRITE_D2 = 5
    };

char command;
int sensor;
char cmdrst;

/************  SETUP **********************/
void setup() 
  {
  command = 0;

  pinMode (Sensor11, INPUT);
  digitalWrite (Sensor11, HIGH);  // enable pull-up

  Serial.begin(9600);
  Wire.begin (MY_ADDRESS);
  Wire.onReceive (receiveEvent);  // interrupt handler for incoming messages
  Wire.onRequest (requestEvent);  // interrupt handler for when data is wanted

  }  // end of setup

/************  LOOP **********************/
void loop() 
  {
        /************************ CONTATORE SENSOR 1 ***********************/
   ValueSensor11  = digitalRead(Sensor11); 

            if ( ValueSensor11 != lastValueSensor11) 
               { if ( ValueSensor11 == LOW)
                    { 
                      contatore11 = contatore11 + 1; 

                        }
                     }
   lastValueSensor11 = ValueSensor11;



/************  I2C RECEIVE **********************/
void receiveEvent (int howMany)
  {

  command = Wire.read ();  // remember command for when we get request
  //Serial.println(command);
  switch (command)
     {
     case CMD_WRITE_D2:  
     Serial.println("comando 5 ricevuto");     
     char cmdrst=Wire.read();
     delay(100);        
     Serial.println (cmdrst);
     break;
    }
} // end of receiveEvent


void sendSensor1 (const byte which)
  {
  int val11 = contatore11;
  Serial.println(contatore11);
  byte buf11 [2];

    buf11 [0] = val11 &gt;&gt; 8;
    buf11 [1] = val11 &amp; 0xFF;
    Wire.write (buf11, 2);
  }  // end of sendSensor



void requestEvent ()
  {
  switch (command)
     {
     case CMD_ID:       
     Wire.write (0x55); 
     break;   // send our ID 


     case CMD_READ_D2:  
     sendSensor1 (2);    
     break;  // send A0 value


     case CMD_READ_D3:  
     sendSensor2 (3); 
     break;    // send D8 value


     case CMD_READ_D4:  
     sendSensor3 (4); 
     break;

     }  // end of switch

  }  // end of requestEvent
void receiveEvent (int howMany)
  {

  command = Wire.read ();  // remember command for when we get request
  //Serial.println(command);
  switch (command)
     {
     case CMD_WRITE_D2:  
     Serial.println("comando 5 ricevuto");     
     char cmdrst=Wire.read();
     delay(100);        
     Serial.println (cmdrst);
     break;
    }
} // end of receiveEvent



Don't do serial prints inside an interrupt service routine (ISR). That is, not here. 
Don't do delay inside an ISR.




  if (key !='A')  
  {


I would be checking for NO_KEY first, otherwise you will assume that no key press is not 'A' and be doing that transmission to the slave a lot.