Sending 4 pot values with I2C from one arduino to another

adrianskalni:
Now I understand but how do I put the 4 pot values in the

int myData[4];

Like this?

int myData[pot1, pot2, pot3, pot4];

No. Like this

myData[0] = pot1;
myData[1] = pot2;
// etc

...R

So like this?

Master:

/*
   Set delay time on delayTime




*/

int delayTime = 50;







int pot1 = A0;
int pot2 = A1;
int pot3 = A2;
int pot4 = A3;

int potValue1 = 0;
int potValue2 = 0;
int potValue3 = 0;
int potValue4 = 0;

int led = 10;
int button = 8;

int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button


int output1 = A4;
int output2 = A5;
int output3 = A6;
int output4 = A7;

#include<Wire.h>
#include <LiquidCrystal.h> // includes the LiquidCrystal Library 
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
#define SLAVE_ADDR 9

int myData[0] = pot1;
int myData[1] = pot2;
int myData[2] = pot3;
int myData[3] = pot4;



void setup() {
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);

  // Open serial communications and wait for port to open:
  Wire.begin();

  lcd.begin(16, 2);
  lcd.print("   ArduinoTX");
  digitalWrite(led, HIGH);
  delay(600);
  digitalWrite(led, LOW);
  delay(600);
  digitalWrite(led, HIGH);
  delay(600);
  digitalWrite(led, LOW);
  delay(600);
  digitalWrite(led, HIGH);
  delay(600);
  delay(3000);
  lcd.clear();


}

void loop() {

  currentButtonState = digitalRead(button);
  potValue1 = analogRead(pot1);
  potValue2 = analogRead(pot2);
  potValue3 = analogRead(pot3);
  potValue4 = analogRead(pot4);

  int sendValue = map(potValue4, 1, 1023, 1, 255);  //converting to values 255 max 
  int sendValue2 = map(potValue3, 1, 1023, 1, 255);
  int sendValue3 = map(potValue2, 1, 1023, 1, 255);
  int sendValue4 = map(potValue1, 1, 1023, 1, 255);

  Wire.beginTransmission(SLAVE_ADDR);
Wire.write( (byte*) myData, 8);
Wire.endTransmission();    // this is what actually sends the data
 

  lastButtonState  = currentButtonState;      // save the last state
  currentButtonState = digitalRead(button); // read new state




  if (lastButtonState == LOW && currentButtonState == LOW) { //dont worry about this

    lcd.print(potValue4);
    lcd.print("        ");
    lcd.print(potValue2);
    delay(delayTime);
    lcd.clear();
    lcd.setCursor(0, 2);
    lcd.print(potValue3);
    lcd.print("        ");
    lcd.print(potValue1);
    delay(delayTime);
    lcd.clear();


  }

}

Slave:

int rd;
int br;
#include <Wire.h>

#define SLAVE_ADDR 9

volatile int myData[4];
int newRxData;



void setup() {
  // put your setup code here, to run once:
  Wire.begin(SLAVE_ADDR);
   
  // Function to run when data received from master
  Wire.onReceive(receiveEvent);
  
  // Setup Serial Monitor 
  Serial.begin(9600);
}
void receiveEvent() {
 Wire.readBytes( (byte*) myData, 8);
 newRxData = true;
 Serial.println(myData);
}

void loop() {
  delay(50);

}

In this code
int myData[0] = pot1;
int myData[1] = pot2;
int myData[2] = pot3;
int myData[3] = pot4;
you are putting the pin numbers for the potentiometers into the array - and the array was not created properly. I'm pretty sure you don't want to send the pin numbers.

It should be like this

Before setup() define the array with 4 elements like this

int myData[4];

and in loop() do this

  myData[0] = map(potValue4, 1, 1023, 1, 255);  //converting to values 255 max 
  myData[1]= map(potValue3, 1, 1023, 1, 255);
  myData[2] = map(potValue2, 1, 1023, 1, 255);
  myData[3] = map(potValue1, 1, 1023, 1, 255);

...R

when I try to upload the slave code it shows this error

exit status 1 no matching function for call to 'println(volatile int [4])'

The "C" and "C++" language do only what you tell them to do, they can not do magic.

You probably have: Serial.println( myData)
But the Serial.println() does not know how to print an array.
It does know how to print an integer, so you have to print integers:

Serial.println( myData[0]);
Serial.println( myData[1]);
Serial.println( myData[2]);
Serial.println( myData[3]);

We prefer that you show the full sketch that shows the problem.
Some on this forum have a punchline: "the problem is in the part that you are not showing us".
My answer is therefor just a guess, but I hope it is a good guess :wink:

