Troble Reading Vibration Sensor

I am trying to use a vibration sensor to read when a target has been knocked and I am running into a problem reading the vibration sensor. When I do an analog read for the sensor, I only get values of around 1050. I can sometimes get the levels to drop to around 900, but it takes quite a bit of vibrations for the sensor to register the levels. If the levels drop too low, then my board locks up.

If I try to do a digital read on the sensor, all I get is a 1 when I am reading my sensor and it never goes to 0.

This is the sensor that I am trying to use:

Here is the code of my program.

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

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void nintySeconds();
const int timeSelect = 2;
const int shockSense= 5;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
//int senseShock;



void setup() {
  Serial.begin(9600);

    lcd.begin(16,2);

   for(int i = 0; i< 3; i++)
  {
    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
  }
  lcd.backlight();

  pinMode(timeSelect, INPUT);
  pinMode(shockSense, INPUT);

}

void loop() {
  
  //int senseShock = analogRead(shockSense);
  lcd.setCursor(0,0);
  lcd.print("Welcome To The ");
  lcd.setCursor(0,1);
  lcd.print("Nerf Gun Target!");
  delay(1000);

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Please Select A ");
  lcd.setCursor(0,1);
  lcd.print("Length Of Time");
  delay(500);
  //Serial.println(time90);

  buttonState = digitalRead(timeSelect);

    if (buttonState != lastButtonState)
      {
        if(buttonState == HIGH)
          {
            buttonPushCounter++;
            Serial.println(buttonPushCounter);
            
          }
        if(buttonPushCounter > 3)
        {
          buttonPushCounter = 0;
          Serial.println(buttonPushCounter);
        }
        //delay(50);
      }
      lastButtonState = buttonState;
     // Serial.println("buttonState");
     // Serial.println(buttonState);

  
  
  switch(buttonPushCounter)
    {
      case 1:
        nintySeconds();
        break;
       case 0:
        Serial.println("NOPE");
    }

}

void nintySeconds()
  {
    int beginTime;
    int countDownTime;
    int points = 0;
    int shockVal;
    //int senseShock;
    //senseShock = analogRead(shockSense);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Your Time Begins in");
    lcd.setCursor(0,1);
    lcd.print("5 seconds");
    
    for(beginTime = 5; beginTime >=0; beginTime--)
      {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(beginTime);
        lcd.print(" Seconds");
        delay(1000);
        //buttonPushCounter = 0;
      }
    for(countDownTime = 90; countDownTime >=0; countDownTime--)
    {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(countDownTime);
        lcd.print(" Seconds");
        delay(1000);
        shockVal = digitalRead(shockSense);
        delay(1000);
        Serial.println("shockSense");
        Serial.println(shockSense);
        Serial.println(shockVal);  
    }
buttonPushCounter = 0;
  }

Any help would be greatly appreciated

I suggest to start with the code provided in that tutorial, and learn how the sensor actually works.

The following reads the shock sensor for about 62 billionths of a second, every two seconds.

