I have completed an initial program that works well as far as throttle control from the pot and setting speed with two buttons that bump up or down the servo by 2* and two others that release the first two buttons back to the throttle pot, clutch and front break levers.
I have now also written code in to read the speed hall sensor and bump the servo by 2 degrees based on the loop comparison. I got the code from youTube for an LCD RPM read out of a hall sensor reading rpm of a drill motor. I removed all the LCD refference and incorporated. I have not made any attempt to tie it into the bikes hall sensor as of yet.
I would like to know if the code looks like it makes any sense as I really do not fully understand the hall read/calculation part of the program.
#include <Bounce.h>
#include <Servo.h>
Servo throttleServo; // create servo object to control a servo
int throttleServoPotpin = 0;
int servoPosition = 0; //Old val1, used to store servo position. Allows use by whole program
int val;
// set pin numbers:
const int throttlePosSet_Up = 10; // the number of the pushbutton pin
const int throttlePosSet_Dn = 8;
const int returnThrottleToPot1 = 4;
const int returnThrottleToPot2 = 2;
// read the hall effect sensor on pin 6
const int rpmHallSensor=6;
long debounceDelay = 50; // the debounce time; increase if the output flickers
//Debounce objects
// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce bouncer1 = Bounce(throttlePosSet_Up, debounceDelay);
Bounce bouncer2 = Bounce(throttlePosSet_Dn, debounceDelay);
Bounce bouncer3 = Bounce(returnThrottleToPot1, debounceDelay);
Bounce bouncer4 = Bounce(returnThrottleToPot2, debounceDelay);
const unsigned long sampleTime=100;
const int maxRPM = 10200;
int currentRPM = 0;
int kount2rpm = 0;
void setup()
{
throttleServo.attach(12); // attaches the servo on pin 12 to the servo object
// initialize the pushbutton pin as an input:
pinMode(throttlePosSet_Up, INPUT);
pinMode(throttlePosSet_Dn, INPUT);
pinMode(returnThrottleToPot1, INPUT);
pinMode(returnThrottleToPot2, INPUT);
// initialize input for pick up of motorcycle speed hall sensor
pinMode(rpmHallSensor,INPUT);
}
void loop()
{
val = analogRead(throttleServoPotpin); //takes reading from pot current position
val = map(val, 0, 1083, 0, 180); //maps pot inputs to servo output
throttleServo.write(val); //writes current position to servo to move it
//Update debounce tool
bouncer1.update();
bouncer2.update();
//Do not need to update these here are they are not used
//bouncer3.update();
//bouncer4.update();
if (bouncer1.read() == HIGH)
{
serloop1(); //Enters button control loop
}
if (bouncer2.read() == HIGH)
{
serloop1(); //Enters button control loop
}
}
/**
* If button control is enabled, loop and handle control buttons
* If exit buttons (To return to pot control) are pressed, exit loop and return
* to pot control
*/
void serloop1()
{
servoPosition = throttleServo.read(); //reads current servo location
int btnControlEnabled = 1; //If button control is enabled this equals 1, else it equals 0
while(btnControlEnabled == 1)
{
//Update all debounce tools
bouncer1.update();
bouncer2.update();
bouncer3.update();
bouncer4.update();
if (bouncer1.read() == HIGH)
{
throttleServo.write(servoPosition + 2); //SUBTRACT 2 degrees to servo position for increased motor rpm
servoPosition = throttleServo.read(); //Read new servo position
delay(100); //allows time for switch ro reset
digitalRead(rpmHallSensor);
currentRPM = digitalRead(rpmHallSensor);
if (kount2rpm != currentRPM);
serloop2();
}
//If first button not pressed, check the next...
else if (bouncer2.read() == HIGH)
{
throttleServo.write(servoPosition - 2); //ADDS 2 degrees to servo position for decreased motor rpm
servoPosition = throttleServo.read(); //Read new servo position
delay(100); //allows time for switch ro reset
digitalRead(rpmHallSensor);
currentRPM = digitalRead(rpmHallSensor);
if (kount2rpm != currentRPM);
serloop3();
}
else if (bouncer3.read() == HIGH)
{
btnControlEnabled = 0; //Set loop exit condition
}
else if (bouncer4.read() == HIGH)
{
btnControlEnabled = 0; //Set loop exit condition
}
//If nothing pressed...
else
{
//...do nothing at all, go back to start of loop
}
}
int rpm=getRPM();
}
int getRPM()
{
// sample for sampleTime in millisecs
int kount=0;
boolean kflag=LOW;
unsigned long currentTime=0;
unsigned long startTime=millis();
while (currentTime<=sampleTime)
{
if (digitalRead(rpmHallSensor)==HIGH)
{
kflag=HIGH;
}
if (digitalRead(rpmHallSensor)==LOW && kflag==HIGH)
{
kount++;
kflag=LOW;
}
currentTime=millis()-startTime;
}
int kount2rpm = int(60000./float(sampleTime))*kount;
return kount2rpm;
serloop1();
}
void serloop2()
{
if (kount2rpm < currentRPM);
serloop4();
}
void serloop3()
{
if (kount2rpm > currentRPM);
serloop5();
}
void serloop4()
{
servoPosition = throttleServo.read(); //reads current servo location
throttleServo.write(servoPosition + 2); //SUBTRACT 2 degrees to servo position for increased motor rpm
}
void serloop5()
{
servoPosition = throttleServo.read(); //reads current servo location
throttleServo.write(servoPosition - 2); //SUBTRACT 2 degrees to servo position for increased motor rpm
}