adrianskalni:
when I try to upload the slave code it shows this error

exit status 1 no matching function for call to 'println(volatile int [4])'

You can't print a complete array. You need to print it one element at a time - for example

for (byte n = 0; n < 4; n++) {
  Serial.println(myData[n]);
}

Also you should NOT have any Serial.print calls in the function receiveEvent() as it is an Interrupt Service Routine (ISR).

In loop() you should do this

if (newRxData == true) {
   // code to print the data
   newRxData = false;
}

...R

Thank you for responding fast

So I'm getting only zeros in the console
Master:

/*
   Set delay time on delayTime




*/

int delayTime = 50;







int pot1 = A0;
int pot2 = A1;
int pot3 = A2;
int pot4 = A3;

int potValue1 = 0;
int potValue2 = 0;
int potValue3 = 0;
int potValue4 = 0;

int led = 10;
int button = 8;

int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button


int output1 = A4;
int output2 = A5;
int output3 = A6;
int output4 = A7;

#include<Wire.h>
#include <LiquidCrystal.h> // includes the LiquidCrystal Library 
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
#define SLAVE_ADDR 9

int myData[4];



void setup() {
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);

  // Open serial communications and wait for port to open:
  Wire.begin();

  lcd.begin(16, 2);
  lcd.print("   ArduinoTX");
  digitalWrite(led, HIGH);
  delay(600);
  digitalWrite(led, LOW);
  delay(600);
  digitalWrite(led, HIGH);
  delay(600);
  digitalWrite(led, LOW);
  delay(600);
  digitalWrite(led, HIGH);
  delay(600);
  delay(3000);
  lcd.clear();


}

void loop() {

  currentButtonState = digitalRead(button);
  potValue1 = analogRead(pot1);
  potValue2 = analogRead(pot2);
  potValue3 = analogRead(pot3);
  potValue4 = analogRead(pot4);

  myData[0] = map(potValue4, 1, 1023, 1, 255);  //converting to values 255 max
  myData[1] = map(potValue3, 1, 1023, 1, 255);
  myData[2] = map(potValue2, 1, 1023, 1, 255);
  myData[3] = map(potValue1, 1, 1023, 1, 255);

  Wire.beginTransmission(SLAVE_ADDR);
  Wire.write( (byte*) myData, 8);
  Wire.endTransmission();    // this is what actually sends the data


  lastButtonState  = currentButtonState;      // save the last state
  currentButtonState = digitalRead(button); // read new state




  if (lastButtonState == LOW && currentButtonState == LOW) { //dont worry about this

    lcd.print(potValue4);
    lcd.print("        ");
    lcd.print(potValue2);
    delay(delayTime);
    lcd.clear();
    lcd.setCursor(0, 2);
    lcd.print(potValue3);
    lcd.print("        ");
    lcd.print(potValue1);
    delay(delayTime);
    lcd.clear();


  }
}

Slave:

int rd;
int br;
#include <Wire.h>

#define SLAVE_ADDR 9

volatile int myData[4];
int newRxData;



void setup() {
  // put your setup code here, to run once:
  Wire.begin(SLAVE_ADDR);

  // Function to run when data received from master
  Wire.onReceive(receiveEvent);

  // Setup Serial Monitor
  Serial.begin(9600);
}
void receiveEvent() {
 
}

void loop() {
   Wire.readBytes( (byte*) myData, 8);
  newRxData = true;
  Serial.print( myData[0]);
  Serial.print("  ");
  Serial.print( myData[1]);
  Serial.print("  ");
  Serial.print( myData[2]);
  Serial.print("  ");
  Serial.println( myData[3]);
}

I made spaces between the mydata so the values wouldn't mix and maybe I have to put this :

Wire.readBytes( (byte*) myData, 8);
  newRxData = true;
  Serial.print( myData[0]);
  Serial.print("  ");
  Serial.print( myData[1]);
  Serial.print("  ");
  Serial.print( myData[2]);
  Serial.print("  ");
  Serial.println( myData[3]);

in to here:

void receiveEvent() {
 
}
int rd;
int br;
#include <Wire.h>

#define SLAVE_ADDR 9

volatile int myData[4];
int newRxData;



void setup() {
  // put your setup code here, to run once:
  Wire.begin(SLAVE_ADDR);

  // Function to run when data received from master
  Wire.onReceive(receiveEvent);

  // Setup Serial Monitor
  Serial.begin(9600);
}
void receiveEvent() {
  Wire.readBytes( (byte*) myData, 8);
  newRxData = true;
}

