- Author: Rapha Pretorius
- Email: [email protected]
- Optical Encoder github page here
- Still working on this. It is my very first extension :)
Optical Encoder aims to make your Smart Robot Car more accurate and versatile by allowing you to track distance travelled and disk slots counted. This will let you make 90 degree turns and move set distances. For example to navigate a maze or a grid with set grid sizes. I was surprised to see there was not yet an encoder extension available so I made one myself. This is my first extension.
This repository can be added as an extension in MakeCode.
- open https://makecode.microbit.org/
- click on New Project
- click on Extensions under the gearwheel menu
- copy paste this link https://github.com/mr-meow-za/optical-encoder-wheel-distance into search bar and import
Add the Initial Setup block to your Start Block and enter the values for your specific parts. It will then automatically create counters based on your input and distance variables.
Initial Setup Values
- Wheel Diameter: Outer diameter of the car wheel in mm. Mine is 66.1
- Number of disk slots: Count how many gaps there are in the small disk that sits in the encoder slot. Mine is 20
- Encoder 1 to pin: The pin number your encoder 1 signal output is connected to. Mine is 0
- Encoder 2 to pin: The pin number your encoder 2 signal output is connected to. Mine is 9
Encoder.setValues(66.1, 20, 0, 9)
Reset Counters
- Simply resets all counters to zero.
- Each time you want to use distance or slot count to do something like move forward 100mm you need to reset the counters, else it will already have a distance value and won't work.
- Example: Tell car to move 100mm forward, reset counters, turn left by moving 5 slot counts, reset counters, move forward 100mm, reset counters.
Encoder.resetAllEncoderCounters()
Variables - Counters
- Gives the number or slots counted by the IR beam in encoder.
- Useful for many functions like creating a turn 90 degrees function by telling wheels to each move 5 slots in opposite directions. 5 Slots for mine as its 18 degrees per slot.
- Can be used to calculate speed too.
Encoder.encoder1Count()
Variables - Distance
- Super useful! No more guessing how many seconds to run motor to move X mm forward.
- Put your move forward function inside an if statement and compare desired distance to the distance variable. Car will then move forward by that many mm and stop.
- Great for navigating mazes or grids with set block sizes.
Encoder.mmMovedOne()
- for PXT/microbit