How to have no delay in program.

Hi.

What are the step's to have no delay in this program.

int led = 3;
int sound = A0;
int threshold = 400;
int sensorValue = 0; 

void setup() 
{
    Serial.begin(9600);
    pinMode(led,OUTPUT);
    pinMode(sound,INPUT);
}
 void loop() 
{
       int sensorValue = analogRead(sound);
    Serial.print("sensorValue ");
        Serial.println(sensorValue);
        
 if(sensorValue > threshold)
    {
           digitalWrite(led, HIGH);
           delay(100);

    }
           digitalWrite(led, LOW);
            delay(10);
        }

Blink without delay. Learn it.

Something like this, but what do I put in the dalay (xxxxxx)?

const int ledPin = 3;
int sound = A0;
int threshold = 400;
int sensorValue = 0; 

int ledState = LOW;          
long previousMillis = 0;  
long interval = 1000;


void setup() 
{
    Serial.begin(9600);
    pinMode(ledPin, OUTPUT);
}
 
void loop() 
{
        int sensorValue = analogRead(sound);
	Serial.print("sensorValue ");
        Serial.println(sensorValue);
        
  if(sensorValue > threshold)
	{
           digitalWrite(ledPin, HIGH);
           delay(100);
		
	}
{
           digitalWrite(ledPin, LOW);
            delay(10);
        }       
 unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    
    previousMillis = currentMillis;  
  }
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    digitalWrite(ledPin, ledState);
}

In blink without delay, you don't have any delay()s. The clue is in the name.

It's built into the IDE - read it.

  if(currentMillis - previousMillis > interval) {
    
    previousMillis = currentMillis;  
  }

This bit is correct - but since you are not doing anything when the interval elapses it's a bit like having a clock and not bothering to look at it.

You need to include within the IF statement the stuff you want to happen when the interval elapses.

And, as @AWOL says, you don't need the delay() function at all.

...R

Here is what I have come up with, its not working well.

What I am trying to do is when the sound sensor goes over the threshold it will will make the led stay on, when the threshold is less then the led will go off.

const int ledPin = 3;
int sound = A0;
int threshold = 400;
int sensorValue = 0; 

int ledState = LOW;          
long previousMillis = 0;  
long interval = 1000;

void setup() 
{
    Serial.begin(9600);
    pinMode(ledPin, OUTPUT);
}

void loop() 
{
        int sensorValue = analogRead(sound);
    Serial.print("sensorValue ");
        Serial.println(sensorValue);
        unsigned long currentMillis = millis();
        
    if(currentMillis - previousMillis > interval) 
   {
    previousMillis = currentMillis;      
   }
     if(sensorValue > threshold)
   {
     ledState = HIGH;
   }
    else
      ledState = LOW;

   digitalWrite(ledPin, ledState);
  }

How’s this? I call this example SerialBlinkwithoutDelay.

Kinda a cross between reading characters from the serial port and doing the whole blink thing. Maybe it helps?

// Constants 
const byte MAX_STRING_LEN = 40;
const byte LED_PIN        = 13;
const int  BLINK_INTERVAL = 1000;

// Variables
char inputString[MAX_STRING_LEN];  // a string to hold incoming data
unsigned long previousMillis = 0;  // Last time we blinked
byte ledState = LOW;               // Current state of LED (HIGH=On)
byte strLen   = 0;                 // current length of rec'd string

void setup() {
  Serial.begin(19200);             // Change baud rate to suit
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  // Check the serial port for any pending data
  if (processSerial()) {
    // Received a complete string. For now, just echo it back
    Serial.println(inputString);

    // Here is where I'd extract data from the string I received and do whatever I wanted.

    // Now setup for the next string
    inputString[0] = '\0';         // Make sure array is empty
    strLen         = 0;            // reset length to zero
  }   

  // Here is where you do the processing that needs to be done each
  // pass thru the loop (such as checking to see if it's time to blink)
  if(millis() - previousMillis > BLINK_INTERVAL) {
    // save the last time you blinked the LED 
    previousMillis = millis();   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable
    digitalWrite(LED_PIN, ledState);
  }  
}

// Check the Serial port and gather any pending data
boolean processSerial() {
  while (Serial.available()) {
    char inChar = (char)Serial.read(); 

    // We are done receiving the string if we received a return (or line feed)
    if ((inChar == '\n') || (inChar == '\r')) {
      return true;
    }

    // add it to the inputString if we have room
    if (strLen < (MAX_STRING_LEN - 1)) {
      inputString[strLen++] = inChar;
      inputString[strLen]   = '\0';
    }
  }

  return false;
}

PS You’re not following Robin2’s suggestion… You’re doing the time check correctly but you have an extra “}” before you’re actually doing what you need.

Hope this helps,

Brad
KF7FER

EDIT: Since you’re so close, always try to use the autoformat command (Control-T). If I move one closing bracket your code looks like:

void loop() 
{
  int sensorValue = analogRead(sound);
  Serial.print("sensorValue ");
  Serial.println(sensorValue);
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) 
  {
    previousMillis = currentMillis;      

    if(sensorValue > threshold)
    {
      ledState = HIGH;
    }
    else
      ledState = LOW;

    digitalWrite(ledPin, ledState);
  }
}

I think that’s closer to what you want, eh?

EDIT2: Sorry I didn’t read your question close enough. If you are really asking

What I am trying to do is when the sound sensor goes over the threshold it will will make
the led stay on, when the threshold is less then the led will go off.

Why do you need a delay? To do exactly what you are asking, you had the code nearly perfect in your original posting. It should be something like

 void loop() {
    int sensorValue = analogRead(sound);
	
    Serial.print("sensorValue ");
    Serial.println(sensorValue);
        
    if(sensorValue > threshold) 
           digitalWrite(led, HIGH);
    else
           digitalWrite(led, LOW); 
}

Any closer to what you want?