hallo zusammen,
seit einigen tagen arbeite ich an meinem roboter projekt
momentan ist es eine laffette mit einem srf02 us sensor einem servo mit sharp gp2d12 ir-sensor
das programm soll vollgendermassen arbeiten
-Fahren
-alle 30sec den us-sensor abfragen
-wenn der wert kleiner als 30 ist
-stoppen
-servo nach links
-ir messung
-servo nach rechts
-ir messung
-messerwerte vergleichen und dementsprechen drehen
mein problem ist das das programm total dureinander läuft so scheint es zumindestens
das servo arbeitet sich immer wieder von links nach rechts obwohl der us kein wert kleiner 30 ausgi bt
bitte um hilfe
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - INCLUDE
#include "Wire.h"
#include "SRF02.h"
#include <Servo.h>
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - VARIABLESSRF02 sensor(0x70, SRF02_CENTIMETERS);
unsigned long nextPrint = 0;Servo myservo;
int irpin = 0;
// Motor Pinsint motor_RechtsPWM = 5; //PWM Motor Rechts
int motor_Rechts1 = 3;
int motor_Rechts2 = 6;int motor_LinksPWM = 11; //PWM Motor Links
int motor_Links1 = 4;
int motor_Links2 = 7;//Servo und IR
// Position des Servos
int rechts = 0;
int links = 120;//Variable für Messert
int IrRechts;
int IrLinks;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - SETUP
void setup()
{
Wire.begin();
Serial.begin(9600);
myservo.attach(9); //servo an pin9pinMode(motor_RechtsPWM, OUTPUT);
pinMode(motor_Rechts1, OUTPUT);
pinMode(motor_Rechts2, OUTPUT);pinMode(motor_LinksPWM, OUTPUT);
pinMode(motor_Links1, OUTPUT);
pinMode(motor_Links2, OUTPUT);
}// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - LOOP
void loop() {
{
//Fahren
analogWrite(motor_RechtsPWM, 200);
digitalWrite(motor_Rechts1, HIGH);
digitalWrite(motor_Rechts2, LOW);analogWrite(motor_LinksPWM, 200);
digitalWrite(motor_Links1, HIGH);
digitalWrite(motor_Links2, LOW);}
{
SRF02::update();
if (millis() > nextPrint)
{
Serial.println(sensor.read());
nextPrint = millis () + 500;
}
}
if (sensor.read() < 30)
{
{
//stop
analogWrite(motor_RechtsPWM, 0);
digitalWrite(motor_Rechts1, HIGH);
digitalWrite(motor_Rechts2, LOW);analogWrite(motor_LinksPWM, 0);
digitalWrite(motor_Links1, HIGH);
digitalWrite(motor_Links2, LOW);}
{
myservo.write(rechts); // tell servo to go to position in variable 'pos'
delay(2000); // waits 15ms for the servo to reach the position
}
{
IrRechts=analogRead(irpin);
delay(100);
}
{
myservo.write(links); // tell servo to go to position in variable 'pos'
delay(2000); // waits 15ms for the servo to reach the position
}
{
IrLinks=analogRead(irpin);
delay(100);
}if ((IrRechts) <(IrLinks))
{//links drehen
analogWrite(motor_RechtsPWM, 100);
digitalWrite(motor_Rechts1, HIGH);
digitalWrite(motor_Rechts2, LOW);analogWrite(motor_LinksPWM, 0);
digitalWrite(motor_Links1, HIGH);
digitalWrite(motor_Links2, LOW);
delay(100);
return;}
else if ((IrRechts)> (IrLinks))
{//rechts drehen
analogWrite(motor_RechtsPWM, 0);
digitalWrite(motor_Rechts1, HIGH);
digitalWrite(motor_Rechts2, LOW);
Serial.println("rechts");
analogWrite(motor_LinksPWM, 100);
digitalWrite(motor_Links1, HIGH);
digitalWrite(motor_Links2, LOW);
delay(100);
return;}
}
}