Here is the Circut:
The code:
const int buttonPin =
1;
int potpin = A0;
int motorpin = 4;
int analogread = 0;
int potvalue = 0;
int motorvalue = 0;
boolean currentState = LOW;
boolean lastState =
LOW;
boolean stateChange =
false;
int currentButton = 0;
int lastButton = 2;
int ledArray[] = {13}; // setup
void setup() {
pinMode(buttonPin, INPUT);
for (int i=0; i<1; i++){
pinMode(ledArray[i],OUTPUT);
}
Serial.begin(9600);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
} // main loop
void loop(){
potvalue =
analogRead(potpin);
if(potvalue <= 600 && potvalue >= 400){
digitalWrite(8,
HIGH);
digitalWrite(9,
LOW);
digitalWrite(10,
LOW);
}
else{
if(potvalue <=
800 && potvalue >= 601){
digitalWrite(8,
LOW);
digitalWrite(9,
HIGH);
digitalWrite(10,
LOW);
}
else{
if(potvalue <=
1023 && potvalue >= 801){
digitalWrite(8,
LOW);
digitalWrite(9,
LOW);
digitalWrite(10,
HIGH);
}
}
}
currentState =
debounceButton();
stateChange = checkForChange(currentState, lastState);
currentButton = getButtonNumber(lastButton, currentState,
stateChange);
indicatorLight(currentButton);
lastState =
currentState;
lastButton = currentButton;
} // function
debounceButton
boolean debounceButton(){
boolean firstCheck =
LOW;
boolean secondCheck =
LOW;
boolean current = LOW;
firstCheck =
digitalRead(buttonPin);
delay(50);
secondCheck = digitalRead(buttonPin);
if (firstCheck == secondCheck){
current = firstCheck;
}
return current;
} // function checkForChange
boolean checkForChange(boolean current, boolean last){
boolean change;
if (current != last){
change = true;
}
else {
change = false;
}
return change;
} // function
getButtonNumber
int getButtonNumber(int button, boolean state, boolean
change){
if (change == true && state == LOW){
button++;
if (button > 1){
button = 0;
}
Serial.println(button);
}
return button;
}
int motorspeed(int potvalue, int mororvalue){
potvalue = analogRead(potpin);
motorvalue =
map(potvalue, 0, 1023, 0, 255);
Serial.print(motorvalue);
}
void indicatorLight(int button){
for (int i=0; i<1; i++) {
digitalWrite(ledArray[i], LOW);
}
digitalWrite(ledArray[button], HIGH);
if(button == 0){
potvalue = analogRead(potpin);
motorvalue =
map(potvalue, 0, 1023, 0, 255);
analogWrite(motorpin,
motorvalue);
Serial.print("potentiometer = "
);
Serial.print(potvalue);
Serial.print("\t
motor = ");
Serial.println(motorvalue);
delay(2);
}
else{
analogWrite(motorpin, 0);
digitalWrite(8,
LOW);
digitalWrite(9,LOW);
digitalWrite(10,
LOW);
}
}