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.
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.
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
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.
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?
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.