How do you imagine that will work? (Hint: don't ever use delay(), if you can avoid it).

        delay(1000);
        shockVal = digitalRead(shockSense);
        delay(1000);
        Serial.println("shockSense");
        Serial.println(shockSense);

The first delay is my attempted to great a ninety-second timer and I was trying to use the delay as a debounce for the vibration sensor.

Do you understand the terrible problem that those delays create?

Yes, I understand the problems that the delays are causing in my program. I am currently trying to find a solution to the problems. If I can figure out how to use a timer, then I can do away with one of the delays. So far, I haven't had much luck in figuring out how to make a timer work.

The Arduino has a timer built in, called millis(), and it is used in the example that you linked in the OP. I suggested that you study that example carefully.

Also study the "blink without delay" tutorial that shows you how to create reasonably accurate timing intervals without (surprise!) using delay().

By using these techniques your program can VERY frequently check the switch (as in 10,000 to 100,000 times each second) and do other useful things at the same time.

Okay, I am trying to work with the millis() instructions and I have been looking over the different examples. I am still having issues getting my 90 second timer to work. As far as I can tell, I have done everything exactly the same as the examples and I get what is going on in the examples but its not helping me solve my problem.

millis() gives me the current time that my program had been running, if my program is running for more than 90 seconds, I will never be able to execute my 90 second countdown timer consistently. So far, I haven't come across an example where mills is used exclusively as a countdown timer.

This is the updated code that I am trying to get working. At this point I am just trying to figure out how to get the 90 second countdown timer to work.

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

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void nintySeconds();
const int timeSelect = 2;
const int shockSense= 5;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
unsigned long prevMills = 0;
const long ninetySec = 90000;
//int senseShock;



void setup() {
  Serial.begin(9600);

    lcd.begin(16,2);

   for(int i = 0; i< 3; i++)
  {
    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
  }
  lcd.backlight();

  pinMode(timeSelect, INPUT);
  pinMode(shockSense, INPUT);

}

void loop() {
  
  //int senseShock = analogRead(shockSense);
  lcd.setCursor(0,0);
  lcd.print("Welcome To The ");
  lcd.setCursor(0,1);
  lcd.print("Nerf Gun Target!");
  delay(1000);

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Please Select A ");
  lcd.setCursor(0,1);
  lcd.print("Length Of Time");
  delay(500);
  //Serial.println(time90);

  buttonState = digitalRead(timeSelect);

    if (buttonState != lastButtonState)
      {
        if(buttonState == HIGH)
          {
            buttonPushCounter++;
            Serial.println(buttonPushCounter);
            
          }
        if(buttonPushCounter > 3)
        {
          buttonPushCounter = 0;
          Serial.println(buttonPushCounter);
        }
        //delay(50);
      }
      lastButtonState = buttonState;
     // Serial.println("buttonState");
     // Serial.println(buttonState);

  
  
  switch(buttonPushCounter)
    {
      case 1:
        nintySeconds();
        break;
       case 0:
        Serial.println("NOPE");
    }

}

void nintySeconds()
  {
    int beginTime;
    int countDownTime;
    int points = 0;
    int shockVal = HIGH;
    unsigned long timer = millis();
    //int senseShock;
    //senseShock = analogRead(shockSense);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Your Time Begins in");
    lcd.setCursor(0,1);
    lcd.print("5 seconds");
    
    for(beginTime = 5; beginTime >=0; beginTime--)
      {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(beginTime);
        lcd.print(" Seconds");
        delay(1000);
        //buttonPushCounter = 0;
        Serial.println(timer);
        Serial.println(prevMills);
      }
      if(timer - prevMills <= ninetySec)
        {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(countDownTime);
        lcd.print(" Seconds");
        //delay(1000);
        shockVal = digitalRead(shockSense);
        //delay(1000);
        prevMills = timer;
        //timer = prevMills;
        Serial.println("shockSense");
        Serial.println(shockSense);
        Serial.println(shockVal);  
      }
        
    
buttonPushCounter = 0;
  }

millis() gives me the current time that my program had been running, if my program is running for more than 90 seconds, I will never be able to execute my 90 second countdown timer consistently.

That you have such silly ideas demonstrates that you don't understand the basic examples yet.

You need to understand those FIRST, before you start to modify a program full of irrelevant code. Your stubborn approach is holding you back.

Take a few minutes to study, download, run, modify and understand the "blink without delay" example, linked in reply #5.

The instructions for mills() specifically state that mills returns the number of milliseconds since the program started. millis() - Arduino Reference

I have been over the blink without delay tutorial several time and yes, I am still trying to figure it out. That would be why I am asking for help. The reason for the double post was to find someone that would be willing to help me get a better understanding of the code than just simply tell me to look at a tutorial.

My question is not silly, it is a valid question. If mills() returns the time that program has been running, how to I increment the counter so that I know when 90,000 milliseconds has passed?

At the start of the period you want to time, record the value that millis returns.
Keep reading millis until the value it returns MINUS the time you saved exceeds 90000

Blink without delay explained, line by line

It is utterly amazing what you can find on the web, if you take a few seconds to look!

You are utterly rude and condescending. I searched for as many example on the the blink without delay tutorials and given the fact that the internet is so vast surprise surprise my searches didn't turn up your exact results.

You really are having "troble".

That link was the second hit on search Google

An excellent guide! Please study it carefully and you will be richly rewarded.

jremington:
You really are having "troble".

That link was the second hit on search

Not only that, there was a direct link earlier in the thread.

Okay, I have been going over the blinkwithnodelay code again. And as far as I can tell, there is nothing on in the code that will help solve me problem.

The line of code unsigned long currentMillis = millis();

Returns the millis

The line of code

previousMillis = currentMillis;

updates the previousMillis to what ever the current mills reads

The line of code

currentMillis - previousMillis > interval[code]

compares the difference between the currentMillis and the previous millis and if it is greater than a thousand the led flashes on.

This program works great for flashing a LED it doesn't work for creating a countdown timer.
If you do the math, currentMillis - previousMillis always equals 1001.

I am trying to countdown 90 seconds from when the loop in my program starts. So I need to read the mills at one point in time and hold it as a static value so that I can figure out when 90 seconds have passed.

I am sorry, but I really don't see how blink without delay helps me solve my problem.

Did you actually use and modify the blink without delay example to do other things, like change the on and off times of the LED, or to dim an LED on a PWM pin?

Or count down a variable that starts at 90?

I did play around with the code and the best that I could do was just to get the LED to stay on. I will keep playing with the code.

I have been playing around with the blink program and I think that I am on the right track at this point. I have a variable testVar that I am trying to increments to 90 and stop the loop. But the problem that I am running into is that the variable never increments past one.

This is my code

/*
  Blink without Delay

  Turns on and off a light emitting diode (LED) connected to a digital pin,
  without using the delay() function. This means that other code can run at the
  same time without being interrupted by the LED code.

  The circuit:
  - Use the onboard LED.
  - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
    and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
    is set to the correct LED pin independent of which board is used.
    If you want to know what pin the on-board LED is connected to on your
    Arduino model, check the Technical Specs of your board at:
    https://www.arduino.cc/en/Main/Products

  created 2005
  by David A. Mellis
  modified 8 Feb 2010
  by Paul Stoffregen
  modified 11 Nov 2013
  by Scott Fitzgerald
  modified 9 Jan 2017
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/

// constants won't change. Used here to set a pin number:
const int ledPin =  LED_BUILTIN;// the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:
const long interval = 1000;           // interval at which to blink (milliseconds)
int testVar;
void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the difference
  // between the current time and last time you blinked the LED is bigger than
  // the interval at which you want to blink the LED.
  unsigned long currentMillis = millis();
  testVar =0;
if(testVar <=90)
{
  testVar++;
  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    //testVar++;
    previousMillis = currentMillis;
    //testVar++;
   
    
    
      
    // 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(ledPin, ledState);
    //testVar++;
    Serial.println(testVar);
    
  }
  //testVar++;
}
  

    

}

no mater where I put testVar++, testVar either stays at 0 or 1.
it doesn't seem like the if currentMills - previousMillis < interval if loop is acting like a normal loop.

You set it to zero every time through loop.

But even when I tried setting testVar as a global variable it still wouldn't work.