code issues.

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);
}

:slight_smile:

Yeah, delay stops the entire program. Look at the 'blink without delay' example under Examples--> digital--> blink without delay

I would be thankful for any help

Go to that last post, click on modify.
Then select your code and hit the # icon.
Then click save.
Then :slight_smile:

How would you use blink w/o delay for multiple leds? like for this. . .

 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);

The issue isn't the number of LEDs. It's that it appears that you don't want to do anything while the delay is happening, except read a switch.

How would YOU handle turning the LEDs on and off at the right time, given only a pad of paper, a pencil, a watch, and some switches, while at the same time needing to listen for the phone to ring?

  • Turn some LEDs on, and write down the time.
  • Listen for the phone to ring.
  • Check the time. If it's time to change the status of the LEDs, change them, and note the time.
  • Repeat.

If it isn't a matter of turning the same set of LEDs on and off each time, you have a state machine (there's a google term for you). Each time it is time to do something, there is a change of state, as well.

But how do you make blink w/o delay adjustable? As I said before i'm new to programing.

But how do you make blink w/o delay adjustable?

if(currentMillis - previousMillis > interval) {

here, "interval" is a variable - just vary it!

Oh, ok. so you are saying to use this as a drop in replacement for the delay command.

if (counter == 1) {     
          // read the value from the sensor:
           sensorValue = analogRead(analogpin1); 
           mappedval = map(sensorValue, 0, 1023, 5, 300);
          // turn the ledPin on
           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);
         
           if(currentMillis - previousMillis > mappedval) {
           // turn the ledPin off:        
           digitalWrite(pin11, LOW);
           digitalWrite(pin10, HIGH);
           digitalWrite(pin9, LOW);}
           
          sensorValue = analogRead(analogpin1); 
           mappedval = map(sensorValue, 0, 1023, 5, 300);
           if(currentMillis - previousMillis > mappedval) {
           digitalWrite(pin11, LOW);
           digitalWrite(pin10, LOW);
           digitalWrite(pin9, HIGH);}
           
           sensorValue = analogRead(analogpin1); 
           mappedval = map(sensorValue, 0, 1023, 5, 300);

           if(currentMillis - previousMillis > mappedval) {
           digitalWrite(pin9, LOW);}
           
 
  }

when i ran the code it blinked very fast, like 60 Hz, its timing is set to blink around 2 Hz. but the main thing is that it is non responsive to any input. >:(

¿Where do you update currentMillis and previousMillis with millis() return value?

I'm not sure how to write this so that the lights will cycle using the blink with out delay. like having one light turn on then changes to the next light and so on. :frowning:

It appears you are looking for something like this. Hope this helps.

define these in void setup(), such as

long interval = 100; // for 0.1 second updates
long previousMillis = 0; // will store last time was updated

// ********************************************************************************
// Loop here endlessly ********************************************************************************
void loop()
{
    unsigned long currentMillis = millis();  // capture the current time

// compare the time to the previously stored time
    if (currentMillis - previousMillis >= interval) // more than our interval?
    {
      // if yes, save the time for comparison the next time around 
      previousMillis = currentMillis; 

// do your actions here that you want done on the intervals

    } // end of reaching our interval

// read your switches here, or change the LED states etc. that happen while you wait
//for the next interval to be reached
}