iiiii
now works, when you put something black on the sensors they give the value 4000, do not know how such a mistake, it was the short circuit: /
as a code for to use with sensors, I visualized this link here:
pololu.com/docs/0J19/3
just do not understand how the error can always varies between -1000 and 1000, as the motor speed will vary so?,
can you explain me? and if possible exemplify me a code to use with this array of sensors? (to make the follower of lines).
What I want is for the engines to turn the two maxima velocidadequando sensors are about half the black line (00011000, thus exemplifying the best), and that when this state is established: 00110000 engine right little slow, but when check this: 11000000, the right engine very slow, you know? …
I worked out the following schedule:
/*Bibliotecas necessárias*/
#include <Servo.h>
/*variáveis de configuração*/
int nsensores = 8;
int rmotor = 9;//pino do motor direito (PWM);
int lmotor = 10;//pino do motor esquerdo (PWM);
int sensor[] = {8,7,6,5,4,3,2,1};
/* variaveis auxiliares*/
int senval[]= {0,0,0,0,0,0,0,0};//variavel onde será guardado os valores dos sensores.
int Rtarguetval = 0;// valor pretendido para o motor direito.
int Ltarguetval = 0;// valor pretendido para o motor esquerdo.
int nowvalR = 0; //valor actual do motor direito.
int nowvalL = 0;//valor actual do motor esquerdo.
int vel_max = 130;//velocidade máxima dos motores.
int CStop = 0;//
boolean stop = false;
double LineErrorVal = 0.0;
void setup(){
Serial.begin(9600);
pinMode (rmotor, OUTPUT);
pinMode (lmotor, OUTPUT);
DDRD = LOW;// portas 7..0 como inputs, aqui ligarei os 8 sensores.
}
void loop(){
stop = false;
//Lemos os valores dos sensores
readALLSensores();
if(senval[11111111]){
stop = true;
}
//vemos se estes valores se mantêm, duante 30ms
if(stop){
CStop++;
if (CStop >=30){
Rtarguetval = 0;
Ltarguetval = 0;
}
else{
stop = false;
}
}
//De seguida iremos determinar as velocidades para os motores, consoante o erro.
if(LineErrorVal < 0.0){
Ltarguetval = (int)(vel_max*(1-fabs(LineErrorVal)));
Rtarguetval = vel_max;
}else if (LineErrorVal > 0.0){
Ltarguetval = vel_max;
Rtarguetval = (int)(vel_max*(1-LineErrorVal));
}else{
Ltarguetval = (int)(vel_max);
Rtarguetval = (int)(vel_max);
}
//controlar os arranques e travagens dos motors.
if(Rtarguetval > nowvalR){
nowvalL +=1;
}else if (Rtarguetval < nowvalR){
nowvalL -=1;
}
if (Ltarguetval > nowvalL){
nowvalL +=1;
}else if(Ltarguetval < nowvalL){
nowvalL -=1;
}
// ter a certeza que a velocidade dos motores nao ultrapassam o valor desejado
if (nowvalL > vel_max){
nowvalL = vel_max;
}
if (nowvalR > vel_max){
nowvalR = vel_max;
delay(20);
}
}
//FUNÇÔES//
void readALLSensores(){
for(int i = 0; i < 8; i++)
{
senval[i] = digitalRead (sensor[i]);
}
}
int bestSensor(int sensor[]){
int bestSensor = 0;
for (int i=0; i < 8; i++)
{
if (sensor[i] == 1){
digitalRead (sensor[i]+1);
if (sensor[i] +1 == 0){
bestSensor = i;
}
}
else if(sensor[i] == 0){
digitalRead (sensor[i]+1);
if(sensor[i] +1 ==1){
bestSensor = i;
}
}
}
}
//retorna valores entre -1.0 e 1.0, consoante o erro da linha
// se a linha estiver ao centro, o erro é 0.0.
double lineError (int sensor[])
{
int best = bestSensor (sensor);
double error = 0.0;
// começando!!
if (best == 3 || best ==4){
error = 0.0;
}
else{ if(best == 2){
error = -0.3;
}
else{if (best = 1){
error = -0.6;
}
else{if (best = 0){
error = -1.0;
}
else{if (best = 5){
error =0.3;
}
else{if (best = 6){
error = 0.6;
}
else{if (best = 7){
error = 1.0;
}
{
return error;
}
}
}
}
}
}
}
}
but is for use with an array of 5 sensors “normal” with these sensors do not know and do heide, when you look at my schedule, my error ranges (-1 to 1) and this error as the engine speed will vary (try to look and realize I do not know how to explain), to use these sensors, percegi the error or is it 1000 or -1000, never varies in these numbers, hence my question …
could you me illustrate a simple code to “adapt” this array of sensors to the follower of lines?
thanks.