hello guys,
After hours of research without any helping advice
i hope you can help me out.
I’m building a line following robot with these components:
Arduino duemillanove
2 motors (3V)
4 QTI sensors from parallax (5V)
My code should be right, but the motors are not working properly.
In fact they are reacting to the black line, but they run way too fast
and sometimes take time till the start moving, so i couldnt even build
a proper test-parcour yet…
I believe that the mistake i made is that i didnt use a h-bridge-
is it possible to run them properly without a h- bridge? i already soldered all parts
and maybe i got the wrong motors? should i use servos instead?
in our class we learned to run motors with a transistor and a “diode”…
or maybe i got the code wrong…
hope you can help me out, iam already pretty desperate…
const int motor1Pin = 9; //
const int motor2Pin = 11; //
int sensorPin2 = 2; //sensor 1
int sensorPin3 = 3; //sensor 2
int sensorPin4 = 4; //sensor 3
int sensorPin5 = 5; //sensor 4 // 220 or 1k resistor connected to this pin
int sDigi2=0; //digitale Werte für jeden Sensor
int sDigi3=0;
int sDigi4=0;
int sDigi5=0;
int x1=0;
int x2=100;
int x3=150;
int x4=200;
int x5=250
;
long result = 0;
void setup() //
{
Serial.begin(9600);
Serial.println("start"); //
}
void loop() //
{
Serial.println( "-----------------");
Serial.println( "-- sensor 1 ----");
Serial.println( RCtime(sensorPin2) );
Serial.println( "-- sensor 2 ----");
Serial.println( RCtime(sensorPin3) );
Serial.println( "-- sensor 3 ----");
Serial.println( RCtime(sensorPin4) );
Serial.println( "-- sensor 4 ----");
Serial.println( RCtime(sensorPin5) );
Serial.println( "-----------------");
// delay(5000);
//--------------------- if schleife fuer digitale zuweisungen sDigi2 -----------------------------
if(RCtime(sensorPin2) > 50) //SCHWARZ
{
sDigi2 = 1;
}
if(RCtime(sensorPin2) < 50) //WEISS
{
sDigi2 = 0;
}
//--------------------- if schleife fuer digitale zuweisungen sDigi3 -----------------------------
if(RCtime(sensorPin3) > 50) //SCHWARZ
{
sDigi3 = 1;
}
if(RCtime(sensorPin3) < 50) //WEISS
{
sDigi3 = 0;
}
//--------------------- if schleife fuer digitale zuweisungen sDigi4 -----------------------------
if(RCtime(sensorPin4) > 50) //SCHWARZ
{
sDigi4 = 1;
}
if(RCtime(sensorPin4) < 50) //WEISS
{
sDigi4 = 0;
}
//--------------------- if schleife fuer digitale zuweisungen sDigi5 -----------------------------
if(RCtime(sensorPin5) > 50) //SCHWARZ
{
sDigi5 = 1;
}
if(RCtime(sensorPin5) < 50) //WEISS
{
sDigi5 = 0;
}
//------------------- 0000--------------------------------------
if ((sDigi2 == 0) && (sDigi3 == 0) && (sDigi4 == 0) && (sDigi5 == 0))
{
analogWrite(motor1Pin, x1);
analogWrite(motor2Pin, x1);
}
//------------------- 1000--------------------------------------
if ((sDigi2 == 1) && (sDigi3 == 0) && (sDigi4 == 0) && (sDigi5 == 0))
{
analogWrite(motor1Pin, x5);
analogWrite(motor2Pin, x2);
}
//------------------- 1100--------------------------------------
if ((sDigi2 == 1) && (sDigi3 == 1) && (sDigi4 == 0) && (sDigi5 == 0))
{
analogWrite(motor1Pin, x5);
analogWrite(motor2Pin, x3);
}
//------------------- 0110--------------------------------------
if ((sDigi2 == 0) && (sDigi3 == 1) && (sDigi4 == 1) && (sDigi5 == 0))
{
analogWrite(motor1Pin, x2);
analogWrite(motor2Pin, x2);
}
//------------------- 0011--------------------------------------
if ((sDigi2 == 0) && (sDigi3 == 0) && (sDigi4 == 1) && (sDigi5 == 1))
{
analogWrite(motor1Pin, x3);
analogWrite(motor2Pin, x5);
}
//------------------- 0001--------------------------------------
if ((sDigi2 == 0) && (sDigi3 == 0) && (sDigi4 == 0) && (sDigi5 == 1))
{
analogWrite(motor1Pin, x2);
analogWrite(motor2Pin, x5);
}
else Serial.println( "-- FALSCHE ZUWEISUNG ----");
}
long RCtime(int sensPin)
{
long result = 0;
pinMode(sensPin, OUTPUT); // make pin OUTPUT
digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic
delay(1); // wait a ms to make sure cap is discharged
pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low
digitalWrite(sensPin, LOW); // turn pullups off - or it won't work
while(digitalRead(sensPin))
{ // wait for pin to go low
result++;
}
return result; // report results
}
thank you,
julia