Do not understand SEN0038 program

Hi guys,

I'm still new to arduino. recently i have been finding an encoder for my wheels. And the solution was the SEN0038. Although i understand the basic concept of how it works, but i do not understand the sample program. Can someone kindly explain the code at the bottom to me please.

Next i understand that the arduion no uno board has interupt pins set specifically on pin 2 and 3. Is it possible to use other pin numbers for the interupt function?

[/code]

#define LEFT 0
#define RIGHT 1

long coder[2] = {0,0};
int lastSpeed[2] = {0,0};  

void setup(){
  
  Serial.begin(9600);                            //init the Serial port to print the data
  attachInterrupt(LEFT, LwheelSpeed, CHANGE);    //init the interrupt mode for the digital pin 2
  attachInterrupt(RIGHT, RwheelSpeed, CHANGE);  //init the interrupt mode for the digital pin 3
  
}

void loop(){
  
  static unsigned long timer = 0;                //print manager timer
  
  if(millis() - timer > 100){                  
    Serial.print("Coder value: ");
    Serial.print(coder[LEFT]);
    Serial.print("[Left Wheel] ");
    Serial.print(coder[RIGHT]);
    Serial.println("[Right Wheel]");
    
    lastSpeed[LEFT] = coder[LEFT];  //record the latest speed value
    lastSpeed[RIGHT] = coder[RIGHT];
    coder[LEFT] = 0;                //clear the data buffer
    coder[RIGHT] = 0;
    timer = millis();
  }
  
}

void LwheelSpeed()
{
  coder[LEFT] ++;  //count the left wheel encoder interrupts
}

void RwheelSpeed()
{
  coder[RIGHT] ++; //count the right wheel encoder interrupts
}

What part of it don't you understand? Explaining the whole thing in detail is too long winded.

On UNO you can only have two of them and they must use 2 or 3 only. On MEGA 2560 you also get pins 18-21 for interrupt use. Other boards I don’t know.

Not sure what code you mean by “at the bottom” but if you’re talking about those two functions, all they are doing is incrementing ( using ++ operator) the contents of one member of an array (called coder). The first one increments the first slot in the array because LEFT is defined to be 0. Which is the index of the first element of the array. The other one is incrementing the second element since RIGHT is set to 1 and that is the index of the second element in the coder array variable.

An equivalent way of writing the first one (might be easier to follow) is like so:

coder[0] = coder[0] +1;  <= same as =>  coder[LEFT] = coder[LEFT] +1;  <= same as => coder[LEFT]++; <= same  as => coder[LEFT] += 1;

Ok my questions are here:

1.) attachInterrupt(LEFT, LwheelSpeed, CHANGE); How do you actually set the interrupt pins? I understand that from this function, the CHANGE should be used to set the interrupt. So do you send a 1 or 0 to set it to not interrupt at the start?

2.) Why do you have to do this --> coder[0] = coder[0] +1;. What is the purpose for increasing the encoder value which determines the speed?

3.) for this program, if(millis() - timer > 100), using this line does it limit the testing time?

4.) Right now i have a pair of wheels running on a L293D dc motor driver using pwm. But due to being different motors and such the wheel run different. In order to stop jerking, i have reduced the pwm numbers instead of it being 255. Have this caused the robot motor to run at a different speed? Does it help if i add in an encoder SEN0038 to help fix it?

5.) So if my guess is not wrong, this program using a time limit finds out how many times the encoder speed increases?

  1. LEFT is defined as 0. Interrupt 0 is on pin 2. RIGHT is defined as 1. Interrupt 1 is on pin 3. To set it to not interrupt, don’t call attachInterrupt.

  2. That is called counting. It’s counting the number of pulses from the encoder.

  3. No, that is saying don’t print that stuff every time the loop function repeats. It will only run the code in that block once every 100ms.

  4. I have no idea what you’re talking about.

  5. It counts how many pulses it gets from the encoder in a given length of time. From that you can calculate how many rotations the wheel makes per unit of time and from that you can calculate the speed. The program you’ve shown isn’t doing any of that calculation and it isn’t really fair to call any of those things speed. It’s really just reporting the number of pulses from each encoder for every 100ms.

So basically the encoder for example LEFT which is represented by 0 is attached to pin 2. and the left encoder sends a signal to pin 2 every time it counts. (am i right?)

What i don't get is how do you actually receive the signal? like from the encoder. What is the code to receive the count.

If i were to try. It would be to set pin 2 as an input pin.
pinMode(2, 1); for the setup. // 2 is interupt pin and 1 is to pull up to prevent interrupt

so when i want to interrupt it i will just call

attachInterrupt(2, brake(), change);
From this command. What i understand is 2 is the pin called to interrupt. Brake() is the function called when the interrupt occurs. And change is when a change in value occurs. But what i don't get is how does the change part works?

So in summary. I do not get how the encoder passes the count into the board. And based from the amount of counts i will then call attachInterrupt(2, brake(), change); Afterwhich for the change i should just trigger the interrupt with a LOW right?

I know that my encoder's output goes into the interrupt pins of either 2 or 3 for arduino uno board. What i do not get is that how do i actually program the count.

You don't program th count. When the interrupt is attached it will be reading the pulses from the encoder and every time th value of coder increases by 1. When you want to know the count you just read that variable.

The encoder does not pass any count anywhere.

The encoder just changes the state of that pin (pin 2, INT0) when the motor turns past a certain point in the rotation. It's up to the arduino to count those transitions.
In that code, they attach an interrupt to pin 2, so it calls LwheelSpeed() every time the state of that pin changes.
LwheelSpeed, despite the name, isn't calculating speed. It's doing the counting - it adds one to the count of times it's seen that pin transition. So now we have a count of how many times the motor has rotated since we reset count to 0. Then in loop, every ~100 ms, they calculate speed from the number of rotations and time passed and reset the counter.

It can be done with other pins, but the code is more complicated. There's a PCINT on every pin, but then you need to read the pins in the ISR to determine which pin it was.

Thank you AxeMurderer, Delta_G and DrAzzy. I now understand how the whoile thing works. And if i ever have problems. i will post it back here again. Thank you guys again!