Arduino 基于特征的伺服电机控制

Arduino 基于特征的伺服电机控制,arduino,arduino-uno,i2c,arduino-ide,servo,Arduino,Arduino Uno,I2c,Arduino Ide,Servo,我一直在制作一个简单的Arduino程序,其中包括两个Arduino UNO之间的从-主I2C通信。主Arduino带有一个伺服电机,从机通过返回一条包含六个字节的消息来返回一个6字节的请求。我希望伺服电机在发送6字节的消息时转动,但如果发送的消息长度大于或小于6字节,我希望伺服电机停止转动。 到目前为止,我已经为master编写了以下代码: // Demonstrates use of the Wire library // Reads data from an I2C/TWI slave d

我一直在制作一个简单的Arduino程序,其中包括两个Arduino UNO之间的从-主I2C通信。主Arduino带有一个伺服电机,从机通过返回一条包含六个字节的消息来返回一个6字节的请求。我希望伺服电机在发送6字节的消息时转动,但如果发送的消息长度大于或小于6字节,我希望伺服电机停止转动。 到目前为止,我已经为master编写了以下代码:

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device

#include <Wire.h>
#include <Servo.h>

Servo servo1;

void setup() {
Wire.begin();        // join i2c bus (address optional for master)
Serial.begin(9600);  // start serial for output
servo1.attach(9);
}

void loop() {
Wire.requestFrom(8, 50);    // request 6 bytes from slave device #8

while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.print(c);         // print the character
if (char c = 150)
{
  servo1.write(180);
}
else {
  servo1.write(90);
}
}

delay(500);
}
现在,当从机发送消息hello时,无论字符长度如何,电机都会全速运行。我做错了什么?谢谢

if (char c = 150)
这是不对的。请确保您理解赋值运算符和关系运算符之间的区别

如果要检查c是否等于150,则必须写入ifc==150