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?