Servo - keine Bewegung

Hallo zusammen, ich bin jetzt soweit mit meinem Robot Car, dass die Funktionen einen Wert ermitteln und dementsprechend handelt. Mein einziges Problem ist der Servo für den Ultraschallsensor. Der Servo funktioniert, aber leider nicht in den der obstacle Funktion. Im setup funktioniert der Servo.

#include <Servo.h>
#include <AFMotor.h>
#include <SoftwareSerial.h>
#define speed 150
#define Echo 3
#define Trig 11
#define Spoint 135
#define MinPoint 80
#define MaxPoint 180
long duration, cm, cml, cmr;
constexpr uint8_t motoPins{ 4 };
constexpr uint8_t motoPin[motoPins]{ 6, 5, 8, 2 };

constexpr bool turnLeft[motoPins]{ LOW, LOW, LOW, HIGH };
constexpr bool turnRight[motoPins]{ HIGH, LOW, LOW, LOW };
constexpr bool forward[motoPins]{ LOW, HIGH, HIGH, LOW };
constexpr bool backward[motoPins]{ HIGH, LOW, LOW, HIGH };
constexpr bool stop[motoPins]{ LOW, LOW, LOW, LOW };
constexpr uint8_t ena[2]{ A3, A4 };

Servo servo;
char BT_input;
char oldInput;
char BLE_val;
int pos;
SoftwareSerial BlueTooth(12, 13);
void setup() {
  Serial.begin(9600);
  BlueTooth.begin(9600);
  servo.attach(10);
  servo.write(90);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);
 
  for (byte b = 0; b < motoPins; b++) {
    pinMode(motoPin[b], OUTPUT);
    digitalWrite(motoPin[b], LOW);
  }

  for (byte b = 0; b < 2; b++) {
    pinMode(ena[b], OUTPUT);
    analogWrite(ena[b], speed);
  }

  delay(2000);
}

void loop() {

  getData();
  setDrive();
}
void drive(const bool* type) {
  for (byte b = 0; b < motoPins; b++) { digitalWrite(motoPin[b], type[b]); }
}
void setDrive() {
  
  if (oldInput != BT_input) {
    if (isAlpha(BT_input)) {
      drive(stop);
      delay(20);  // kurze Pause schont die Antriebe!

      switch (ucase(BT_input)) {
        case 'F': drive(forward); break;

        case 'B': drive(backward); break;

        case 'L': drive(turnLeft); break;

        case 'R':
          drive(turnRight);
          break;

        case 'X': obstacle(); break;
        

        default:
          if (BT_input >= ' ') {
            Serial.print(F("FailChar: "));
            Serial.println(BT_input, HEX);
          }
      }

      debugData();
    }

    oldInput = BT_input;
    return BT_input;
  }
}

void debugData() {
  Serial.print(F("Input Char: "));
  Serial.println(BT_input);
  Serial.print(F("DriveState: "));

  for (byte b = 0; b < motoPins; b++) {
    Serial.print(digitalRead(motoPin[b]));
    Serial.print(' ');
  }

  Serial.println();
}
void getData() {
  if (BlueTooth.available()) {
    BT_input = BlueTooth.read();
    Serial.println(BT_input);
  }
}
char ucase(const char c) {
  if (c >= 'a' && c <= 'z') { return (c + ('A' - 'a')); }

  return c;
}

void obstacle() {
  
  servo.write(92); 
  ultra();
  Serial.println(cm);

  if ( cm >= 20 )
  {
    drive(forward);
    servo.write(92); 
    delay(10);
  }

  else if ( cm < 20 )
  {
    drive(stop);
    delay(200);
    servo.write(136);  
    ultral();
    Serial.println(cml);
    delay(200);
    servo.write(48); 
    ultrar();
        Serial.println(cmr);
    delay(200);
    servo.write(92);
   
    if ( cml >= cmr )
    {
      drive(turnLeft);
      delay(300);
    }
    else if ( cmr > cml )
    {
      drive(turnRight);
      delay(300);
    }
  }

}


void ultra()
{
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  duration = pulseIn(Echo, HIGH);
  cm = microsecondsToCentimeters(duration);

}

