I am running the following code to strobe an LED and fire off an SSR on a mini arduino pro. I would like to assign the duration separately, so the SSR disconnects after 2 seconds, but the LED flashes for 4 seconds.
const int ledPin = 12; // led connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 5; // threshold value to decide when the detected sound is a knock or not
const int ssrPin = 11;
// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int ledState = HIGH; // variable used to store the last LED status, to toggle the light
int timer = 20;
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
pinMode(ssrPin, OUTPUT);
pinMode(knockSensor, INPUT);
Serial.begin(9600); // use the serial port
}
void loop() {
// read the sensor and store it in the variable sensorReading:
sensorReading = analogRead(knockSensor);
// if the sensor reading is greater than the threshold:
if (sensorReading >= threshold) {
unsigned long startTime = millis();
do {
digitalWrite (ssrPin, HIGH);
digitalWrite(ledPin , !digitalRead(ledPin ));
delay(timer);
digitalWrite(ssrPin, LOW);
}
while (millis()- startTime < 4000);
}
}