Control of flashing led's?

Hi, I am thinking of building an Intruder alarm with flashing, warning led's, I would like to use a piezo knock sensor, to turn on the flashing led's as the knock sensor detects vibration, how can this be done?

What are you using as a knock sensor ?

A circular Peizo Disc.

Have you tried reading from it using either analogRead() or digitalRead() ?

Yes, I can control one Led, by activating Led with a knock to the sensor, and use delay(5000); to keep it HIGH, what I need is to control a flashing Led, not a stable one.

Do you want one knock to turn on the flashing LEDs and the next one to turn it off or something else ?

Just one knock to start the Led's flashing, and a delay to turn them off.

When you detect a knock set a boolean variable to true.

Later in loop() if the boolean is true blink the LED using millis() for timing. See Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Count the LED blinks and when the requisite number have occurred set the boolean to false to stop the blinking

Great, I will give it a try.

Hi UKHeliBob, I have been on this now for some 3 hours, it looks above my skill level, I just want something simple to with less on and off again on receipt of a signal from the knock sensor, more is there no such thing?

Hi,
Can you post t he code you have at the moment and a circuit diagram please?

Thanks.. Tom.. :grinning: :+1: :coffee: :australia:

[code]
//Sketch by Ray Smith 18/05/2021 for Parsons Close Mancave, Alarm System.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 20, 4);// Set the LCD address to 0x27 for a 20 chars and 4 line display

int ledPin1 = 2; 
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 5;
int ledPin5 = 6;
int ledPin6 = 7;
int ledPin7 = 8;
int ledPin8 = 9; 
int ledPin9 = 10;
int ledPin10 = 11;

void setup() {
 
lcd.begin();// initialize the LCD,
lcd.backlight();// Turn on the blacklight and print a message.

pinMode(ledPin1, OUTPUT); 
pinMode(ledPin2, OUTPUT); 
pinMode(ledPin3, OUTPUT); 
pinMode(ledPin4, OUTPUT); 
pinMode(ledPin5, OUTPUT); 
pinMode(ledPin6, OUTPUT); 
pinMode(ledPin7, OUTPUT); 
pinMode(ledPin8, OUTPUT);
pinMode(ledPin9, OUTPUT); 
pinMode(ledPin10, OUTPUT);
 
}

void loop() {
digitalWrite(ledPin1,HIGH);
delay(75);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,HIGH);
delay(75); 
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,HIGH);
delay(75);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,HIGH);
delay(100);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,HIGH);
delay(75);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,HIGH);
delay(75);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,HIGH);
delay(75);
digitalWrite(ledPin7,LOW);
digitalWrite(ledPin8,HIGH);
delay(75);
digitalWrite(ledPin8,LOW);
digitalWrite(ledPin9,HIGH);
delay(75);
digitalWrite(ledPin9,LOW);
digitalWrite(ledPin10,HIGH);
delay(75);
digitalWrite(ledPin10,LOW);
digitalWrite(ledPin9,HIGH);
delay(75);
digitalWrite(ledPin9,LOW);
digitalWrite(ledPin8,HIGH);
delay(75); 
digitalWrite(ledPin8,LOW);
digitalWrite(ledPin7,HIGH);
delay(75); 
digitalWrite(ledPin7,LOW);
digitalWrite(ledPin6,HIGH);
delay(75); 
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin5,HIGH);
delay(75); 
digitalWrite(ledPin5,LOW);


}




Hi, This sketch works, it's a sketch to control a LCD, and a set of LED's set in the Knightrider sequence, what I would like to do is for the LCD & LED's to come on when a knock sensor hs been activated, sorry no schematic, I use POWERPOINT, I do not know how to up load this file












      
[/code]```

Based on your original description you may get some ideas from this

unsigned long currentTime;
unsigned long blinkStartTime;
unsigned long blinkPeriod = 500;
byte blinkCount = 0;
boolean blinking = false;
const byte buttonPin = A3;
byte currentButtonState = HIGH;
byte previousButtonState = HIGH;
byte ledPin = 3;

void setup()
{
  Serial.begin(115200);
  while (!Serial);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH); //start with LED off
}

void loop()
{
  currentTime = millis();
  currentButtonState = digitalRead(buttonPin);
  if (currentButtonState != previousButtonState && currentButtonState == LOW) //buttton became pressed
  {
    blinking = true;
    blinkStartTime = currentTime;
    blinkCount = 0;
  }
  previousButtonState = currentButtonState;
  

if (blinking)  //blink LED if blinking is true
  {
    if (currentTime - blinkStartTime >= blinkPeriod)  //time to blink ?
    {
      digitalWrite(ledPin, !digitalRead(ledPin));
      blinkStartTime = currentTime;
      blinkCount++;
      if (blinkCount == 10) //done enough blinks ?
      {
        blinking = false; //turn off blinking
        digitalWrite(ledPin, HIGH); //turn off teh LED
      } //test if enough blinks have been done
    } //test if time to blink
  } //test if blinking
}

As written the LED starts blinking when an input goes LOW but you could adapt it to start when a knock is detected

Hi, Just to let you know, I am struggling with this, I still do not understand how to start of the nightrider sequence in my sketch, with a knock sensor, here is my sketch again, can you help further?`

int ledPin1 = 2;
int ledPin2 = 3;
int ledPin3 = 4;
int ledPin4 = 5;
int ledPin5 = 6;
int ledPin6 = 7;
int ledPin7 = 8;
int ledPin8 = 9;
int ledPin9 = 10;
int ledPin10 = 11;