void ultral()
{
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  duration = pulseIn(Echo, HIGH);
  cml = microsecondsToCentimeters(duration);
  
}

void ultrar()
{
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  duration = pulseIn(Echo, HIGH);
  cmr = microsecondsToCentimeters(duration);
  
}

int microsecondsToCentimeters(int microseconds)
{
  return microseconds / 60;
}

?????

Ich habe rum probiert. ich nehms raus

Ich nehme mal an Du hast einen Arduino UNO R3.

A3 und A4 sind keine PWM Ausgänge.
Grüße Uwe

Na ja, von der Initialisierung auf 90° im setup() bis zu den 92° in obstacle() ist es nicht weit.
Was passiert, wenn das Fahrzeug tatsächlich weniger als 20cm vor einem Hindernis steht?
Bewegt sich das Servo dann?

Grundsätzlich: Bitte den Code immer bereinigen, damit man das hat, was Du benutzt.
Wenn Du da mit libs rumprobierst, die aber nicht genutzt werden, sorgt das für noch mehr Verwirrung.

Das return BT_input ist da vollkommen falsch und müsste Dir eigentlich als Warnung ausgegeben worden sein.

oldInput = BT_input; brauchst Du an der Stelle - also nicht löschen. War nur als Hinweis, wo Du das findest.

Die enable-Pins sollen an PWM-Pins.
Am UNO sind das 3,5,6,9,10,11
Die PIN 3, 5, 6, 10 & 11 sind schon belegt.
Du musst also etwas umlegen, sonst wird das nichts.

So ich hab mir die Tipps mal zu herzen genommen.

#include <Servo.h>
#include <SoftwareSerial.h>
#define speed 150
#define Echo 4
#define Trig 7
#define Spoint 135
#define MinPoint 80
#define MaxPoint 180
long duration, cm, cml, cmr;
constexpr uint8_t motoPins{ 4 };
constexpr uint8_t motoPin[motoPins]{ 6, 5, 8, 3 };

constexpr bool turnLeft[motoPins]{ LOW, LOW, LOW, HIGH };
constexpr bool turnRight[motoPins]{ HIGH, LOW, LOW, LOW };
constexpr bool forward[motoPins]{ LOW, HIGH, HIGH, LOW };
constexpr bool backward[motoPins]{ HIGH, LOW, LOW, HIGH };
constexpr bool stop[motoPins]{ LOW, LOW, LOW, LOW };
constexpr uint8_t ena[2]{ 9, 11 };
const int servo = 10;
Servo myservo;
char BT_input;
char oldInput;
char BLE_val;
int pos;
SoftwareSerial BlueTooth(12, 13);
void setup() {
  Serial.begin(9600);
  BlueTooth.begin(9600);
  myservo.attach(10);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);

  for (byte b = 0; b < motoPins; b++) {
    pinMode(motoPin[b], OUTPUT);
    digitalWrite(motoPin[b], LOW);
  }

  for (byte b = 0; b < 2; b++) {
    pinMode(ena[b], OUTPUT);
    analogWrite(ena[b], speed);
  }

  delay(2000);
}

void loop() {

  getData();
  setDrive();
}
void drive(const bool* type) {
  for (byte b = 0; b < motoPins; b++) { digitalWrite(motoPin[b], type[b]); }
}
void setDrive() {
  if (oldInput != BT_input) {
    if (isAlpha(BT_input)) {
      drive(stop);
      delay(20);  // kurze Pause schont die Antriebe!

      switch (ucase(BT_input)) {
        case 'F': drive(forward); break;

        case 'B': drive(backward); break;

        case 'L': drive(turnLeft); break;

        case 'R':
          drive(turnRight);
          break;

        case 'X': obstacle(); break;


        default:
          if (BT_input >= ' ') {
            Serial.print(F("FailChar: "));
            Serial.println(BT_input, HEX);
          }
      }

      debugData();
    }

    oldInput = BT_input;
  }
}

void debugData() {
  Serial.print(F("Input Char: "));
  Serial.println(BT_input);
  Serial.print(F("DriveState: "));

  for (byte b = 0; b < motoPins; b++) {
    Serial.print(digitalRead(motoPin[b]));
    Serial.print(' ');
  }

  Serial.println();
}
void getData() {
  if (BlueTooth.available()) {
    BT_input = BlueTooth.read();
    Serial.println(BT_input);
  }
}
char ucase(const char c) {
  if (c >= 'a' && c <= 'z') { return (c + ('A' - 'a')); }

  return c;
}

