While I appreciate all of your concerns, this is 99% bench project right now. It is only for my potential use. It will only go into actual use with extensive bench testing. I have limited knowledge on Arduino programming but extensive knowledge in design and build applications. I fully understand the difference between safe and unsafe. I have built from scratch, my own single side swing arm, handle bars and most of the rest of the bike. People often praise and criticize for those endeavors also because they lack the knowledge and understanding of technical design.
I would appreciate the continued support on programming help on this project and would again emphasize that at this stage it is a bench project.
For all the comments on here, it is interesting that everyone is quick to understand that I am lacking in programming skill and is more interested in telling me what is wrong with my code in ways that are over my head and never offers code snippets as examples to allow me to move forward.
Here is my latest code.
I am trying to incorporate debounce. I have probably done this in the worst way but with everything I have added, the program still works. I will condense it down later but for now I just would like to know how to add the final part that actually initializes the button push for my application.
Reference http://arduino.cc/en/Tutorial/Debounce
// set the LED using the state of the button:
digitalWrite(ledPin, buttonState);
#include <Servo.h>
Servo throttleServo; // create servo object to control a servo
// set pin numbers:
const int throttleSetPlusThrottleUp = 10; // the number of the pushbutton pin
const int throttleSetPlusThrottleDn = 8;
const int ReturnToPot = 4;
const int ReturnToPot2 = 2;
// constants won't change. They're used here to
// set pin numbers:
int throttlePositionPot = 0;
int val;
// Variables will change:
int debounceThrottleSetPlusThrottleUp; // the current reading from the input pin
int debounceThrottleSetPlusThrottleDn; // the current reading from the input pin
int debounceReturnToPot; // the current reading from the input pin
int debounceReturnToPot2; // the current reading from the input pin
int lastdebounceThrottleSetPlusThrottleUp = LOW; // the previous reading from the input pin
int lastdebounceThrottleSetPlusThrottleDn = LOW; // the previous reading from the input pin
int lastdebounceReturnToPot = LOW; // the previous reading from the input pin
int lastdebounceReturnToPot2 = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long lastDebounceTime2 = 0; // the last time the output pin was toggled
long lastDebounceTime3 = 0; // the last time the output pin was toggled
long lastDebounceTime4 = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
long debounceDelay2 = 50; // the debounce time; increase if the output flickers
long debounceDelay3 = 50; // the debounce time; increase if the output flickers
long debounceDelay4 = 50; // the debounce time; increase if the output flickers
int servoPosition = 0; //Old val1, used to store servo position. Allows use by whole program
void setup()
{
throttleServo.attach(12); // attaches the servo on pin 12 to the servo object
// initialize the pushbutton pin as an input:
pinMode(throttleSetPlusThrottleUp, INPUT);
pinMode(throttleSetPlusThrottleDn, INPUT);
pinMode(ReturnToPot, INPUT);
pinMode(ReturnToPot2, INPUT);
}
void loop()
{
val = analogRead(throttlePositionPot); //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
if (digitalRead(throttleSetPlusThrottleUp) == HIGH)
{
serloop1(); //Enters button control loop
}
if (digitalRead(throttleSetPlusThrottleDn) == 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
// read the state of the switch into a local variable:
int reading = digitalRead(throttleSetPlusThrottleUp);
int reading2 = digitalRead(throttleSetPlusThrottleDn);
int reading3 = digitalRead(ReturnToPot);
int reading4 = digitalRead(ReturnToPot2);
// If the switch changed, due to noise or pressing:
if (reading != lastdebounceThrottleSetPlusThrottleUp)
// reset the debouncing timer
lastdebounceThrottleSetPlusThrottleUp = millis();
// If the switch changed, due to noise or pressing:
if (reading2 != lastdebounceThrottleSetPlusThrottleDn)
// reset the debouncing timer
lastDebounceTime2 = millis();
// If the switch changed, due to noise or pressing:
if (reading3 != lastdebounceReturnToPot)
// reset the debouncing timer
lastDebounceTime3 = millis();
// If the switch changed, due to noise or pressing:
if (reading4 != lastdebounceReturnToPot2)
// reset the debouncing timer
lastDebounceTime4 = millis();
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
debounceThrottleSetPlusThrottleUp = reading;
}
if ((millis() - lastDebounceTime2) > debounceDelay2) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
debounceThrottleSetPlusThrottleDn = reading2;
}
if ((millis() - lastDebounceTime3) > debounceDelay3) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
debounceReturnToPot = reading3;
}
if ((millis() - lastDebounceTime4) > debounceDelay4) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
debounceReturnToPot2 = reading4;
}
int btnControlEnabled = 1; //If button control is enabled this equals 1, else it equals 0
while(btnControlEnabled == 1)
{
if (digitalRead(throttleSetPlusThrottleUp) == 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
}
//If first button not pressed, check the next...
else if (digitalRead(throttleSetPlusThrottleDn) == 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
}
else if (digitalRead(ReturnToPot) == HIGH)
{
btnControlEnabled = 0; //Set loop exit condition
}
else if (digitalRead(ReturnToPot2) == HIGH)
{
btnControlEnabled = 0; //Set loop exit condition
}
//If nothing pressed...
else
{
//...do nothing at all, go back to start of loop
}
}
}