void loop() {
  if (newRxData == true) {
    for (byte n = 0; n < 4; n++) {
      Serial.println(myData[n]);
    }
    newRxData = false;
  }

}

I tried doing this but it didn't work maybe I did it wrong then I don't know

Also this doesn't work anymore:

 if (lastButtonState == HIGH && currentButtonState == LOW) { //dont worry about this

    lcd.print(potValue4);
    lcd.print("        ");
    lcd.print(potValue2);
    delay(delayTime);
    lcd.clear();
    lcd.setCursor(0, 2);
    lcd.print(potValue3);
    lcd.print("        ");
    lcd.print(potValue1);
    delay(delayTime);
    lcd.clear();


  }

I know its nothing special but it's like debugging to check if the pots work

@adrianskalni, it is just confusing when you make several Posts in succession without allowing time between them for a Reply.

Please post the latest version of your programs and describe what actually happens when you run them.

...R

When I upload this code to the master and slave I tried robins method and koepels method on sending and receiving the values koepels method I'm having zeros in the console robins method didn't even show anything also this function didn't work:

if (lastButtonState == HIGH && currentButtonState == LOW) { 
[color=#222222][/color]
    lcd.print(potValue4);
    lcd.print("        ");
    lcd.print(potValue2);
    delay(delayTime);
    lcd.clear();
    lcd.setCursor(0, 2);
    lcd.print(potValue3);
    lcd.print("        ");
    lcd.print(potValue1);
    delay(delayTime);
    lcd.clear();

  }

Master:

/*
   Set delay time on delayTime




*/

int delayTime = 50;







int pot1 = A0;
int pot2 = A1;
int pot3 = A2;
int pot4 = A3;

int potValue1 = 0;
int potValue2 = 0;
int potValue3 = 0;
int potValue4 = 0;

int led = 10;
int button = 8;

int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button


int output1 = A4;
int output2 = A5;
int output3 = A6;
int output4 = A7;

#include<Wire.h>
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
#define SLAVE_ADDR 9

int myData[4];



void setup() {
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);

  // Open serial communications and wait for port to open:
  Wire.begin();

  lcd.begin(16, 2);
  lcd.print("   ArduinoTX");
  digitalWrite(led, HIGH);
  delay(600);
  digitalWrite(led, LOW);
  delay(600);
  digitalWrite(led, HIGH);
  delay(600);
  digitalWrite(led, LOW);
  delay(600);
  digitalWrite(led, HIGH);
  delay(600);
  delay(3000);
  lcd.clear();


}

void loop() {

  currentButtonState = digitalRead(button);
  potValue1 = analogRead(pot1);
  potValue2 = analogRead(pot2);
  potValue3 = analogRead(pot3);
  potValue4 = analogRead(pot4);

  myData[0] = map(potValue4, 1, 1023, 1, 255);  //converting to values 255 max
  myData[1] = map(potValue3, 1, 1023, 1, 255);
  myData[2] = map(potValue2, 1, 1023, 1, 255);
  myData[3] = map(potValue1, 1, 1023, 1, 255);

  Wire.beginTransmission(SLAVE_ADDR);
  Wire.write( (byte*) myData, 8);
  Wire.endTransmission();    // this is what actually sends the data


  lastButtonState  = currentButtonState;      // save the last state
  currentButtonState = digitalRead(button); // read new state




  if (lastButtonState == HIGH && currentButtonState == LOW) { //dont worry about this

    lcd.print(potValue4);
    lcd.print("        ");
    lcd.print(potValue2);
    delay(delayTime);
    lcd.clear();
    lcd.setCursor(0, 2);
    lcd.print(potValue3);
    lcd.print("        ");
    lcd.print(potValue1);
    delay(delayTime);
    lcd.clear();


  }
}

Slave:

int rd;
int br;
#include <Wire.h>

#define SLAVE_ADDR 9

volatile int myData[4];
int newRxData;



void setup() {
  // put your setup code here, to run once:
  Wire.begin(SLAVE_ADDR);

  // Function to run when data received from master
  Wire.onReceive(receiveEvent);

  // Setup Serial Monitor
  Serial.begin(9600);
}
void receiveEvent() {
 Wire.readBytes( (byte*) myData, 8);
  newRxData = true;
}

void loop() {
  Serial.print( myData[0]);
  Serial.print("  ");
  Serial.print( myData[1]);
  Serial.print("  ");
  Serial.print( myData[2]);
  Serial.print("  ");
  Serial.println( myData[3]);
}

That's all for now!

In your Slave program you have not taken account of all that I said in Reply #8. The code in loop() should be like this