void obstacle() {

  myservo.write(92);
  ultra();
  Serial.println(cm);

  if (cm >= 20) {
    drive(forward);
    myservo.write(92);
    delay(10);
  }

  else if (cm < 20) {
    drive(stop);
    delay(200);
    myservo.write(136);
    ultral();
    Serial.println(cml);
    delay(200);
    myservo.write(48);
    ultrar();
    Serial.println(cmr);
    delay(200);
    myservo.write(92);

    if (cml >= cmr) {
      drive(turnLeft);
      delay(300);
    } else if (cmr > cml) {
      drive(turnRight);
      delay(300);
    }
  }
}


void ultra() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  duration = pulseIn(Echo, HIGH);
  cm = microsecondsToCentimeters(duration);
}

void ultral() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  duration = pulseIn(Echo, HIGH);
  cml = microsecondsToCentimeters(duration);
}

void ultrar() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  duration = pulseIn(Echo, HIGH);
  cmr = microsecondsToCentimeters(duration);
}

int microsecondsToCentimeters(int microseconds) {
  return microseconds / 60;
}

Der Servo scheint sich tatsächlich nur zu bewegen, wenn die Entfernung unter 20cm fällt. Danke für den Hinweis, klar er checkt ja erst frontal ob die Entfernung kleiner 20 cm ist und macht dann erst den restlichen Ablauf, wenn die zutrifft

Mal ein paar Code Dubletten entfernt:

#include <Servo.h>
#include <SoftwareSerial.h>
#define speed 150
#define Echo 4
#define Trig 7
long duration, cm, cml, cmr;
constexpr uint8_t motoPins{ 4 };
constexpr uint8_t motoPin[motoPins] { 6, 5, 8, 3 };

constexpr bool turnLeft[motoPins] { LOW, LOW, LOW, HIGH };
constexpr bool turnRight[motoPins] { HIGH, LOW, LOW, LOW };
constexpr bool forward[motoPins] { LOW, HIGH, HIGH, LOW };
constexpr bool backward[motoPins] { HIGH, LOW, LOW, HIGH };
constexpr bool stop[motoPins] { LOW, LOW, LOW, LOW };
constexpr uint8_t ena[2] { 9, 11 };
constexpr uint8_t servo{10};
Servo myservo;
char BT_input;
char oldInput;
char BLE_val;

SoftwareSerial BlueTooth(12, 13);

void setup() {
  Serial.begin(9600);
  BlueTooth.begin(9600);
  myservo.attach(servo);
  myservo.write(92);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);

  for (byte b = 0; b < motoPins; b++) {
    pinMode(motoPin[b], OUTPUT);
    digitalWrite(motoPin[b], LOW);
  }

  for (byte b = 0; b < 2; b++) {
    pinMode(ena[b], OUTPUT);
    analogWrite(ena[b], speed);
  }
  delay(2000);
}

void loop() {
  getData();
  setDrive();
}

void drive(const bool* type) {
  for (byte b = 0; b < motoPins; b++) {
    digitalWrite(motoPin[b], type[b]);
  }
}

void setDrive() {
  if (oldInput != BT_input) {
    if (isAlpha(BT_input)) {
      drive(stop);
      delay(20);  // kurze Pause schont die Antriebe!

      switch (ucase(BT_input)) {
        case 'F': drive(forward); break;
        case 'B': drive(backward); break;
        case 'L': drive(turnLeft); break;
        case 'R': drive(turnRight); break;
        case 'X': obstacle(); break;
        default:
          if (BT_input >= ' ') {
            Serial.print(F("FailChar: "));
            Serial.println(BT_input, HEX);
          }
      }
      debugData();
    }
    oldInput = BT_input;
  }
}

void debugData() {
  Serial.print(F("Input Char: "));
  Serial.println(BT_input);

  Serial.print(F("DriveState: "));
  for (byte b = 0; b < motoPins; b++) {
    Serial.print(digitalRead(motoPin[b]));
    Serial.print(' ');
  }
  Serial.println();
}

