The code shown below is to “make the LEDs blink like the previous sketch during the 15 seconds”. The previous sketch (that is, the code in the question) turns the LEDs on for 600 ms and off for 200. The code below reverses the order – that is, does off for 200 and on for 600 – and repeats that pattern during the first 15 seconds after conditions being met. The annoyingly complicated if
statements in the code are to prevent retriggering; that is, to prevent another 15-second blink session immediately starting up if the trigger conditions are still present. Your problem statement is not perfectly clear on how that should be handled.
// 6 Flashing LED Lights With A Pushbutton and FSR Sensor
enum { LedPin6=2, LedPin5, LedPin4, LedPin3, LedPin2, LedPin1 };
enum { buttonPin=8, SensorPin = A0};
enum { quantum=200, sessionLength=15000/quantum};
byte buttonState = 0;
byte SensorPinReading;
byte blinkLEDs=false, sessionDone=false;
unsigned long baseMilli;
void setLEDlevels(byte level) {
digitalWrite(LedPin1, level);
digitalWrite(LedPin2, level);
digitalWrite(LedPin3, level);
digitalWrite(LedPin4, level);
digitalWrite(LedPin5, level);
digitalWrite(LedPin6, level);
}
void setup() {
Serial.begin(115200);
pinMode(LedPin1, OUTPUT);
pinMode(LedPin2, OUTPUT);
pinMode(LedPin3, OUTPUT);
pinMode(LedPin4, OUTPUT);
pinMode(LedPin5, OUTPUT);
pinMode(LedPin6, OUTPUT);
pinMode(buttonPin,INPUT);
pinMode(SensorPin, INPUT);
}
void loop() {
SensorPinReading = analogRead(SensorPin);
Serial.print("Analog reading = ");
Serial.print(SensorPinReading);
if (SensorPinReading < 10) {
Serial.println(" - No Pressure");
} else if (SensorPinReading < 200) {
Serial.println(" - Light Touch");
} else if (SensorPinReading < 500) {
Serial.println(" - Light Squeeze");
} else if (SensorPinReading < 800) {
Serial.println(" - Medium Squeeze");
} else {
Serial.println(" - Big Squeeze");
}
buttonState = digitalRead(buttonPin);
// Turn on 15-second blink session if conditions met
if (buttonState == HIGH && SensorPinReading > 10) {
if (!blinkLEDs && !sessionDone) { // Is session eligible?
blinkLEDs = true;
sessionDone = false;
baseMilli = millis();
}
} else { // A condition failed.
if (sessionDone) // Has session finished?
blinkLEDs = sessionDone = false; // Enable next session
}
if (blinkLEDs) {
int age = (millis()-baseMilli)/quantum;
setLEDlevels((age&3)>0); // Off for 1 quantum, on for 3 quanta
// See if blink session has run long enough
if (age > sessionLength) {
blinkLEDs = false; // Stop blinking
sessionDone = true; // Prevent retrigger until conditions reset
setLEDlevels(LOW); // Turn off lights
}
}
}
The statement setLEDlevels((age&3)>0)
calls setLEDlevels()
with a parameter value of 0 or 1. It will be 0 if age
is a multiple of 4. In binary, multiples of 4 have their lowest two bits clear. It will be 1 if age
is not a multiple of 4. This will alternately turn the LEDs off for one quantum (200 ms) and then on for 3 quanta (600 ms).
Edit 1: To “make the sensor read input while the button is not pressed, and then relay that input once the button is pressed so the LEDs immediately light up when it is pressed”, add a state variable (eg sensorPressed
) to use in place of SensorPinReading > 10
. Whenever SensorPinReading
is large enough (and any other necessary conditions are met), set sensorPressed
. When you start a blink session, reset sensorPressed
(make it false).
Note, from your comment, you've added the following code near the end of loop()
:
if (buttonState == LOW && blinkLEDs == true) {
blinkLEDs = false;
sessionDone = true;
setLEDlevels(LOW);
}
A minor issue with that code is it leaves sessionDone = true
. It is minor because the next pass through loop()
usually will execute the else
branch (of the if (buttonState == HIGH && SensorPinReading > 10)
if-statement) which will go ahead and set sessionDone = false
to enable the next blink session. Assuming you want to shut down blinking whenever the button is low, what I would do instead of that little code block is this:
if (!buttonState) {
blinkLEDs = sessionDone = false;
setLEDlevels(LOW);
}
(The expression !buttonState
has the same value as buttonState == LOW
and is four syllables shorter.)
Note, the code perhaps can be made more clear by using a state machine to control what happens. To do so, draw a diagram with nodes representing possible states – such as “idle” or “waiting for inputs”, “enabled”, “blinking”, “after blinking”. Draw lines between states, each labeled with the condition that triggers the particular state transition. State machines can be implemented using switch and case statements, or via an if–else sequence.