void setup() {

pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, INPUT);
pinMode(ledPin6, OUTPUT);
pinMode(ledPin7, OUTPUT);
pinMode(ledPin8, OUTPUT);
pinMode(ledPin9, OUTPUT);
pinMode(ledPin10, OUTPUT);

}

void loop() {

digitalWrite(ledPin1,HIGH);
delay(80);
digitalWrite(ledPin1,LOW);
digitalWrite(ledPin2,HIGH);
delay(80);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin3,HIGH);
delay(80);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin4,HIGH);
delay(80);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin5,HIGH);
delay(80);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin6,HIGH);
delay(80);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin7,HIGH);
delay(80);
digitalWrite(ledPin7,LOW);
digitalWrite(ledPin8,HIGH);
delay(80);
digitalWrite(ledPin8,LOW);
digitalWrite(ledPin9,HIGH);
delay(80);
digitalWrite(ledPin9,LOW);
digitalWrite(ledPin10,HIGH);
delay(80);
digitalWrite(ledPin10,LOW);
digitalWrite(ledPin9,HIGH);
delay(80);
digitalWrite(ledPin9,LOW);
digitalWrite(ledPin8,HIGH);
delay(80);
digitalWrite(ledPin8,LOW);
digitalWrite(ledPin7,HIGH);
delay(80);
digitalWrite(ledPin7,LOW);
digitalWrite(ledPin6,HIGH);
delay(80);
digitalWrite(ledPin6,LOW);
digitalWrite(ledPin5,HIGH);
delay(80);
digitalWrite(ledPin5,LOW);
digitalWrite(ledPin4,HIGH);
delay(80);
digitalWrite(ledPin4,LOW);
digitalWrite(ledPin3,HIGH);
delay(80);
digitalWrite(ledPin3,LOW);
digitalWrite(ledPin2,HIGH);
delay(80);
digitalWrite(ledPin2,LOW);
digitalWrite(ledPin1,HIGH);
delay(80);
digitalWrite(ledPin1,LOW);
delay(80);

}

`

Back in reply #5 you said that you knew how to activate a single LED using the knock sensor

Start simple. Write a function that uses delay() to do the LED sequence that you want. Read the knock sensor and when you detect a knock, call the function

The function will almost certainly need to be changed or even scrapped but start with it

1 Like

Yes that's correct, I have learned to control one LED, I have used the example sketch in the IDE, what I want now is for the Knock sensor to start the "KNIGHTRIDER" sequence.
This is the IDE sketch for one Led, I am ok with this, but do I have to repeat the void loop10 times(the amount of Led's in my Sequence)?

const int ledPin = 13; // LED connected to digital pin 13
const int knockSensor = A0; // the piezo is connected to analog pin 0
const int threshold = 100; // threshold value to decide when the detected sound is a knock or not

// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int ledState = LOW; // variable used to store the last LED status, to toggle the light

void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
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) {
// toggle the status of the ledPin:
ledState = !ledState;
// update the LED pin itself:
digitalWrite(ledPin, ledState);
// send the string "Knock!" back to the computer, followed by newline
Serial.println("Knock!");
}
delay(100); // delay to avoid overloading the serial port buffer
}`

No

You already have the code for the sequence in reply #14. Put it in a function. Call the function when you detect a knock

Hi, I know what you mean, but I still cannot seem to get anything to work, where do I write a function? set up or loop? what do I call the function "Knight rider"? I know I am but a retired mechanical engineer, but learning by oneself is very difficult, OJT or college is much better, I have neither.

Based on a previous post of yours here is a function to run the Knight Rider sequence

void knightRider
{
  digitalWrite(ledPin1, HIGH);
  delay(80);
  digitalWrite(ledPin1, LOW);
  digitalWrite(ledPin2, HIGH);
  delay(80);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin3, HIGH);
  delay(80);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin4, HIGH);
  delay(80);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin5, HIGH);
  delay(80);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin6, HIGH);
  delay(80);
  digitalWrite(ledPin6, LOW);
  digitalWrite(ledPin7, HIGH);
  delay(80);
  digitalWrite(ledPin7, LOW);
  digitalWrite(ledPin8, HIGH);
  delay(80);
  digitalWrite(ledPin8, LOW);
  digitalWrite(ledPin9, HIGH);
  delay(80);
  digitalWrite(ledPin9, LOW);
  digitalWrite(ledPin10, HIGH);
  delay(80);
  digitalWrite(ledPin10, LOW);
  digitalWrite(ledPin9, HIGH);
  delay(80);
  digitalWrite(ledPin9, LOW);
  digitalWrite(ledPin8, HIGH);
  delay(80);
  digitalWrite(ledPin8, LOW);
  digitalWrite(ledPin7, HIGH);
  delay(80);
  digitalWrite(ledPin7, LOW);
  digitalWrite(ledPin6, HIGH);
  delay(80);
  digitalWrite(ledPin6, LOW);
  digitalWrite(ledPin5, HIGH);
  delay(80);
  digitalWrite(ledPin5, LOW);
  digitalWrite(ledPin4, HIGH);
  delay(80);
  digitalWrite(ledPin4, LOW);
  digitalWrite(ledPin3, HIGH);
  delay(80);
  digitalWrite(ledPin3, LOW);
  digitalWrite(ledPin2, HIGH);
  delay(80);
  digitalWrite(ledPin2, LOW);
  digitalWrite(ledPin1, HIGH);
  delay(80);
  digitalWrite(ledPin1, LOW);
  delay(80);
}

Call it when you detect a knock instead of lighting a single LED. This is a very clumsy way to run the sequence, so be prepared for suggestions to change it, but is a step in the right direction.

Does void knightrider replace void loop?