void getData() {
  if (BlueTooth.available()) {
    BT_input = BlueTooth.read();
    Serial.println(BT_input);
  }
}

char ucase(const char c) {
  if (c >= 'a' && c <= 'z') {
    return (c + ('A' - 'a'));
  }
  return c;
}

void obstacle() {
  cm = ultra();
  Serial.println(cm);

  if (cm >= 20) {
    drive(forward);
  } else {
    drive(stop);
    delay(200);
    myservo.write(136);
    cml = ultra();
    Serial.println(cml);
    delay(200);
    myservo.write(48);
    cmr = ultra();
    Serial.println(cmr);
    delay(200);
    myservo.write(92);

    if (cml >= cmr) {
      drive(turnLeft);
    } else {
      drive(turnRight);      
    }
    delay(300);
  }
}

long ultra() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  duration = pulseIn(Echo, HIGH);
  return duration / 60;
}

Vielen Dank für die Hilfe. Ich habe jetzt mal wieder mein "standard" Problem, dass sich nur ein Rad dreht. Also alles wieder auf Anfang und da drehen sich beide -.-

#include <Servo.h>
#include <SoftwareSerial.h>
#define speed 150
#define Echo 4
#define Trig 7
#define Spoint 135
#define MinPoint 80
#define MaxPoint 180
//long duration, cm, cml, cmr;
constexpr uint8_t motoPins{ 4 };
constexpr uint8_t motoPin[motoPins]{ 6, 5, 8, 3 };

constexpr bool turnLeft[motoPins]{ LOW, LOW, LOW, HIGH };
constexpr bool turnRight[motoPins]{ HIGH, LOW, LOW, LOW };
constexpr bool forward[motoPins]{ LOW, HIGH, HIGH, LOW };
constexpr bool backward[motoPins]{ HIGH, LOW, LOW, HIGH };
constexpr bool stop[motoPins]{ LOW, LOW, LOW, LOW };
constexpr uint8_t ena[2]{ 9, 11 };
const int servo = 10;
Servo myservo;
char BT_input;
char oldInput;
char BLE_val;

SoftwareSerial BlueTooth(12, 13);
void setup() {
  Serial.begin(9600);
  BlueTooth.begin(9600);
  //myservo.attach(10);
  //myservo.write(90);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);

  for (byte b = 0; b < motoPins; b++) {
    pinMode(motoPin[b], OUTPUT);
    digitalWrite(motoPin[b], LOW);
  }

  for (byte b = 0; b < 2; b++) {
    pinMode(ena[b], OUTPUT);
    analogWrite(ena[b], speed);
  } 

  delay(2000);
}

void loop() {

  getData();
  setDrive();
}
void drive(const bool* type) {
  for (byte b = 0; b < motoPins; b++) { digitalWrite(motoPin[b], type[b]); }
}
void setDrive() {
  if (oldInput != BT_input) {
    if (isAlpha(BT_input)) {
      drive(stop);
      delay(20);  // kurze Pause schont die Antriebe!

      switch (ucase(BT_input)) {
        case 'F': drive(forward); break;

        case 'B': drive(backward); break;

        case 'L': drive(turnLeft); break;

        case 'R':
          drive(turnRight);
          break;

        //case 'X': obstacle(); break;


        default:
          if (BT_input >= ' ') {
            Serial.print(F("FailChar: "));
            Serial.println(BT_input, HEX);
          }
      }

      debugData();
    }

    oldInput = BT_input;
  }
}

void debugData() {
  Serial.print(F("Input Char: "));
  Serial.println(BT_input);
  Serial.print(F("DriveState: "));

  for (byte b = 0; b < motoPins; b++) {
    Serial.print(digitalRead(motoPin[b]));
    Serial.print(' ');
  }

  Serial.println();
}
void getData() {
  if (BlueTooth.available()) {
    BT_input = BlueTooth.read();
    Serial.println(BT_input);
  }
}
char ucase(const char c) {
  if (c >= 'a' && c <= 'z') { return (c + ('A' - 'a')); }

  return c;
}
/*
void obstacle() {
  cm = ultra();
  Serial.println(cm);

  if (cm >= 20) {
    drive(forward);
  } else {
    drive(stop);
    delay(200);
    myservo.write(136);
    cml = ultra();
    Serial.println(cml);
    delay(200);
    myservo.write(48);
    cmr = ultra();
    Serial.println(cmr);
    delay(200);
    myservo.write(92);

    if (cml >= cmr) {
      drive(turnLeft);
    } else {
      drive(turnRight);      
    }
    delay(300);
  }
}

long ultra() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  duration = pulseIn(Echo, HIGH);
  return duration / 60;
}*/

