首页 文章

基于特征控制 Servo 电机

提问于
浏览
0

我一直在制作一个简单的Arduino程序,它涉及2个Arduino UNO之间的Slave-Master I2C通信 . Master Arduino附有 Servo 电机,从机通过返回6字节的消息返回6字节的请求 . 我希望每当发送一个包含6个字节的消息时 Servo 电机就会转动,但如果发送的信息长于或短于6个字节,我希望它停止转动 . 到目前为止,我已经为主人编写了这段代码:

// 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”时,无论字符长度如何,电机都以全速运行 . 我做错了什么?谢谢 .

1 回答

相关问题