Hey guys im making eco robot for this years robocon but i need some help with it
check this code of mine and help me to fix it pls pSSS / ASAP :)/
//float Kp=30,Ki=0.0015,Kd=60;
float Kp=30,Ki=0.0015,Kd=80;
float error=0, P=0, I=0, D=0, PID_value=0;
float previous_error=0, previous_I=0;
int sensor[7]={0, 0, 0, 0, 0, 0, 0};
int initial_motor_speed=0;
int lap=0;
int a=0,b=0;
void read_sensor_values(void);
void calculate_pid(void);
void motor_control(void);
double timer=0;
int del=2;
int state=1,pre_state=0;
void setup()
{
pinMode(10,OUTPUT);
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);
Serial.begin(9600);
}
void start(){
if(timer<300){
initial_motor_speed=map(timer,0);
//Kp=30,Ki=0.0015,Kd=80;
Kp=map(timer,0);
//Ki=map(timer,0,300,0.0015,0.0015);
Kd=map(timer,0);
}
else{initial_motor_speed=0; }
}
void loop()
{
start();
read_sensor_values();
calculate_pid();
motor_control();
delay(del);
aldaa();
timer=timer+del;
// if(initial_motor_speed<0
){
// initial_motor_speed=initial_motor_speed+(timer/10);
// }
}
//void left(){
// lap=lap+1;
// digitalWrite(9, LOW);
// digitalWrite(8, HIGH);
// //delay(500);
// analogWrite(6,200);
// analogWrite(11,200);
// delay(300);
// loop();
//}
void aldaa(){
sensor[7]=analogRead(A7);
if(sensor[7]>150){
sensor[7]=1;
}
else{
sensor[7]=0;
}
if(sensor[7]==1){
if(lap<3){
lap=lap+1;
}
else{
stop();
}
}
// if(timer<100){
// initial_motor_speed=0;
// Kp=8,Ki=0.0015,Kd=1;
// }
//
// else if(sensor[7]==1 && sensor[2]==1 || sensor[3]==1 || sensor[4]==1){
// if(initial_motor_speed==0){
// a=a+70
// }
// initial_motor_speed=0;
// Kp=30,Ki=0.0015,Kd=60;
// a=a-70
// //a=a-70
// }
// else {
// if(initial_motor_speed==0){
// a=a-70
// }
// initial_motor_speed=0;
// Kp=8,Ki=0.0015,Kd=1;
// }
}
void stop(){
digitalWrite(8, LOW);
digitalWrite(9, LOW);
delay(1000000);
}
void read_sensor_values()
{
sensor[0]=analogRead(A0);
sensor[1]=analogRead(A1);
sensor[2]=analogRead(A2);
sensor[3]=analogRead(A3);
sensor[4]=analogRead(A4);
sensor[5]=analogRead(A5);
sensor[6]=analogRead(A6);
for(int k=0;k<7;k++){
if(sensor[k]>265){
sensor[k]=1;
}
else{
sensor[k]=0;
}
}
// Serial.print(sensor[k]);
//Serial.println();
if((sensor[0]==1)&&(sensor[1]==1)&&(sensor[2]==1)&&(sensor[3]==1)&&(sensor[4]==1)&&(sensor[5]==1)&&(sensor[6]==1))
// {;PID_value=0; initial_motor_speed=255; delay(1000); initial_motor_speed=120;}
{
}
else if((sensor[0]==0)&&(sensor[1]==1)&&(sensor[2]==1)&&(sensor[3]==1)&&(sensor[4]==1)&&(sensor[5]==1)&&(sensor[6]==1))
// {;PID_value=0; initial_motor_speed=255; delay(1000); initial_motor_speed=120;}
{
error=0;
if(lap<19){
lap=lap+1;
//pre_state=timer;
}
else{
stop();
}
}
else if((sensor[0]==1)&&(sensor[1]==1)&&(sensor[2]==1)&&(sensor[3]==1)&&(sensor[4]==1)&&(sensor[5]==1)&&(sensor[6]==0))
// {;PID_value=0; initial_motor_speed=0; delay(1000); initial_motor_speed=0;}
{
error=0;
if(lap<19){
lap=lap+1;
//pre_state=timer;
}
else{
stop();
}
}
else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==1)&&(sensor[3]==1)&&(sensor[4]==1)&&(sensor[5]==1)&&(sensor[6]==1))
stop();
else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==1)&&(sensor[3]==1)&&(sensor[4]==1)&&(sensor[5]==1)&&(sensor[6]==0))
stop();
else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[3]==1)&&(sensor[4]==1)&&(sensor[5]==1)&&(sensor[6]==0))
stop();
else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[3]==1)&&(sensor[4]==1)&&(sensor[5]==1)&&(sensor[6]==1))
stop();
else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[3]==0)&&(sensor[4]==1)&&(sensor[5]==1)&&(sensor[6]==1))
stop();
else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[3]==0)&&(sensor[4]==0)&&(sensor[5]==0)&&(sensor[6]==1))
error=6;
else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[3]==0)&&(sensor[4]==0)&&(sensor[5]==1)&&(sensor[6]==1))
error=5;
else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[3]==0)&&(sensor[4]==0)&&(sensor[5]==1)&&(sensor[6]==0))
error=4;
else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[3]==0)&&(sensor[4]==1)&&(sensor[5]==1)&&(sensor[6]==0))
error=3;
else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[3]==0)&&(sensor[4]==1)&&(sensor[5]==0)&&(sensor[6]==0))
error=2;
else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[3]==1)&&(sensor[4]==1)&&(sensor[5]==0)&&(sensor[6]==0))
error=1;
else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[3]==1)&&(sensor[4]==0)&&(sensor[5]==0)&&(sensor[6]==0))
error=0;
else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==1)&&(sensor[3]==1)&&(sensor[4]==0)&&(sensor[5]==0)&&(sensor[6]==0))
error=-1;
else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==1)&&(sensor[3]==0)&&(sensor[4]==0)&&(sensor[5]==0)&&(sensor[6]==0))
error=-2;
else if((sensor[0]==0)&&(sensor[1]==1)&&(sensor[2]==1)&&(sensor[3]==0)&&(sensor[4]==0)&&(sensor[5]==0)&&(sensor[6]==0))
error=-3;
else if((sensor[0]==0)&&(sensor[1]==1)&&(sensor[2]==0)&&(sensor[3]==0)&&(sensor[4]==0)&&(sensor[5]==0)&&(sensor[6]==0))
error=-4;
else if((sensor[0]==1)&&(sensor[1]==1)&&(sensor[2]==0)&&(sensor[3]==0)&&(sensor[4]==0)&&(sensor[5]==0)&&(sensor[6]==0))
error=-5;
else if((sensor[0]==1)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[3]==0)&&(sensor[4]==0)&&(sensor[5]==0)&&(sensor[6]==0))
error=-6;
// else if((sensor[0]==1)&&(sensor[1]==1)&&(sensor[2]==1)&&(sensor[3]==1)&&(sensor[4]==0)&&(sensor[5]==0))
// left();
//else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==1)&&(sensor[3]==1)&&(sensor[4]==1)&&(sensor[5]==1))
// error=-5;
// else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[3]==1)&&(sensor[4]==1)&&(sensor[5]==1))
// error=-8;
else if((sensor[0]==0)&&(sensor[1]==0)&&(sensor[2]==0)&&(sensor[3]==0)&&(sensor[4]==0)&&(sensor[5]==0)&&(sensor[6]==0)){
if(previous_error==6)
error=7;
else if(previous_error==-6)
{
error=-7;
}
}
}
void calculate_pid()
{
P = error;
I = error + previous_I;
D = error-previous_error;
PID_value = (KpP) +KiI+ (Kd*D);
previous_I=I;
previous_error=error;
Serial.print("---");
Serial.print(PID_value); Serial.print(" "); Serial.print(initial_motor_speed); Serial.print(" ");
}
void motor_control()
{
// Calculating the effective motor speed:
int left_motor_speed = initial_motor_speed-PID_value;
a=initial_motor_speed-PID_value;
// The motor speed should not exceed the max PWM value
a=constrain(a,0,255);
analogWrite(2,a); //Left Motor Speed
//following lines of code are to make the bot move forward
/*The pin numbers and high, low values might be different
depending on your connections */
Serial.print(a);
Serial.print(" ");
Serial.println(b);
digitalWrite(8,HIGH);
digitalWrite(9,LOW);
}
Let's start with an obvious problem
Kp=map(timer,0);The map() function takes 5 parameters but you have used only 2
Did you really get this far without sorting out such basic errors ?
:o :o :o :o for real ?
i never noticed that ?!
how can i fix this error pls help
What values are you trying to map() ?
Did you write this code ?
i did
but not dat void start part my friend told me to do it like dat ![]()
Start by deciding what value you want to map to another range then read https://www.arduino.cc/en/Reference/Map
There are other things wrong with the code but start by fixing the use of the map() function.