i appreciate your advice, but here's the new code, but i'm unable to verify it (because I don't have my hardware here)
but i would like if someone could skim trough the code and see if i made some stupidity (because i haven't programmed in months and because i'm not quite sure if i made right float to int conversions)...
Thanks, xvjeko
//Written for atmega328-PU (16MHz external crystal)
//Compiled with arduino 1.0 (www.arduino.cc)
#include <Servo.h>
#define BAUD 9600 //UART baud rate
#define ECHOPINL 9 //Echo pin l = left
#define ECHOPINR 11 //Echo pin r = right
#define MINPULSE 600 //Minimum servo pulse
#define MAXPULSE 2400 //Maximum servo pulse
#define TRIGPINL 10 //Trig pin l
#define TRIGPINR 12 //Trig pin r
Servo servol; //Creating servo object servol
Servo servor; //Creating servo object servor
int holdtime = 15;
bool hold = false;
int vall, valr;
float perl, perr;
float prevperl = 0, prevperr = 0;
int left(int d0, int d1)
{
int cm;
long duration;
float percentage;
digitalWrite(TRIGPINL, LOW);
delay(2);
digitalWrite(TRIGPINL, HIGH);
delay(5);
digitalWrite(TRIGPINL, LOW);
duration = pulseIn(ECHOPINL, HIGH);
cm = microsecondsToCentimeters(duration);
percentage = map(cm, d0, d1, 0, 100);
return( (float)percentage / 100);
}
int right(int d0, int d1)
{
int cm;
long duration;
float percentage;
digitalWrite(TRIGPINR, LOW);
delay(2);
digitalWrite(TRIGPINR, HIGH);
delay(5);
digitalWrite(TRIGPINR, LOW);
duration = pulseIn(ECHOPINR, HIGH);
cm = microsecondsToCentimeters(duration);
percentage = map(cm, 20, 6, 90, 180);
return( (float)percentage / 100);
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
void debugg()
{
Serial.print(vall);
Serial.print(" ");
Serial.print(valr);
Serial.println();
}
void setup()
{
Serial.begin(BAUD);
pinMode(ECHOPINL, INPUT);
pinMode(ECHOPINR, INPUT);
pinMode(TRIGPINL, OUTPUT);
pinMode(TRIGPINR, OUTPUT);
}
void loop()
{
perl = left(20, 6);
perr = right(6, 20);
if( (perl - prevperl) >= (5/100) )
{
perl = prevperl;
hold=false;
}
if( (perr - prevperr) >= (5/100) )
{
perr = prevperr;
hold=false;
}
vall = perl * 100;
vall = (int)vall;
valr = perr * 100;
valr = (int)valr;
vall = map(vall, 0, 100, 90, 180);
valr = map(valr, 0, 180, 180, 90);
servol.write(vall * perl);
servor.write(valr * perl);
if(hold==true) delay(holdtime);
hold=true;
debugg();
}