PID controller for encoders

Hey Arduino forum :slight_smile:

I have made a line follower robot for my school project. I have implemented a PID controller that determines which way and haw fast the robot should turn.

My teachers wanted me to use the encoders. I know how to count the encoder steps, but how can i count the steps according to time?

unsigned long counterLeft = 0;  //Encoder counter for left motor
unsigned long counterRight = 0; //Encoder counter for Right motor


pinMode(encoderLB, INPUT);
pinMode(encoderLA, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encoderLA), stepCountLeft, HIGH);
 
pinMode(encoderRB, INPUT);
pinMode(encoderRA, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(encoderRA), stepCountRight, HIGH);
void stepCountLeft(){  
  counterLeft++;
}  
 
void stepCountRight(){
  counterRight++;
}

link to code here: pastebin

Your question belongs with your code. Either ask your question there or post your code here.

Broendholt:
Well - since you can't click the link. The code can be seen above :slight_smile:

welcome to the forum.
I would like to invite you to go to any forum and look at the sticky post at the top. how to use this forum.
scroll down to #7.
come back here, go to your post
to the bottom right are some options, edit and modify
practice that #7 bit.

void readSensors(){      //the function that reads the sensors and sets the error
  if(analogRead(lineSensorL) < 920 && analogRead(lineSensorR) > 920){
    error = -1;
    }
  else if(analogRead(lineSensorR) < 920 && analogRead(lineSensorL) > 920){
    error = 1;
    }
  else if(analogRead(lineSensorL) > 920 && analogRead(lineSensorR) > 920){
    error = 0;
    }
  }

What a mess. That code is way too confusing. Do NOT switch the order of the analogRead().

What is the purpose of the encoders? You never use the data obtained from the encoders.

Broendholt:
I have changed the question now, and used the code brackets :smiley:

That makes people that responded to the original question look like they were smoking something not legal everywhere. Doing that pisses a lot of people off, including me.

Broendholt:
Sorry it is the first time i am posting a question on a forum and now i know i need some practice. Can i somehow delete the post and put up a new fresh one?

You can click the Report to moderator link, in any post, and ask them. It may be possible to restore your original post without removing the replies. Then, you can ask your new question in a new reply.

PaulS:
You can click the Report to moderator link, in any post, and ask them. It may be possible to restore your original post without removing the replies. Then, you can ask your new question in a new reply.

Moderator mode: Unfortunately it is not possible to restore the original post as far as I know.

at this point, there has been no actual response to your question.

I think you should ask the moderator to delete and stat fresh.

BTW, you had a successful trial run !
I can't tell you all the new posts that get sideways and never get fixed.

you can start a new thread

EDIT your first post and change the subject line to [closed]

then notifiy the moderator to delete.

btw , when you post code.

others look and comment.

then you try their recommendations and - on a new reply - , you post the new code.

that leaves the trail of how we got to where we are.

adding the code tags is a good thing. ( in my opinion)
fixing your code in the first post to something that works make all the replies moot.
confuses people and pisses people off.

but, I am wondering if the comment from PaulS
was that he did not like that you went back and added code tags ?