I2C send and recieve

Hello, I am currently trying to create a program that can be used to send and recieve data using I2C communication. I have the function mostly correct and all it is supposed to do is send 8 bits and then the other prints those 8 bits. But my error is sketch_oct22a:4: error: variable or field ‘myhandler’ declared void. Any advice?

#include <Wire.h>

void setup() {
  int MOBO = 00000001;
  int PAYLOAD = 00000010;
  int ATTITUDE = 00000011;
  int COMM = 00000100;
  int POWER = 00000101;
  Wire.begin(MOBO);
  Wire.onReceive(myhandler);
  Serial.begin(9600);
}

void loop() {
  Serial.println("Enter the address of the target ard.");
  int TARGET = Serial.read();
  Serial.println("Enter the function you want to send.");
  int function = Serial.read();
  Wire.beginTransmission(TARGET);
  Wire.write(function);
  Wire.endTransmission();
}

void myhandler(8){
  char incomingfun[8];
  while (Wire.available()){
	for (i = 0; i < sizeof(incoming) - 1; i++){
	incoming[i] = Wire.receive();
	}
    Serial.print(incoming);
  }
  Serial.println(); 
}
void myhandler(8)

This... should be void myhandler(int something).

void loop() {

Serial.println(“Enter the address of the target ard.”);
  int TARGET = Serial.read();
  Serial.println(“Enter the function you want to send.”);
  int function = Serial.read();

Trouble brewing here, too.

So now it's mostly compiled, I still don't understand what trouble is brewing in the loop but hey I will get to that some time. But now it is saying "C:6: error: expected ',' or '...' before numeric constant"

Really? But your code is a secret?

How to use this forum

As for trouble brewing: http://www.gammon.com.au/serial

You are reading data before it arrives. Plus you can't read multiple digits like that.

  int ATTITUDE = 00000011;

ATTITUDE is not decimal 11 here. It is decimal 9.

Proof:

  int ATTITUDE = 00000011;

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
  Serial.println (ATTITUDE);
  }  // end of setup

void loop () { }

Output:

9

This is my code so far, after a bit of back and forth binary addresses changed to hex. Sorry for not posting this with my first reply

#include <Wire.h>

void setup() {
  int MOBO = 0x01;
  int PAYLOAD = 0x02;
  int ATTITUDE = 0x03;
  int COMM = 0x04;
  int POWER = 0x05;
  Wire.begin(MOBO);
  Wire.onReceive(myhandler);
  Serial.begin(9600);
}

void loop() {
  Serial.println("Enter the address of the target ard.");
  int TARGET = Serial.read();
  Serial.println("Enter the function you want to send.");
  int function = Serial.read();
  Wire.beginTransmission(TARGET);
  Wire.write(function);
  Wire.endTransmission();
}

void myhandler(int 8){
  char incomingfun[8];
  int i = 0;
  while (Wire.available()){
	for (i = 0; i < sizeof(incomingfun) - 1; i++){
	incomingfun[i] = Wire.read();
	}
    Serial.print(incomingfun);
  }
  Serial.println(); 
}
  Serial.println("Enter the address of the target ard.");
  int TARGET = Serial.read();
  Serial.println("Enter the function you want to send.");
  int function = Serial.read();

This hasn't a hope of working. Read my link above about serial please.

If you want to “block” and get a line of entered text this is closer to what you want:

// get a line from serial
//  ignore spaces, tabs etc.
//  forces to upper case
void getline (char * buf, size_t bufsize)
{
byte i;
  
  // discard any old junk
  while (Serial.available ())
    Serial.read ();
    
  for (i = 0; i < bufsize - 1; )
    {
    if (Serial.available ())
      {
      int c = Serial.read ();
      
      if (c == '\n')  // newline terminates
        break;
      
      if (!isspace (c))  // ignore spaces, carriage-return etc.
        buf [i++] = toupper (c);
      } // end if available
    }  // end of for
  buf [i] = 0;  // terminator
  Serial.println (buf);  // echo what they typed
  }     // end of getline

Then:

  char response [10];
  getline (response, sizeof response);
    
  int TARGET = atoi (response);

Hey thanks, your website is very interesting by the way. If I understand you correctly then this code will correctly process target, but int function will not work?

/*
Some code provided by: Nick Gammon
*/


#include <Wire.h>
#define MOBO  0x01
#define PAYLOAD 0x02
#define ATTITUDE  0x03
#define COMM  0x04
#define POWER  0x05
  

void setup() {
  Wire.begin(MOBO);
  Wire.onReceive(myhandler);
  Serial.begin(9600);
}


// get a line from serial
//  ignore spaces, tabs etc.
//  forces to upper case
void getline (char * buf, size_t bufsize)
{
byte i;
  
  // discard any old junk
  while (Serial.available ())
    Serial.read ();
    
  for (i = 0; i < bufsize - 1; )
    {
    if (Serial.available ())
      {
      int c = Serial.read ();
      
      if (c == '\n')  // newline terminates
        break;
      
      if (!isspace (c))  // ignore spaces, carriage-return etc.
        buf [i++] = toupper (c);
      } // end if available
    }  // end of for
  buf [i] = 0;  // terminator
  Serial.println (buf);  // echo what they typed
  }     // end of getline

void loop() {
  Serial.println("Enter the address of the target ard.");
  char response [8];
  getline (response, sizeof response); 
  int TARGET = atoi (response);
  Serial.println("Enter the function you want to send.");
  int function = Serial.read();
  Wire.beginTransmission(TARGET);
  Wire.write(function);
  Wire.endTransmission();
}

void myhandler(int  howMany){
  char incomingfun[8];
  int i = 0;
  while (Wire.available()){
	for (i = 0; i < sizeof(incomingfun) - 1; i++){
	incomingfun[i] = Wire.read();
	}
    Serial.print(incomingfun);
  }
  Serial.println(); 
}

Hey thanks, your website is very interesting by the way. If I understand you correctly then this code will correctly process target, but int function will not work?

Why do you say that?

void myhandler(int  howMany){

char incomingfun[8];
  int i = 0;
  while (Wire.available()){
for (i = 0; i < sizeof(incomingfun) - 1; i++){
incomingfun[i] = Wire.read();
}
    Serial.print(incomingfun);
  }
  Serial.println();
}

Don’t do serial prints inside an ISR.

See my I2C stuff:

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

Well my reasoning is that while I changed the code for int target, I didn’t change the code for int function which, I assume since they both are just from serial, that int function will not work correctly. Because the code for int function doesn’t use getline.

void myhandler(int  howMany){
  char incomingfun[8];
  int i = 0;
  while (Wire.available()){
	for (i = 0; i < sizeof(incomingfun) - 1; i++){
	incomingfun[i] = Wire.read();
	}
  }
  Serial.print(incomingfun);
  Serial.println(); 
}

Well, yes. You were supposed to change that, from the example for TARGET:

  Serial.println("Enter the address of the target ard.");
  char response [8];
  getline (response, sizeof response); 
  int TARGET = atoi (response);
  Serial.println("Enter the function you want to send.");
  getline (response, sizeof response); 
  int function = atoi (response);

You should read this part: I2C summary

Example of requesting data from a slave:

  byte buf [10];

  if (Wire.requestFrom (SLAVE_ADDRESS, 10) == 10)  // if request for 10 bytes satisfied
    {
    for (byte i = 0; i < 10; i++)
      buf [i] = Wire.read ();
    }
  else
    {
    // failure ... maybe slave wasn't ready or not connected
    }