Help once again needed with wire.h and communication between 2 arduinos

Hello ,

I am trying to use the wire.h library to send from an arduino to the other messages, using a button. Every time the button is clicked it should send a message. The receiver arduino for the very first time it receives the command, afterwards whenever I press the button it does not receive any string more.
Please help me thank you very much

Here is the sender code.

#include <Wire.h>
const int buttonPin=2;
int buttonState=0;
void setup() {
 Serial.begin(9600);
 pinMode(buttonPin,INPUT);
 // put your setup code here, to run once:
 Wire.begin();
 //
}
byte x=0;
void sendMessage(String msg)
{
 Wire.beginTransmission(8);
 byte bmsg[msg.length()];
 for (int i=0;i<msg.length();i++)
 {
   bmsg=(byte) msg;
   Wire.write(bmsg);
 }

 Wire.endTransmission(false);

}
int mode=0;
void loop() {
 Serial.println("waiting...");
 buttonState=digitalRead(buttonPin);
 if (buttonState==HIGH)
 {
   if (mode==0)
   {
     sendMessage("MNI 3;");
     mode++;
   }
   if (mode==1)
   {
     sendMessage("MNSSMP 3 0 232 5000;");
      mode++;
   }
   if (mode==2)
   {
     sendMessage("MNSSMP 3 0 24 5000;");
     mode--;
   }
 
 }

 
 
 delay(1000);

}

And the receiver event method

void receiveEvent(int howMany) {


 //Serial.println("Receive Event called");
 while(1<Wire.available()) {
   Serial.println("Inside the loop");
   char c=Wire.read();
   Serial.print(c);
   commandSended+=c;
  
  
 }

 Serial.println("recevt ");
 Serial.println(commandSended);
 gotCommand=true;

}

Now with more code tags and fewer italics please

corrected the code no Italics. Thank you

But still no code tags
Ho-hum

Read How to post code properly and do it.

Pete

Now I've changed it... Thx now I will need your help people.

Thank you

Excellent.

Now, you have not posted all the receiver code. It would help to see all of it.

The receiver arduino for the very first time it receives the command

It would also help if you showed, exactly what the receiver prints - copy and paste the serial monitor output into a message between code tags.

I don't think the transmitting code is doing what you intend.

 if (buttonState == HIGH)
  {
    if (mode == 0)
    {
      sendMessage("MNI 3;");
      mode++;
    }
    if (mode == 1)
    {
      sendMessage("MNSSMP 3 0 232 5000;");
      mode++;
    }
    if (mode == 2)
    {
      sendMessage("MNSSMP 3 0 24 5000;");
      mode--;
    }

  }

When you first push the button, mode is zero so the message "MNI 3;" is sent. 'mode' is then incremented and now it is 1. Because of this, message "MNSSMP 3 0 232 5000;" is immediately sent and then mode is incremented to 2. Therefore, the message "MNSSMP 3 0 24 5000;" will be sent as well. So, all three messages are sent immediately one after the other. Now mode is decremented and has the value 1 and the code then delays for one second by which time, presumably, the button has been released.
The next time you push the button, messages "MNSSMP 3 0 232 5000;" and "MNSSMP 3 0 24 5000;" will be sent and then the code will delay for one second again and this will be repeasted each time the button is pressed.

However, I am not sure what the sendMessage function actually does. It doesn't look right. In fact, with the latest Arduino IDE 1.8.4, it doesn't compile. For example, the compiler doesn't like this statement:

   bmsg = (byte) msg;

'msg' is declared to be a String and you can't cast a String to byte.
Since you are passing C-style null-terminated strings (NOTE the lower case 's') to the sendMessage function, it doesn't make sense to try to treat the string as if it was a String.

Pete