我正试图通过发送4个电位计值。 i2c从另一个arduino通过虚拟线路接收到这些值之后。 我的设置包括3个arduinos。第一个arduino连接到变送器和4个电位器。它通过发送值。 tx通过virtualwire。 第二个arduino由接收器组成。 第3 arduino由4个伺服系统组成。第2和第3 arduino通过耦合。一辆i2c总线。 我无法发送所有4个potrentiometer值。但是我能够发送一个单独的电位计值。 这是我的草图。
#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)'
答案 0 :(得分:2)
您无法使用import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
public class DemoPoly {
private JFrame frame;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new DemoPoly().createAndShowGui();
}
});
}
public void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
CustomPanel cp = new CustomPanel();
cp.setBackground(Color.RED);
frame.add(cp);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
class CustomPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
}
发送一组整数。它只接受一个字节或一个字节数组。
因为您的数组值不大于160,所以您只需将数组更改为字节数组。
变化:
TwoWire::write
为:
int data[4];