Reputation: 109
I'm trying to send 4 potentiometer values via. i2c after receiving those values through virtual wire from another arduino. My setup consists of 3 arduinos. 1st arduino is connected to transmitter and 4 potentiometers.It sends values via. tx through virtualwire. 2nd arduino consists of receiver. 3rd arduino consists of 4 servos. 2nd and 3rd arduino are coupled via. an i2c bus. im not able to send all 4 values of potrentiometer. however i'm able to send a single value of potentiometer. here's my sketch.
#include <VirtualWire.h>
#include <Wire.h>
int Sensor1Data; // VARIABLE WHERE THE ANALOG VALUE OF POT 1 GOT STORED BY THE TX
int Sensor2Data; // VARIABLE WHERE THE ANALOG VALUE OF POT 2 GOT STORED BY THE TX
int Sensor3Data; // VARIABLE WHERE THE ANALOG VALUE OF POT 3 GOT STORED BY THE TX
int Sensor4Data; // VARIABLE WHERE THE ANALOG VALUE OF POT 4 GOT STORED BY THE TX
int data[4];
char StringReceived[22];
void setup()
{
Wire.begin(); // START I2C (WIRE.h)
vw_setup(6000); // BAUDERATE FOR VIRTUALWIRE
vw_set_rx_pin(11); // DEFINE PIN FOR VIRTUALWIRE
vw_rx_start(); // START VIRTUALWIRE
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) { // GET THE DATA
int i;
for (i = 0; i < buflen; i++)
{ // CHECKSUM OK ? GET MESSAGE
StringReceived[i] = char(buf[i]); // FILL THE ARRAY
}
sscanf(StringReceived, "%d,%d,%d,%d,%d,%d",&Sensor1Data, &Sensor2Data,&Sensor3Data,&Sensor4Data); // Converts a string to an array
Sensor1Data = map(Sensor1Data, 0, 1023, 0, 180); // MAPS THE 8BIT SERVODATA TO SERVOMIN/MX
Sensor2Data = map(Sensor2Data, 0, 1023, 26, 160); // MAPS THE 8BIT SERVODATA TO SERVOMIN/MX
Sensor3Data = map(Sensor3Data, 0, 1023, 26, 160); // MAPS THE 8BIT SERVODATA TO SERVOMIN/MX
Sensor4Data = map(Sensor4Data, 0, 1023, 26, 160);
data[0] = Sensor1Data;
data[1] = Sensor2Data;
data[2] = Sensor3Data;
data[3] = Sensor4Data;// MAPS THE 8BIT SERVODATA TO SERVOMIN/MX
Wire.beginTransmission(8); // OPENS AN I2C ON PIN 8
Wire.write(data,4);
// SEND POT 1 VALUE TO I2C DEVICE 8
Wire.endTransmission(); // END TRANSMISSION
// SEND POT 1 VALUE TO I2C DEVICE 8
}
memset( StringReceived, 0, sizeof( StringReceived)); // RESET STRING RECEIVED
}`
the error this sketch is showing
no matching function for call to 'TwoWire::write(int [4], int)'
Upvotes: 0
Views: 245
Reputation: 6781
You can't send an array of ints with TwoWire::write
. It only accepts a single byte or an array of bytes.
Because your array values aren't bigger than 160, you can just change your array into a byte array.
change:
int data[4];
to:
byte data[4];
Upvotes: 2