// pin connections constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPins[] = {8, 9, 10 }; //the pins that the ambient LED's are attached to
const int motorPin = 3; //the pin the motor is attached to
const int uvPin = 4; // the pin that the UV LED's are attached to
//const int speakerPin = 5; //TBA pin the attached connect to
// Button control Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 1; // previous state of the button
unsigned long lastPush = 0;
byte intervalDebounce = 50;
// UV timeout settings
int ledState = LOW;
unsigned long uvPreviousMillis = 0;
const long uvTimeOut = 4000; //TBA 900000 (15 min) for final code
bool timeExpired = false;//have we timed out yet..
//knight rider timing
unsigned long previousMillis = 0;
static long TIMER = 75;
int knightLight = 0;
int knightCounter = 1;
void setup() {
//knight rider setup start
for (int p = 0; p < 3; p++) {
pinMode(ledPins[p], OUTPUT);
} //knight rider setup end
// Initialize button pin
pinMode(buttonPin, INPUT_PULLUP); // initialize the button pin as a input:
// initialize the uvLED's,motor and speaker as an output:
pinMode(motorPin, OUTPUT);
pinMode(uvPin, OUTPUT);
//pinMode(speakerPin, OUTPUT);
Serial.begin(9600); // initialize serial communication:
}
void loop() {
unsigned long now = millis();
if (now - lastPush >= intervalDebounce) {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
lastPush = now;//start debouncing..
lastButtonState = buttonState;
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.print("Button Pushes");
Serial.println(buttonPushCounter);
if (buttonPushCounter == 3) uvPreviousMillis = now;//set count down timer..
}
}
}
// save the current state as the last state, for next time through the loop
// Controlling the Clicks
if (buttonPushCounter > 0) {
knightRider(); //one click turns on the ambient LED with effect
}
if (buttonPushCounter > 1) {
digitalWrite(motorPin, HIGH); //two cliks turns on the motor while ambient LED stays on
}
if (buttonPushCounter > 2) {
if (!timeExpired) {
//turn uv lights on..
digitalWrite(uvPin, HIGH);
ledState = HIGH;
}
uvLightsOut();
} //three clicks turns on the uvLEDs while motor & ambient LEDs stays on
if (buttonPushCounter > 3) {
//digitalWrite(speakerPin, HIGH); //four clicks turns on speaker
}
if (buttonPushCounter > 4) {
digitalWrite(motorPin, LOW);
digitalWrite(uvPin, LOW);
// digitalWrite(speakerPin, LOW); //Five clicks turns all LED's, motor and speaker off
}
if (buttonPushCounter == 5)buttonPushCounter = 0; //reset button counter to 0
}
//knightRider function
void knightRider() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > TIMER) {
previousMillis = currentMillis;
// reset last knightlight
digitalWrite(ledPins[knightLight], LOW);
// calc new knight light
knightLight = knightLight + knightCounter;
if (knightLight > 3 ) {
knightLight = 3;
knightCounter = -1;
}
if (knightLight < 0) {
knightLight = 0;
knightCounter = 1;
}
// set new knight light
digitalWrite(ledPins[knightLight], HIGH);
}
}
// Timeout for UV light Function
void uvLightsOut() {
// check to see if it's time to turn off the uvLED; that is, if the difference
// between the current time and last time you turned on the LED is bigger than
// the timeOut at which you want to turn off the uvLED.
unsigned long uvCurrentMillis = millis();
if (uvCurrentMillis - uvPreviousMillis >= uvTimeOut) {
//time to turn off..
timeExpired = true;//our time has expired
ledState = LOW;//set led state low
// set the LED with the ledState of the variable:
digitalWrite(uvPin, ledState);
}
}