0

我正在尝试通过发送 4 个电位器值。i2c 通过虚拟线路从另一个 arduino 接收这些值后。我的设置由 3 个 arduino 组成。第一个 arduino 连接到变送器和 4 个电位器。它通过发送值。通过虚拟线发送。第二个arduino由接收器组成。第三个 arduino 由 4 个伺服系统组成。第二和第三个arduino通过耦合。i2c 总线。我无法发送电位器的所有 4 个值。但是我可以发送一个电位器值。这是我的草图。

  #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
 }`

此草图显示的错误

     no matching function for call to 'TwoWire::write(int [4], int)'
4

1 回答 1

2

您不能使用TwoWire::write. 它只接受单个字节或字节数组。

因为您的数组值不大于 160,所以您可以将数组更改为字节数组。

改变:

int data[4];

至:

byte data[4];
于 2017-02-23T17:56:05.657 回答