Links / rechts bewegt sich nur ein Rad.
forward / Backward beide.
Dreht sich da auch nur eines, dann darf sich bei links ODER rechts keines drehen.

Dann die dazugehörigen PIN ENA und Motor und die zugehörigen Kabel prüfen.

Also ich habe das gemacht, was ich von dir gelernt habe. Ich habe erstmal alles mit // zum ausgrauen gebracht und habe angefangen, nacheinander die Funktionen wieder zu aktivieren. Es fängt schon beim Servo im setup an, sowie ich myservo.attach(10); freigebe, dreht sich nur noch das rechte Rad in alle Richtungen.

Die Kabel hab ich prophylaktisch getauscht.

Es ist 2 Rädrig und es dreht sich lediglich das rechte Rad, vor und rückwärts geht beides.

Warum? Never touch a runing system.

Baue dir doch mal Serielle Ausgaben ein. Damit du nicht die ganze Zeit in dunkeln stocherst.

Ändere das mal in 115200. Das geht dann viel schneller.

Wenn man dir einen Sketch schickt, warum übernimmst du dann nur Teile?

Dann jetzt:
Die MotorPins ENA werden PIN 9 und PIN 10
Der ServoPin wird PIN 11.

#include <Servo.h>
#include <SoftwareSerial.h>
#define speed 150
#define Echo 4
#define Trig 7
#define Spoint 135
#define MinPoint 80
#define MaxPoint 180
//long duration, cm, cml, cmr;
constexpr uint8_t motoPins{ 4 };
constexpr uint8_t motoPin[motoPins] { 6, 5, 8, 3 };

constexpr bool turnLeft[motoPins] { LOW, LOW, LOW, HIGH };
constexpr bool turnRight[motoPins] { HIGH, LOW, LOW, LOW };
constexpr bool forward[motoPins] { LOW, HIGH, HIGH, LOW };
constexpr bool backward[motoPins] { HIGH, LOW, LOW, HIGH };
constexpr bool stop[motoPins] { LOW, LOW, LOW, LOW };
constexpr uint8_t ena[2] { 9, 10 };
const int servo = 11;
Servo myservo;
char BT_input;
char oldInput;
char BLE_val;

SoftwareSerial BlueTooth(12, 13);
void setup()
{
  Serial.begin(9600);
  BlueTooth.begin(9600);
  myservo.attach(servo);
  myservo.write(90);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);

  for (byte b = 0; b < motoPins; b++)
  {
    pinMode(motoPin[b], OUTPUT);
    digitalWrite(motoPin[b], LOW);
  }

  for (byte b = 0; b < 2; b++)
  {
    pinMode(ena[b], OUTPUT);
    analogWrite(ena[b], speed);
  }

  delay(2000);
}

void loop()
{
  getData();
  setDrive();
}
void drive(const bool* type)
{
  for (byte b = 0; b < motoPins; b++)
  {
    digitalWrite(motoPin[b], type[b]);
  }
}
void setDrive()
{
  if (oldInput != BT_input)
  {
    if (isAlpha(BT_input))
    {
      drive(stop);
      delay(20);  // kurze Pause schont die Antriebe!

      switch (ucase(BT_input))
      {
        case 'F': drive(forward); break;

        case 'B': drive(backward); break;

        case 'L': drive(turnLeft); break;

        case 'R':
          drive(turnRight);
          break;

        //case 'X': obstacle(); break;

        default:
          if (BT_input >= ' ')
          {
            Serial.print(F("FailChar: "));
            Serial.println(BT_input, HEX);
          }
      }

      debugData();
    }

    oldInput = BT_input;
  }
}

