Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用处理在RPI和Arduino之间实现I2C_Arduino_Raspberry Pi_Processing_I2c - Fatal编程技术网

使用处理在RPI和Arduino之间实现I2C

使用处理在RPI和Arduino之间实现I2C,arduino,raspberry-pi,processing,i2c,Arduino,Raspberry Pi,Processing,I2c,这里发布的是我的RPi主程序和Arduino从程序的代码。我有一个连接到Arduino的模拟传感器,我正在通过RPi上的处理读取这些数据。我之所以使用Processing,是因为我打算用数据生成图形和波形。下面的代码似乎可以工作,但是,设置的任何轻微移动都会“断开”从设备,因为我收到以下消息。“设备没有响应。请检查布线以及您使用的地址是否正确。”我已经缩小了问题范围,发现它总是在i2c.read()处断开连接函数。我的问题是,是否存在某种类型的中断函数,以便在发生这种情况时,处理继续进行,并在下

这里发布的是我的RPi主程序和Arduino从程序的代码。我有一个连接到Arduino的模拟传感器,我正在通过RPi上的处理读取这些数据。我之所以使用Processing,是因为我打算用数据生成图形和波形。下面的代码似乎可以工作,但是,设置的任何轻微移动都会“断开”从设备,因为我收到以下消息。“设备没有响应。请检查布线以及您使用的地址是否正确。”我已经缩小了问题范围,发现它总是在
i2c.read()处断开连接函数。我的问题是,是否存在某种类型的中断函数,以便在发生这种情况时,处理继续进行,并在下一次迭代中重试?或者,如果它卡在环路中,它是否在等待从设备发出的信号?有人对如何处理这个问题有什么建议吗

处理代码

import processing.io.*;
I2C i2c;
int val = 0;
void setup()
{
 i2c = new I2C(I2C.list()[0]);
}
void draw ()
{
if (I2C.list() != null)
{
 i2c.beginTransmission(0x04);
 i2c.write(8);
 byte[] in = i2c.read(1);
 int accel = in[0];
 println (accel);
}
}
阿杜伊诺代码

 #include <Wire.h>
 #define SLAVE_ADDRESS 0x04
 int number = 5;
 int state = 0;
 const int zInput = A0;
 int zRawMin = 493;
 int zRawMax = 530;
 float acceleration;
 int accel;
 void setup() {
 analogReference(EXTERNAL);
 pinMode(13,OUTPUT);
 Serial.begin(9600);           // start serial for output
 Wire.begin(SLAVE_ADDRESS);                // join i2c bus with address #8
 Wire.onReceive(receiveData); // register event
 Wire.onRequest(sendData);
 Serial.println ("Ready");
 }
 void loop() {
 int zRaw = ReadAxis (zInput);
 acceleration = map (float(zRaw), float (zRawMin), float(zRawMax), -9.81, 9.81);
 accel = int(acceleration);
 //delay(100);
 }
 void receiveData(int byteCount) 
 {
 while (0 < Wire.available()) 
 { // loop through all but the last
 number = Wire.read(); // receive byte as a character
 //Serial.print("data received");
 Serial.println(number);         // print the character
 if (number==1)
 {
 if (state == 0)
 {
 digitalWrite(13,HIGH);
 state = 1;
 }
 else
 {
 digitalWrite(13,LOW);
 state = 0;
 }
 }
 }
 }
 void sendData()
 {
 Wire.write(accel);
 }
 int ReadAxis(int axisPin)
 {
 long reading = 0;
 int raw = analogRead(axisPin);
 return raw;
 } 
#包括
#定义从站地址0x04
整数=5;
int state=0;
常数int zInput=A0;
int-zRawMin=493;
int-zRawMax=530;
浮子加速度;
int accel;
无效设置(){
类比参考(外部);
pinMode(13,输出);
Serial.begin(9600);//开始串行输出
Wire.begin(从#u地址);//用地址#8连接i2c总线
Wire.onReceive(receiveData);//注册事件
Wire.onRequest(发送数据);
Serial.println(“就绪”);
}
void循环(){
int-zRaw=读取轴(zInput);
加速度=地图(浮动(zRaw)、浮动(zRawMin)、浮动(zRawMax),-9.81、9.81);
加速度=int(加速度);
//延迟(100);
}
无效接收数据(整数字节计数)
{
而(0
看起来解决方案可能是使用@Kevin Workman提供的try/catch块。对于我需要它做的事情,它工作得很好

这是更新后的处理代码

import processing.io.*;
I2C i2c;
int val = 0;
void setup()
{
 i2c = new I2C(I2C.list()[0]);
}
void draw ()
{
if (I2C.list() != null)
{
 i2c.beginTransmission(0x04);
 i2c.write(8);

 try
 {
 byte[] in = i2c.read(1);
 }
 catch(Exception e)
 {
  i2c.endTransmission();
 }
 int accel = in[0];
 println (accel);
}
}

看起来解决方案可能是使用@Kevin Workman提供的try/catch块。对于我需要它做的事情,它工作得很好

这是更新后的处理代码

import processing.io.*;
I2C i2c;
int val = 0;
void setup()
{
 i2c = new I2C(I2C.list()[0]);
}
void draw ()
{
if (I2C.list() != null)
{
 i2c.beginTransmission(0x04);
 i2c.write(8);

 try
 {
 byte[] in = i2c.read(1);
 }
 catch(Exception e)
 {
  i2c.endTransmission();
 }
 int accel = in[0];
 println (accel);
}
}

你从哪里得到这个信息的?这是个例外吗?您是否尝试过将对
read()
的调用包装到try/catch块中?Kevin,我在处理控制台中得到了它。不确定是否是异常,但我将尝试try/catch块,看看它是否有效。@KevinWorkman我尝试过它,似乎效果很好。请查看下面的内容。您从哪里获得该消息?这是个例外吗?您是否尝试过将对
read()
的调用包装到try/catch块中?Kevin,我在处理控制台中得到了它。不确定是否是异常,但我将尝试try/catch块,看看它是否有效。@KevinWorkman我尝试过它,似乎效果很好,请在下面查看。