Hey Guys, I am currently upgrading my Enduro Ebike frame from a hub motor to a Bafang BBSHD Mid Drive with ASI BAC800 external controller from ERT. The drive components are a 42T Luna Eclipse Front ChainRing with a 9 Speeds Shimano Deore RD-M6000GS rear derailleur driving a 11-46T cassette. I am proficient in Mechanical Engineering, have a 3D printer and can design very complex things, but I'm not too good with coding, as Arduino is all new to me... I would need help with Arduino coding for my DIY e-Shifter. The code below is working flawlessly for shifting as you can see in the video, but I need to add to my current code something that will allow me to downshift to gear one (Lowest gear) when I hold the shifter "down button" for 2 seconds or more.
Here is my current Arduino Sketch code:
Here is a video of e-Shifter working and also second video of the Bafang BBSHD spinning at 11000+RPM using ASI BAC800 external controller Field Weakening Technology.
[youtube]pbAWU7V_r1Y[/youtube]
[youtube]93wpKgX8quw[/youtube]
The controller and e-Shifter are inside the enduro ebike frame battery compartment and there are no wires exposed once the battery compartment side covers are installed. Very clean build as you will see in a couple weeks...
I will post videos and Pics when I am done with this build...
Can anyone help me with the Arduino coding to add the function that will allow me to downshift to gear one when I hold the down button for more than 2 seconds so I don't need to hit the button 8 times everytime I come to a stop.
Cheers.
Dan
Here is my current Arduino Sketch code:
Code:
// Electromechanical derailleur project v.1.0
// Assumes you know the servo positions for each gear (determined using v.0.0 software)
#include <Servo.h>
#include <EEPROM.h> //Need this because we'll be saving the gear selection in EEPROM (to avoid shifts on startup)
#define db_time 20 //Button debounce time in mS
#define servo_pin 9 //HW pin to which servo signal line is attached
#define servoDelta 1 //Amt servo moves with each button push (degress)
#define upButtonPin 2 //Up button push grounds it
#define dnButtonPin 3 //Dn button push grounds it
#define led_pin 10 //LED Turn ON when button is pressed
#define highestGear 9 //Number of sprockets on rear hub
#define rearGear1 180 //Servo position values for the various rear sprockets (numbered low to high gears, not by size)
#define rearGear2 162 //These were determined empirically using the diyshift0 sketch while hooked up to a laptop
#define rearGear3 144
#define rearGear4 126
#define rearGear5 108
#define rearGear6 90
#define rearGear7 72
#define rearGear8 54
#define rearGear9 36
#define rearGearAddr 0 //EEPROM address for saving the gear selected
Servo rearServo; // Create servo object to control a servo
int lastUpButtonState = 1;
int lastDnButtonState = 1;
int rearPos = 90; // variable to store the servo position
int rearGear; //Numbered sprocket (1 is lowest gear; 9 is highest)
void setup()
{
rearGear = EEPROM.read(rearGearAddr);
rearServo.attach(servo_pin); //Attach the rear servo to the servo object
if ((rearGear > 0) && (rearGear < 10))
{
shiftToRearGear(rearGear); //And immediately set it to whatever gear was last saved in EEPROM
}
else
{
shiftToRearGear(3); //If not valid, goto gear 3
}
pinMode(upButtonPin, INPUT);
pinMode(dnButtonPin, INPUT);
pinMode(led_pin, OUTPUT);
digitalWrite(upButtonPin, HIGH); //Enable internal pullup resistors
digitalWrite(dnButtonPin, HIGH);
digitalWrite(led_pin, LOW);
Serial.begin(9600); //USED ONLY FOR DEBUG
}
void loop()
{
int upButtonState = digitalRead(upButtonPin); //Poll the up/dn buttons
int dnButtonState = digitalRead(dnButtonPin);
int count; //variables to count the time button is held
if (upButtonState == 0){ //Button was pushed
delay(db_time); //Wait a bit to debounce the button
upButtonState = digitalRead(upButtonPin); //Test again
if (upButtonState == 0 && lastUpButtonState == 1){ //If still down AND was up before
upOneGear(); //Shift up one gear
lastUpButtonState = 0; //Save state
digitalWrite(led_pin, HIGH); // Turn On LED when button is pressed
}
}
else { //upButton must be open (button state = 1)
delay (db_time); //Wait a bit to debounce the button
upButtonState = digitalRead(upButtonPin); //Test again
if (upButtonState == 1){ //Still open, save state
lastUpButtonState = 1;
digitalWrite(led_pin, LOW);
}
}
//
//Now same for dowm
//
if (dnButtonState == 0){ //Button was pushed
delay(db_time); //Wait a bit to debounce the button
dnButtonState = digitalRead(dnButtonPin); //Test again
if (dnButtonState == 0 && lastDnButtonState == 1){ //If still down AND was up before
dnOneGear(); //Shift down one gear
lastDnButtonState = 0; //Save state
digitalWrite(led_pin, HIGH); // Turn On LED when button is pressed
}
}
else { //dnButton must be open (button state = 1)
delay (db_time); //Wait a bit to debounce the button
dnButtonState = digitalRead(dnButtonPin); //Test again
if (dnButtonState == 1){ //Still open, save stated
lastDnButtonState = 1;
digitalWrite(led_pin, LOW);
}
}
}
//
//Functions called
//
void upOneGear(){
if (rearGear < highestGear){
rearGear = rearGear + 1;
}
shiftToRearGear(rearGear);
}
void dnOneGear(){
if (rearGear > 1){
rearGear = rearGear - 1;
}
shiftToRearGear(rearGear);
}
void shiftToRearGear(int gear){
EEPROM.write(rearGearAddr, gear);
switch (gear) {
case 1:
rearServo.write(rearGear1);
break;
case 2:
rearServo.write(rearGear2);
break;
case 3:
rearServo.write(rearGear3);
break;
case 4:
rearServo.write(rearGear4);
break;
case 5:
rearServo.write(rearGear5);
break;
case 6:
rearServo.write(rearGear6);
break;
case 7:
rearServo.write(rearGear7);
break;
case 8:
rearServo.write(rearGear8);
break;
case 9:
rearServo.write(rearGear9);
break;
}
}
Here is a video of e-Shifter working and also second video of the Bafang BBSHD spinning at 11000+RPM using ASI BAC800 external controller Field Weakening Technology.
[youtube]pbAWU7V_r1Y[/youtube]
[youtube]93wpKgX8quw[/youtube]
The controller and e-Shifter are inside the enduro ebike frame battery compartment and there are no wires exposed once the battery compartment side covers are installed. Very clean build as you will see in a couple weeks...
I will post videos and Pics when I am done with this build...
Can anyone help me with the Arduino coding to add the function that will allow me to downshift to gear one when I hold the down button for more than 2 seconds so I don't need to hit the button 8 times everytime I come to a stop.
Cheers.

Dan