【问题标题】:Weird behaviour after deleting/commenting code删除/评论代码后的奇怪行为
【发布时间】:2016-06-25 13:16:48
【问题描述】:

我遇到了一些奇怪的行为,看起来我找不到答案。我已经编写了一些简单的代码来使用我的手机通过蓝牙控制伺服电机。

#include <Servo.h>
#include "SoftwareSerial.h";

int servoPin = 2;

int bluetoothTx = 11;
int bluetoothRx = 10;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

Servo myservo;
char unChar;
String readString;

void setup() {

    Serial.begin(9600);
    bluetooth.begin(115200);
    delay(100);
    bluetooth.begin(9600);

    myservo.attach(servoPin, 800, 2200);

}

void loop() {

    if (bluetooth.available()) {
        unChar = bluetooth.read();
        Serial.println(unChar);

        if (unChar == 'A') {
            motor();
        }
    }
}

void motor() {
    delay(15);
    while (bluetooth.available()) {
        char c = bluetooth.read();
        readString += c;
    }
    if (readString.length() > 0) {
        Serial.println(readString.toInt());
        myservo.write(readString.toInt());
        readString = "";
    }
}

对于 android,我使用 MIT App Inventor 制作了一个基本的滑块。

我使用的是 Arduino Mega 2560、Power HD 高扭矩高压数字伺服 1218TH 和蓝牙 Mate Silver RN-41。

除了在手机上拖动滑块时伺服卡顿外,一切正常。这是一个小问题。当我在 arduino 循环中评论或删除我的代码时,会出现奇怪的行为。当我玩滑块时,伺服器仍然在它的位置上结结巴巴。是什么导致了这种行为?

【问题讨论】:

    标签: android bluetooth arduino android-bluetooth servo


    【解决方案1】:

    删除评论后尝试重新调整延迟时间,这是因为在循环内添加调试日志需要时间显示。换句话说,循环内的注释对延迟时间有影响。

    【讨论】:

    • 你指的延迟时间是多少?
    • 我的意思是你需要在循环方法中添加延迟时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    相关资源
    最近更新 更多