0

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 <Wire.h>
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 >> 8;
 buf11 [1] = val11 & 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
Nick Gammon
38.9k13 gold badges69 silver badges125 bronze badges
asked Sep 24, 2015 at 20:53

1 Answer 1

1
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.

answered Sep 24, 2015 at 21:42
1
  • Was the answer helpful? If so, please accept it by clicking on the "tick" icon next to the answer, and also possibly upvote it. This indicates to other users that you found the answer useful, and it also stops Stack Exchange from periodically "bumping" your question in the hope of getting an accepted answer. Thanks for your understanding and cooperation! Commented Jan 23, 2016 at 3:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.