void debugData()
{
  Serial.print(F("Input Char: "));
  Serial.println(BT_input);
  Serial.print(F("DriveState: "));

  for (byte b = 0; b < motoPins; b++)
  {
    Serial.print(digitalRead(motoPin[b]));
    Serial.print(' ');
  }

  Serial.println();
}
void getData()
{
  if (BlueTooth.available())
  {
    BT_input = BlueTooth.read();
    Serial.println(BT_input);
  }
}
char ucase(const char c)
{
  if (c >= 'a' && c <= 'z')
  {
    return (c + ('A' - 'a'));
  }

  return c;
}
/*
  void obstacle() {
  cm = ultra();
  Serial.println(cm);

  if (cm >= 20) {
    drive(forward);
  } else {
    drive(stop);
    delay(200);
    myservo.write(136);
    cml = ultra();
    Serial.println(cml);
    delay(200);
    myservo.write(48);
    cmr = ultra();
    Serial.println(cmr);
    delay(200);
    myservo.write(92);

    if (cml >= cmr) {
      drive(turnLeft);
    } else {
      drive(turnRight);
    }
    delay(300);
  }
  }

  long ultra() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  duration = pulseIn(Echo, HIGH);
  return duration / 60;
  }*/

Ich wollte von Anfang an ausschliessen, dass wir hier rum experimentieren und dann sind die Kabel defekt.

#include <Servo.h>
#include <SoftwareSerial.h>
#define speed 150
#define Echo 4
#define Trig 7
#define Spoint 135
#define MinPoint 80
#define MaxPoint 180
long duration, cm, cml, cmr;
constexpr uint8_t motoPins{ 4 };
constexpr uint8_t motoPin[motoPins]{ 6, 5, 8, 3 };

constexpr bool turnLeft[motoPins]{ LOW, LOW, LOW, HIGH };
constexpr bool turnRight[motoPins]{ HIGH, LOW, LOW, LOW };
constexpr bool forward[motoPins]{ LOW, HIGH, HIGH, LOW };
constexpr bool backward[motoPins]{ HIGH, LOW, LOW, HIGH };
constexpr bool stop[motoPins]{ LOW, LOW, LOW, LOW };
constexpr uint8_t ena[2]{ 9, 11 };
const int servo = 10;
Servo myservo;
char BT_input;
char oldInput;
char BLE_val;

SoftwareSerial BlueTooth(12, 13);
void setup() {
  Serial.begin(115200);
  BlueTooth.begin(115200);
 myservo.attach(2);
  //myservo.write(90);
  pinMode(Trig, OUTPUT);
  pinMode(Echo, INPUT);

  for (byte b = 0; b < motoPins; b++) {
    pinMode(motoPin[b], OUTPUT);
    digitalWrite(motoPin[b], LOW);
  }

  for (byte b = 0; b < 2; b++) {
    pinMode(ena[b], OUTPUT);
    analogWrite(ena[b], speed);
  } 

  delay(2000);
}

void loop() {

  getData();
  setDrive();
}
void drive(const bool* type) {
  for (byte b = 0; b < motoPins; b++) { digitalWrite(motoPin[b], type[b]); }
}
void setDrive() {
  if (oldInput != BT_input) {
    if (isAlpha(BT_input)) {
      drive(stop);
      delay(20);  // kurze Pause schont die Antriebe!

      switch (ucase(BT_input)) {
        case 'F': drive(forward); break;

        case 'B': drive(backward); break;

        case 'L': drive(turnLeft); break;

        case 'R':
          drive(turnRight);
          break;

        //case 'X': obstacle(); break;


        default:
          if (BT_input >= ' ') {
            Serial.print(F("FailChar: "));
            Serial.println(BT_input, HEX);
          }
      }

      debugData();
    }

    oldInput = BT_input;
  }
}

