I'm fairly new to programming, I'm trying write a code that uses a button to select a program to run. the issue I'm having is that when the delay value is high the program some times does not register the button change. it can be really hit or miss. I would be thankful for any help. here is the code, sorry it's really messy.
// constants won't change. They're used here to set pin numbers:
int buttonPin1 = 7; // the number of the pushbutton pin
int buttonPin2 = 6;
int counter = 0; //counter 1
int analogpin1 = 0; // select the input pin for the potentiometer
int pin13 = 13; // select the pin for the LED
int pin9 = 9;
int pin10 = 10;
int pin11 = 11;
int sensorValue; // variable to store the value coming from the sensor
int mappedval;
// variables will change:
int buttonState1; // variable for reading the pushbutton status
int buttonState2;
int lastButtonState1;
int val9 = 0; // variable that holds the current LED brightness
int delayval = 0; // variable that holds the current delay time
int val10 = 0; // variable that holds the current LED brightness
int delayval2 = 0; // variable that holds the current delay time
int val11 = 0; // variable that holds the current LED brightness
int delayval3 = 0; // variable that holds the current delay time
void setup() {
// initialize the LED pin as an output:
pinMode(pin13, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(pin11, OUTPUT);
pinMode(pin10, OUTPUT);
pinMode(pin9, OUTPUT);
Serial.begin(115200);
}
void loop(){
// read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2);
buttonState1 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState1 != lastButtonState1) {
// if the state has changed, increment the counter
if (buttonState1 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
counter++;
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState1 = buttonState1;
// Reset count if over max mode number
if(counter > 8)
{
counter = 0;
}
if (counter == 1) {
// read the value from the sensor:
sensorValue = analogRead(analogpin1);
mappedval = map(sensorValue, 0, 1023, 5, 300);
// turn the ledPin on
digitalWrite(pin13, HIGH);
digitalWrite(pin11, HIGH);
digitalWrite(pin10, LOW);
digitalWrite(pin9, LOW);
// stop the program for <sensorValue> milliseconds:
sensorValue = analogRead(analogpin1);
mappedval = map(sensorValue, 0, 1023, 5, 300);
delay(mappedval);
// turn the ledPin off:
digitalWrite(pin13, HIGH);
digitalWrite(pin11, LOW);
digitalWrite(pin10, HIGH);
digitalWrite(pin9, LOW);
sensorValue = analogRead(analogpin1);
mappedval = map(sensorValue, 0, 1023, 5, 300);
delay(mappedval);
digitalWrite(pin13, HIGH);
digitalWrite(pin11, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin9, HIGH);
sensorValue = analogRead(analogpin1);
mappedval = map(sensorValue, 0, 1023, 5, 300);
delay(mappedval);
digitalWrite(pin9, LOW);
// Serial.print("\t");
// Serial.println(mappedval);
}
buttonState1 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState1 != lastButtonState1) {
// if the state has changed, increment the counter
if (buttonState1 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
counter++;
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState1 = buttonState1;
// Reset count if over max mode number
if(counter > 8)
{
counter = 0;
}
else if(counter == 2) {
sensorValue = analogRead(analogpin1);
mappedval = map(sensorValue, 0, 1023, 5, 300);
// turn the ledPin on
digitalWrite(pin13, HIGH);
digitalWrite(pin11, LOW);
digitalWrite(pin10, HIGH);
digitalWrite(pin9, LOW);
// stop the program for <sensorValue> milliseconds:
// sensorValue = analogRead(analogpin1);
// mappedval = map(sensorValue, 0, 1023, 5, 300);
// delay(mappedval);
// turn the ledPin off:
// digitalWrite(pin13, HIGH);
// digitalWrite(pin11, LOW);
// digitalWrite(pin10, HIGH);
// digitalWrite(pin9, LOW);
sensorValue = analogRead(analogpin1);
mappedval = map(sensorValue, 0, 1023, 5, 300);
delay(mappedval);
digitalWrite(pin13, HIGH);
digitalWrite(pin11, LOW);
digitalWrite(pin10, LOW);
digitalWrite(pin9, HIGH);
sensorValue = analogRead(analogpin1);
mappedval = map(sensorValue, 0, 1023, 5, 300);
delay(mappedval);
digitalWrite(pin9, LOW);
}
buttonState1 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState1 != lastButtonState1) {
// if the state has changed, increment the counter
if (buttonState1 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
counter++;
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState1 = buttonState1;
// Reset count if over max mode number
if(counter > 8)
{
counter = 0;
}
else if(counter == 3){
sensorValue = analogRead(analogpin1);
mappedval = map(sensorValue, 0, 1023, 1, 255);
val9 = random(1,mappedval); // pick a random number between 100 and 255
analogWrite(pin9, val9); // set the LED brightness
val10 = random(1,mappedval); // pick a random number between 100 and 255
analogWrite(pin10, val10); // set the LED brightness
val11 = random(1,mappedval); // pick a random number between 100 and 255
analogWrite(pin11, val11); // set the LED brightness
// set the LED brightness
delayval = random(50,150); // pick a random number between 30 and 100
delay(delayval); // delay that many milliseconds
}
buttonState1 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState1 != lastButtonState1) {
// if the state has changed, increment the counter
if (buttonState1 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
counter++;
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState1 = buttonState1;
// Reset count if over max mode number
if(counter > 8)
{
counter = 0;
}
else if(counter == 4){
sensorValue = analogRead(analogpin1);
mappedval = map(sensorValue, 0, 1023, 5, 300);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
delay(mappedval);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
buttonState1 = digitalRead(buttonPin1);
// compare the buttonState to its previous state
if (buttonState1 != lastButtonState1) {
// if the state has changed, increment the counter
if (buttonState1 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
counter++;
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState1 = buttonState1;
// Reset count if over max mode number
if(counter > 8)
{
counter = 0;
}
else if(counter == 5){
digitalWrite(9, HIGH);
delay(50);
}
else if(counter == 6){
digitalWrite(10, HIGH);
delay(50);
}
else if(counter == 7){
digitalWrite(11, HIGH);
delay(50);
}
else if(counter == 8 ){
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
delay(50);
}
else
{
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
}
Serial.println(mappedval);
Serial.println(counter);
}