now i have my code but
now i have my code but servo180(); wont be activated strangly
aslo now my servo is moving to left fast and to the right relay slow…
im pretty sure i’ve got every thing wired up correctly and its just the code
<code>
#include <Servo.h>
int motor1Pin = 3; // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
int motor3Pin = 5; // H-bridge leg 3 (pin 10, 3A)
int motor4Pin = 6; // H-bridge leg 4 (pin 15, 4A)
int enablePin1 = 9; // H-bridge enable pin
int enablePin2 = 10; // H-bridge enable pin
int mustTurn = 1;
int distance;
//servo configs
int lastPos = 180;
int pos;
int servoPin = 7;
Servo myservo;
void setup() {
// set all the other pins you’re using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(motor3Pin, OUTPUT);
pinMode(motor4Pin, OUTPUT);
pinMode(enablePin1, OUTPUT);
pinMode(enablePin2, OUTPUT);
// set enablePin high so that motor can turn on:
digitalWrite(enablePin1, HIGH);
digitalWrite(enablePin2, HIGH);
Serial.begin(9600);
myservo.attach(servoPin);
myservo.write(180);
distance = analogRead(5);
if(distance > 410)
{
mustTurn = 1;
}
}
void loop()
{
if(mustTurn = 1)
{
turn();
}else{
forward();
}
////////////////////////////
if(lastPos < 180)
{
servo130();
}
else
{
if(lastPos > 130)
{
servo180();
}
else
{
endedServoPos();
}
}
}
void servo180(){
for(pos = 130; pos < 180; pos++)
{
myservo.write(pos);
lastPos = pos;
distance = analogRead(5);
if(distance > 410)
{
mustTurn = 1;
break;
}
delay(10);
}
delay(100);
}
void servo130(){
for(pos = 180; pos > 130; pos–)
{
myservo.write(pos);
lastPos = pos;
distance = analogRead(5);
if(distance > 410)
{
mustTurn = 1;
break;
}
delay(10);
}
delay(100);
}
void endedServoPos()
{
for(pos = lastPos; pos > 130; pos–)
{
myservo.write(pos);
lastPos = pos;
distance = analogRead(5);
if(distance > 410)
{
mustTurn = 1;
break;
}
delay(10);
}
}
void forward()
{
digitalWrite(motor1Pin, LOW);
digitalWrite(motor2Pin, HIGH);
digitalWrite(motor3Pin, HIGH);
digitalWrite(motor4Pin, LOW);
}
void turn()
{
mustTurn = 0;
digitalWrite(motor1Pin, HIGH);
digitalWrite(motor2Pin, LOW);
digitalWrite(motor3Pin, HIGH);
digitalWrite(motor4Pin, LOW);
}
<code>
PS: how can i put this in some sort of code block so it cant wipe all space at this page out?
thnx nickn4