#include <Arduino.h>
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TFT_DC 27
#define TFT_CS 5
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
unsigned long interval = 5000; // This is the interval for the timer
bool timer_up = 0; // This variable holds when the time is up
bool timer(unsigned long fn_interval, bool fn_button); // This is the function for the timer
void color(unsigned long currentTime, unsigned long totalTime); // This is the function for the time dependent color-changing LED
// void button_ISR(); //This holds the interupt service routine for button that has the attach interupt function
void setup()
{
// Start the serial moniter
Serial.begin(115200);
Serial.println("start");
// attachInterrupt(digitalPinToInterrupt(32), button_ISR,CHANGE); //Attach interupt to the button
tft.begin();
tft.setCursor(26, 120);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
tft.println("Timer Cube v.1");
}
void loop()
{
// Needs improvement
bool y = digitalRead(32); // Read the button's status
timer_up = timer(interval, y); // Set the timer counting to x_interval, and have it resettable by the button (var: y)
if (timer_up == 1) // If the timer is done, then display it in the serial moniter
{
Serial.println("Time's up");
}
}
/*
This function is a standalone timer
Record current time as the startup time
Current time - startup time = remaining time on the timer
If the button is pressed, then reset the "startup time" to the "current time"
Check whether the time remaining is more than x interval time
If so then return true
else keep checking everything
*/
bool timer(unsigned long fn_interval, bool fn_button)
{
static unsigned long fn_startup_time = millis(); // This records the first millis, then it'll remember that first value
unsigned long remaining_time = (millis() - fn_startup_time);
color(remaining_time, fn_interval);
Serial.print("Time Left:");
Serial.println(remaining_time);
if (fn_button == HIGH) // If the button is pressed then reset the variable "fn_startup_time" to the current time
// to reset the timer
{
fn_startup_time = millis();
}
if (remaining_time >= fn_interval)
{
return true;
}
else
{
return false;
}
}
/*
bool timer (unsigned long fn_prev_time, unsigned long fn_interval)
{
unsigned long remaining_time = (millis() - fn_prev_time);
color(remaining_time, fn_interval);
if (remaining_time >= fn_interval)
{
return true;
}
else{
return false;
}
}
*/
/*
color:
This function is a time dependent color-changing LED
It starts by taking both the remaining time of the function and the interval of the timer
Then divides it by 3 (to support 3 changes to colors)
The first stage is green -> (white) purple -> Blue
Each color's if statement:
check if it's in the correct time frame for the color
if it is, then map the remaining time to a value from 0-1028(max of led) (eg. 1/2 time left in time frame, color = 512 (1/2 brightness))
while decreasing the color of the previous time frame
change the LED to the corresponding color
-> purple (differs by changing both Red and Blue colors to a max of 512 (instead of 1028))
*/
void color(unsigned long fn_remaining_time, unsigned long fn_total_time)
{
unsigned long total_time_div_3 = (fn_total_time / ((unsigned long)3));
// Serial.println(fn_remaining_time);
// Green
if (fn_remaining_time < (total_time_div_3) && fn_remaining_time >= 0) // Check if it's in the correct time frame
{
unsigned long time_remaining_mapped_1 = map(fn_remaining_time, 0, total_time_div_3, 0, 1028); // Map the remaining time to a brightness of the LED
// map(value, fromLow, fromHigh, toLow, toHigh)
analogWrite(25, time_remaining_mapped_1); // Change the LED accordingly
Serial.print("G:");
Serial.println(time_remaining_mapped_1);
}
//-> purple
if (fn_remaining_time < (total_time_div_3 * 2) && fn_remaining_time >= (total_time_div_3)) // Check if it's in the correct time frame
{
unsigned long time_remaining_mapped_2 = map(fn_remaining_time, (total_time_div_3), (total_time_div_3 * 2), 0, 512); // Map the remaining time to a (1/2) brightness of the LED
unsigned long green_temp = (1028 - (time_remaining_mapped_2 * 3)); // Reduce the Green LED's 3x faster then the Red, Blue LED's are increasing
if (green_temp <= 1028) // To avoid spikes in color, if it's over 1028 then just turn off the led
{
analogWrite(25, 1028 - (time_remaining_mapped_2 * 3));
Serial.print("G:");
Serial.print(1028 - (time_remaining_mapped_2 * 3));
}
else
{
analogWrite(25, 0);
}
analogWrite(33, time_remaining_mapped_2);
Serial.print(" R:");
Serial.print(time_remaining_mapped_2);
analogWrite(26, time_remaining_mapped_2);
Serial.print(" B:");
Serial.println(time_remaining_mapped_2);
}
//-> blue
if (fn_remaining_time < (total_time_div_3 * 3) && fn_remaining_time >= (total_time_div_3 * 2)) // Check if it's in the correct time frame
{
unsigned long time_remaining_mapped_3 = map(fn_remaining_time, (total_time_div_3 * 2), (total_time_div_3 * 3), 0, 1028); // Map the remaining time to a brightness of the LED
// analogWrite(25, 1028-(time_remaining_mapped_3));
analogWrite(25, 0); // Keep the green turned off
Serial.print("G:");
Serial.print("0");
// Serial.print(1028-(time_remaining_mapped_3));
analogWrite(33, 1028 - time_remaining_mapped_3); // Reduce the red proportional to the time remaining
Serial.print(" R:");
Serial.print(1028 - (time_remaining_mapped_3));
analogWrite(26, 512 + (time_remaining_mapped_3 / 1.5)); // increase the blue 0.5x slower than the other colors
Serial.print(" B:");
Serial.println(512 + (time_remaining_mapped_3 / 1.5));
}
// Green -> Purple -> Blue
// analogWrite(33, 0); //Red
// analogWrite(25, 0); //Green
// analogWrite(26, 0); //Blue
}