2017-12-04 1 views
1

Ich verwende eine App, um entweder eine LED einzuschalten oder den Winkel eines Mikroservos zu ändern, je nachdem, welche Taste gedrückt wird (mit Arduino). Mein Code arbeitet für die LED (während die Taste gedrückt wird, leuchtet die LED auf), aber es passiert nichts, wenn ich die Taste drücken soll, um den Winkel des Servo ändern 40.Ändern des Servowinkels basierend auf dem Bluetooth-Eingang

// Bluetooth serial: 
#include <SoftwareSerial.h> // import the serial library 

// setup the bluetooth coms 
SoftwareSerial BTSerial(8,7); 

#include <Servo.h> 

int servoPin = 0; 

Servo servo; 

int angle = 0; // servo position in degrees 
int input = 0; 
int led2 = 13; 

void setup() { 
    // put your setup code here, to run once: 
    servo.attach(servoPin); 
    Serial.begin(9600); // coms w/ computer 
    BTSerial.begin(9600); // coms w/ Bluetooth 
    pinMode(led2, OUTPUT); 
} 

void loop() { 
    // put your main code here, to run repeatedly: 

    if (BTSerial.available()) 
    { 
    input = BTSerial.read(); 
    digitalWrite(led2, LOW); 

    switch(input) { 
     case 'E': 
     angle = 40; 
     break; 

     case 'C': 
     digitalWrite(led2, HIGH); 
     break; 
    } 

    servo.write(angle); 
    } 
} 

Der Eingang ist richtig, wie ich überprüft, indem auch die LED im Fall 'E' eingeschaltet wurde, wo sie normal funktioniert. Ich hatte auch versucht, servo.write() innerhalb der Case-Funktion zu verwenden, aber das hat auch nicht funktioniert.

case 'E': 
    servo.write(40); 
    break; 
+0

Es gibt keine Fehlermeldung, das Servo bewegt sich überhaupt nicht – user9049016

+0

Ja, ich habe servo.attach, ich habe versucht Servo :: Refresh(); aber es gab den Fehler: 'Aktualisieren' ist kein Mitglied von 'Servo'. – user9049016

+0

An welchen Pin hast du es angehängt? Bearbeiten Sie Ihre Frage, um ein [vollständiges Beispiel] anzuzeigen (https://stackoverflow.com/help/mcve). – aaron

Antwort

0

Sie cannot Einsatz digitaler Stifte 0 oder 1 als Eingang:

As mentioned, those are serial send and receive pins. If you power your computer through USB, it can interfere if you try to use them, since it's reading from both the USB to Serial and the pin. Also you have an issue with anything connected (well, not anything, but remove it just to be safe) when you're trying to program. If you use the pins as intended, and program then run it off battery, it should be no problem at all.

Most of us stay away from it because it's a bit of a hassle

auf Ihrem Arduino Je nach Modell servo.attach unterstützt nur Stifte 9 und 10:

Note that in Arduino 0016 and earlier, the Servo library supports only servos on only two pins: 9 and 10.

Oder Sie könnten nur benutze einen davon trotzdem.

Verwandte Themen