Here's a screenshot of the Arduino code I'm currently using, I keep making minor alterations and improvements. It looks like a lot of code but two large sections of it I haven't written. They are the reed switch timer code originally meant to be used as a cycle computer. The other is servo control using timer2 as timer1 is tied up with timing the reed switch. The library code for servo control uses timer1.
The most important line of code is this one servopos = constrain (((val) + (100 + (Wheel x 20))), 540, 2450);
The constrain function sets the limits on under and overdrive. This should be adjusted to suit the servo in use. In my case its 540 and 2450. Val is the potentiometer and allows adjustment of cadence. Wheel is the output of the reed switch timer, it can be adjusted to feed directly into this function. The multiplication by 20 allows me to add in the derailleur position value to the reed timer code. The offset of +100 determines when the hub starts to shift as to bicycle starts to move from a stop. A value of 540 means it will start immediately which can compensate for a lag in shifting due to acceleration and load on the hub. In practice its too much and results in a low cadence when going slowly up hill.
![[Image: NV%20auto%20shifter%20code%20ss%201_zps8os6mgzr.jpg]](http://i1301.photobucket.com/albums/ag114/Ed_N_R/Mobile%20Uploads/NV%20auto%20shifter%20code%20ss%201_zps8os6mgzr.jpg)
![[Image: NV%20auto%20shifter%20code%20ss%202_zpslmdpgswq.jpg]](http://i1301.photobucket.com/albums/ag114/Ed_N_R/Mobile%20Uploads/NV%20auto%20shifter%20code%20ss%202_zpslmdpgswq.jpg)
![[Image: NV%20auto%20shifter%20code%20ss%203_zpsq5lhhjv6.jpg]](http://i1301.photobucket.com/albums/ag114/Ed_N_R/Mobile%20Uploads/NV%20auto%20shifter%20code%20ss%203_zpsq5lhhjv6.jpg)
![[Image: NV%20auto%20shifter%20code%20ss%204_zpsfoho9okh.jpg]](http://i1301.photobucket.com/albums/ag114/Ed_N_R/Mobile%20Uploads/NV%20auto%20shifter%20code%20ss%204_zpsfoho9okh.jpg)
![[Image: NV%20auto%20shifter%20code%20ss%205_zpsmn0bzz7s.jpg]](http://i1301.photobucket.com/albums/ag114/Ed_N_R/Mobile%20Uploads/NV%20auto%20shifter%20code%20ss%205_zpsmn0bzz7s.jpg)
![[Image: NV%20auto%20shifter%20code%20ss%206_zpsnwnglvrx.jpg]](http://i1301.photobucket.com/albums/ag114/Ed_N_R/Mobile%20Uploads/NV%20auto%20shifter%20code%20ss%206_zpsnwnglvrx.jpg)
![[Image: NV%20auto%20shifter%20code%20ss%207_zpsqg1jxw00.jpg]](http://i1301.photobucket.com/albums/ag114/Ed_N_R/Mobile%20Uploads/NV%20auto%20shifter%20code%20ss%207_zpsqg1jxw00.jpg)
The most important line of code is this one servopos = constrain (((val) + (100 + (Wheel x 20))), 540, 2450);
The constrain function sets the limits on under and overdrive. This should be adjusted to suit the servo in use. In my case its 540 and 2450. Val is the potentiometer and allows adjustment of cadence. Wheel is the output of the reed switch timer, it can be adjusted to feed directly into this function. The multiplication by 20 allows me to add in the derailleur position value to the reed timer code. The offset of +100 determines when the hub starts to shift as to bicycle starts to move from a stop. A value of 540 means it will start immediately which can compensate for a lag in shifting due to acceleration and load on the hub. In practice its too much and results in a low cadence when going slowly up hill.
![[Image: NV%20auto%20shifter%20code%20ss%201_zps8os6mgzr.jpg]](http://i1301.photobucket.com/albums/ag114/Ed_N_R/Mobile%20Uploads/NV%20auto%20shifter%20code%20ss%201_zps8os6mgzr.jpg)
![[Image: NV%20auto%20shifter%20code%20ss%202_zpslmdpgswq.jpg]](http://i1301.photobucket.com/albums/ag114/Ed_N_R/Mobile%20Uploads/NV%20auto%20shifter%20code%20ss%202_zpslmdpgswq.jpg)
![[Image: NV%20auto%20shifter%20code%20ss%203_zpsq5lhhjv6.jpg]](http://i1301.photobucket.com/albums/ag114/Ed_N_R/Mobile%20Uploads/NV%20auto%20shifter%20code%20ss%203_zpsq5lhhjv6.jpg)
![[Image: NV%20auto%20shifter%20code%20ss%204_zpsfoho9okh.jpg]](http://i1301.photobucket.com/albums/ag114/Ed_N_R/Mobile%20Uploads/NV%20auto%20shifter%20code%20ss%204_zpsfoho9okh.jpg)
![[Image: NV%20auto%20shifter%20code%20ss%205_zpsmn0bzz7s.jpg]](http://i1301.photobucket.com/albums/ag114/Ed_N_R/Mobile%20Uploads/NV%20auto%20shifter%20code%20ss%205_zpsmn0bzz7s.jpg)
![[Image: NV%20auto%20shifter%20code%20ss%206_zpsnwnglvrx.jpg]](http://i1301.photobucket.com/albums/ag114/Ed_N_R/Mobile%20Uploads/NV%20auto%20shifter%20code%20ss%206_zpsnwnglvrx.jpg)
![[Image: NV%20auto%20shifter%20code%20ss%207_zpsqg1jxw00.jpg]](http://i1301.photobucket.com/albums/ag114/Ed_N_R/Mobile%20Uploads/NV%20auto%20shifter%20code%20ss%207_zpsqg1jxw00.jpg)
- Oran

