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;
}