void debugData() {
  Serial.print(F("Input Char: "));
  Serial.println(BT_input);
  Serial.print(F("DriveState: "));

  for (byte b = 0; b < motoPins; b++) {
    Serial.print(digitalRead(motoPin[b]));
    Serial.print(' ');
  }

  Serial.println();
}
void getData() {
  if (BlueTooth.available()) {
    BT_input = BlueTooth.read();
    Serial.println(BT_input);
  }
}
char ucase(const char c) {
  if (c >= 'a' && c <= 'z') { return (c + ('A' - 'a')); }

  return c;
}
/*
void obstacle() {
  cm = ultra();
  Serial.println(cm);

  if (cm >= 20) {
    drive(forward);
  } else {
    drive(stop);
    delay(200);
    myservo.write(136);
    cml = ultra();
    Serial.println(cml);
    delay(200);
    myservo.write(48);
    cmr = ultra();
    Serial.println(cmr);
    delay(200);
    myservo.write(92);

    if (cml >= cmr) {
      drive(turnLeft);
    } else {
      drive(turnRight);      
    }
    delay(300);
  }
}

long ultra() {
  digitalWrite(Trig, LOW);
  delayMicroseconds(2);
  digitalWrite(Trig, HIGH);
  delayMicroseconds(10);
  digitalWrite(Trig, LOW);
  duration = pulseIn(Echo, HIGH);
  return duration / 60;
}*/
16:10:42.218 -> FailChar: 53
16:10:42.250 -> Input Char: S
16:10:42.250 -> DriveState: 0 0 0 0 
16:10:42.290 -> 
16:10:42.290 -> 
16:10:42.695 -> L
16:10:42.695 -> Input Char: L
16:10:42.784 -> DriveState: 0 0 0 1 
16:10:42.822 -> 
16:10:42.822 -> 
16:10:43.035 -> S
16:10:43.035 -> FailChar: 53
16:10:43.056 -> Input Char: S
16:10:43.056 -> DriveState: 0 0 0 0 
16:10:43.100 -> 
16:10:43.100 -> 
16:10:43.178 -> L
16:10:43.178 -> Input Char: L
16:10:43.210 -> DriveState: 0 0 0 1 
16:10:43.253 -> 
16:10:43.253 -> 
16:10:43.565 -> S
16:10:43.565 -> FailChar: 53
16:10:43.597 -> Input Char: S
16:10:43.597 -> DriveState: 0 0 0 0 
16:10:43.638 -> 
16:10:43.638 -> 
16:10:43.828 -> B
16:10:43.861 -> Input Char: B
16:10:43.928 -> DriveState: 1 0 0 1 
16:10:43.928 -> 
16:10:43.928 -> 
16:10:43.979 -> S
16:10:43.979 -> FailChar: 53
16:10:44.011 -> Input Char: S
16:10:44.011 -> DriveState: 0 0 0 0 
16:10:44.054 -> 
16:10:44.054 -> 

Bis ich den Servo einbinde funktionieren, wie gesagt beide Räder perfekt. Aber sowie ich myservo.attach(2); freigebe, dreht sich nur noch das rechte Rad.

Wie kommst Du denn auf die 2?
Es gibt eine Variable Servo.
Und ich hab Dir den Code doch umgeschrieben...
Geht der?

Die Servo-Lib benutzt den Timer 1 auf dem UNO. Sobald Du den Servo aktivierst ( attach() ), funktionieren die PWM-Pins, die vom Timer 1 gesteuert werden nicht mehr. Das sind die Pins 9 und 10. Die kannst Du in deinem Fall also auch nicht für analogWrite() verwenden. Das ist unabhängig davon, an welchem Pin das Servo angeschlossen ist.

2 Likes

Ich hatte gehofft, dass das Problem wegen pwm besteht und hab den Servo auf einen Non-pwm gesteckt. Ah das hatte ich garnicht wahrgenommen. Ich machs sofort

Das Problem liegt schon am PWM, aber nicht daran, dass das Servo auf einem PWM-Pin steckt. Die Servolib schaltet die PWM an Pin9 und 10 ab, egal, auf welchem Pin das Servo angeschlossen ist. Du musst die Enable-Eingänge auf andere PWM-Pins legen. Pins 9 und 10 stehen dir als PWM nicht zur Verfügung.

2 Likes

Erledigt. Jetzt leuchtet zwar das rote Lämpchen, wenn er ein Signal empfängt, aber beide Räder bewegen sich nicht

Ich schreibs noch und machs doch falsch :man_facepalming: