I got that part figured out. I am creating an interval of 1000 milliseconds and during that interval I am incriminating testVar by 1. When testVar reaches 90 the loop stops and it works perfectly. After 90 seconds the LED stops flashing.
When I tried implementing the same thing one my program it doesn't work. Is it becasue I am calling currentMillis90 from inside a function and not the void loop?
The Blink 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 = 0;
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;
//testVar++;
if ((currentMillis - previousMillis >= interval) && (testVar <=90)) {
// 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;
}
Serial.println(testVar);
testVar++;
/*if(testVar <= 90);
{
Serial.println(testVar);
testVar++;
}*/
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
//testVar++;
//Serial.println(testVar);
}
}
My program code
#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 previousMillis90 = 0;
const long interval = 1000;
int testVar = 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 = 90;
int points = 0;
int shockVal = HIGH;
int endTime = 90;
//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");*/
unsigned long currentMillis90 = millis();
/*for(beginTime = 5; beginTime >=0; beginTime--)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(beginTime);
lcd.print(" Seconds");
delay(1000);
//buttonPushCounter = 0;
}*/
//lcd.clear();
if((currentMillis90 - previousMillis90 >= interval) && (testVar <= 90))
{
previousMillis90 = currentMillis90;
//currentMillis90 = millis();
Serial.println(testVar);
Serial.println(currentMillis90);
Serial.println(previousMillis90);
/*if(countDownTime >= 0)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(countDownTime);
lcd.print(" SECONDS");
countDownTime--;
}*/
testVar++;
}else
{
Serial.println("LOOP DONE");
}
//buttonPushCounter = 0;
}