void loop() {
    if (newRxData == true) {
        Serial.print( myData[0]);
        Serial.print("  ");
        Serial.print( myData[1]);
        Serial.print("  ");
        Serial.print( myData[2]);
        Serial.print("  ");
        Serial.println( myData[3]);
        
        newRxData = false;
    }
}

and the definition of newRxData should be

volatile boolean newRxData;

...R

PS ... You have had all these niggling little errors. They could have been avoided by starting with the exact code from my working I2C tutorial and gradually adapting it to your project. If you made a change that breaks things you can easily go back a stage to the working version.

I fixed my code but I'm still getting nothing in the console

adrianskalni:
I fixed my code but I'm still getting nothing in the console

And we are not getting your "fixed" code.

Have you tried the example from my Tutorial without any changes to it?

...R

Master:

/*
   Set delay time on delayTime




*/

int delayTime = 50;







int pot1 = A0;
int pot2 = A1;
int pot3 = A2;
int pot4 = A3;

int potValue1 = 0;
int potValue2 = 0;
int potValue3 = 0;
int potValue4 = 0;

int led = 10;
int button = 8;

int lastButtonState;    // the previous state of button
int currentButtonState; // the current state of button


int output1 = A4;
int output2 = A5;
int output3 = A6;
int output4 = A7;

#include<Wire.h>
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
#define SLAVE_ADDR 9

int myData[4];



void setup() {
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);

  // Open serial communications and wait for port to open:
  Wire.begin();

  lcd.begin(16, 2);
  lcd.print("   ArduinoTX");
  digitalWrite(led, HIGH);
  delay(600);
  digitalWrite(led, LOW);
  delay(600);
  digitalWrite(led, HIGH);
  delay(600);
  digitalWrite(led, LOW);
  delay(600);
  digitalWrite(led, HIGH);
  delay(600);
  delay(3000);
  lcd.clear();


}

void loop() {

  currentButtonState = digitalRead(button);
  potValue1 = analogRead(pot1);
  potValue2 = analogRead(pot2);
  potValue3 = analogRead(pot3);
  potValue4 = analogRead(pot4);

  myData[0] = map(potValue4, 1, 1023, 1, 255);  //converting to values 255 max
  myData[1] = map(potValue3, 1, 1023, 1, 255);
  myData[2] = map(potValue2, 1, 1023, 1, 255);
  myData[3] = map(potValue1, 1, 1023, 1, 255);

  Wire.beginTransmission(SLAVE_ADDR);
  Wire.write( (byte*) myData, 8);
  Wire.endTransmission();    // this is what actually sends the data


  lastButtonState  = currentButtonState;      // save the last state
  currentButtonState = digitalRead(button); // read new state




  if (lastButtonState == HIGH && currentButtonState == LOW) { //dont worry about this

    lcd.print(potValue4);
    lcd.print("        ");
    lcd.print(potValue2);
    delay(delayTime);
    lcd.clear();
    lcd.setCursor(0, 2);
    lcd.print(potValue3);
    lcd.print("        ");
    lcd.print(potValue1);
    delay(delayTime);
    lcd.clear();


  }
}

Slave:

int rd;
int br;
#include <Wire.h>

#define SLAVE_ADDR 9

volatile int myData[4];
volatile boolean newRxData;



void setup() {
  // put your setup code here, to run once:
  Wire.begin(SLAVE_ADDR);

  // Function to run when data received from master
  Wire.onReceive(receiveEvent);

  // Setup Serial Monitor
  Serial.begin(9600);
}
void receiveEvent() {
 Wire.readBytes( (byte*) myData, 8);
  newRxData = true;
}

void loop() {
   if (newRxData == true) {
        Serial.print( myData[0]);
        Serial.print("  ");
        Serial.print( myData[1]);
        Serial.print("  ");
        Serial.print( myData[2]);
        Serial.print("  ");
        Serial.println( myData[3]);
        
        newRxData = false;
    }


}

adrianskalni:
Master:

You did not answer my question in Reply #34

...R

I tried understanding the example code but from what I understood In my opinion, I have everything right in my code. Your example code is really complicated for me maybe I'm just dumb or something but I just cannot process your code.

adrianskalni:
I tried understanding the example code but from what I understood In my opinion, I have everything right in my code. Your example code is really complicated for me maybe I'm just dumb or something but I just cannot process your code.

For the moment. never mind about understanding it. Just try it and see if it works.

It seems to me that you don't yet have the level of programming knowledge needed to debug your own program so it should be easier to start from a working program.

...R

I have looked over my code a hundred times and have noticed that my code gets stuck after the Wire.write();
And also which example are you talking about? Like the Master to Slave right?