Button coding

Im trying to have one button do each of these actions when pressed 1 time to 2 time to 3 time.

Im just not sure how to code it. If anyone can look at my code and help me it would be greatly helpful.

sketch_may05a.ino (4.18 KB)

boggieswag:
Im trying to have one button do each of these actions when pressed 1 time to 2 time to 3 time.

Im just not sure how to code it. If anyone can look at my code and help me it would be greatly helpful.

Just incase the download link didn't work

//Define Pin Mappings
const int leftMotorPin = 9;
const int leftSensorPin = A0;
const int rightMotorPin = 8;
const int rightSensorPin = A1;
int button = 5;

// car Variables

int state = 0;
int old = 0;
int buttonPoll = 0;
int buttonCounter = 0;

int rightlightThreshold;
int leftlightThreshold;
int lightThreshold;
int rightdarkThreshold;
int leftdarkThreshold;
int darkThreshold;
int Lightl;
int Lightr;
int Light;
int Darkl;
int Darkr;
int Dark;
int threshold;

void setup() {

//Set Baud Rate, Required to use Serial Monitor
Serial.begin(9600);
pinMode(leftMotorPin, OUTPUT);
pinMode(leftSensorPin, INPUT);
pinMode(rightMotorPin, OUTPUT);
pinMode(rightSensorPin, INPUT);
pinMode(button, INPUT);

digitalWrite(leftSensorPin, LOW);
digitalWrite(rightSensorPin, LOW);

//Need to use button to set light and dark thresholds
//Do this in set up so that thresholds can be set and used later on in loop function
//Want: first press uses the left/right sensors to set light thresholds, second press to set dark thresholds, third press to start
//Possible solution: Use variable "buttonCounter" to keep track of number of presses, if loops to set thresholds based on buttonCounter
//Then in the loop function put the movement using dark/light thresholds, similar to your case 3 of the switch statement
//Not completely sure that this will work, might require adjustments to movement code and its interaction with the thresholds
//The while loop code I wrote is very similar to the switch statement you wrote, but in the setup function to intialize the thresholds that can be used in the movement loop function
//Other option: You can keep the switch statement you already wrote, as that should work fine. I made comments on the areas you might need to change to ensure that it works but I can't be certain that
//those changes will work. If you decide to stick with switch statement (which might be the better option) just delete the below code and appropriate variables

}

void loop(){
//Serial.println("The button counter value is:");
//Serial.print(buttonCounter);
buttonPoll = digitalRead(button);

while(buttonCounter != 4){ //Will run until the buttonCounter variable reaches 3, then breaks out and goes to the loop function
//buttonPoll = digitalRead(button);

//Serial.println("The button counter value is:");
//Serial.print(buttonCounter);

if(buttonPoll == 0) //If button is pressed, add one to the counter
buttonCounter = buttonCounter + 1;

if (buttonCounter == 1){ //If the counter is equal to 1, take sensor readings and assign light threshold
Serial.print("Taking Light readings"); //Added serial displays so that progress can be tracked in the serial monitor, if you run into trouble you can see where the code is stopping/not working
leftlightThreshold = analogRead(leftSensorPin);
rightlightThreshold = analogRead(rightSensorPin);
lightThreshold = (leftlightThreshold + rightlightThreshold)/2;
Serial.print("\n The light threshold value is: ");
Serial.print(lightThreshold);
Serial.print("\n The button counter value is: ");
Serial.print(buttonCounter);
}

if(buttonCounter == 2){ //If the counter is equal to 2, take sensor readings and assign dark threshold
Serial.print("\n \n Taking Dark readings");
leftdarkThreshold = analogRead(leftSensorPin);
rightdarkThreshold = analogRead(rightSensorPin);
darkThreshold = (leftdarkThreshold + rightdarkThreshold)/2;
Serial.print("\n The dark threshold value is: ");
Serial.print(darkThreshold);
Serial.print("\n The button counter value is: ");
Serial.print(buttonCounter);
}

if(buttonCounter == 3){ //If the counter is equal to 3, take sensor readings and assign threshold
Serial.print("\n \n Taking Threshold readings");
threshold = (lightThreshold + darkThreshold)/2;
Serial.print("\n The threshold value is: ");
Serial.print(threshold);
Serial.print("\n The button counter value is: ");
Serial.print(buttonCounter);
}

}
}

Im just not sure how to code it.

I already told you in #7 of your other thread, where I suggested you need state change detection to do that.

Your code attached here does not do any comparison of the current state of the button to its previous state, so it's going to increment your counter as long as the button is pressed. Even a short press/release in human time is going to count up a few times I'm sure. That's why the state change detect example as suggested in your other thread, exists. Did you even look at it?

you used the incorrect method to count the button press.

If you want to be simple, use the button library. See the button count example using library

jubukraa:
I already told you in #7 of your other thread, where I suggested you need state change detection to do that.

Your code attached here does not do any comparison of the current state of the button to its previous state, so it's going to increment your counter as long as the button is pressed. Even a short press/release in human time is going to count up a few times I'm sure. That's why the state change detect example as suggested in your other thread, exists. Did you even look at it?

I did look at and I made another code sheet I'm just very confused by it.

boggieswag:
I'm just very confused by it.

What is it that confuses you?