Code:
-Alarm Function 100%
-Time Functions 20%
-Draw Interface Functions 85%
-Touch Screen and Collission Detection Function (for Buttons) 30%
-Generating and processing alarm array 100%
-Input Button Functions 0%
Hardware:
-Clock connected 0% (Placeholder variables atm)
-Display Connected 100%
-Touch Function connected 100%
-On/Off Switch connected 100%
-Sound Output connected 0%
-Buttons connected 0%
-Final product 20%
class Button
{
int posX, posY, sizeX, sizeY;
boolean isCollission, hasBorder;
String text;
public:
void init( String text, int posX, int posY, int sizeX, int sizeY, boolean hasBorder )
{
this->text = text;
this->posX = posX;
this->posY = posY;
this->sizeX = sizeX;
this->sizeY = sizeY;
this->hasBorder = hasBorder;
isCollission = false;
}
int getX()
{
return posX;
}
int getY()
{
return posY;
}
int getWidth()
{
return sizeX;
}
int getHeight()
{
return sizeY;
}
String getText()
{
return text;
}
boolean getBorder()
{
return hasBorder;
}
};
#include <Adafruit_TFTLCD.h>
#include <Adafruit_GFX.h>
#include "Button.h"
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
#define white 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
int timeHours = 10;
int timeMinutes = 20;
int timeSeconds = 0;
int displayMode = 0;
int tick = 0;
int alarmIteration = 0;
int alarmSize = 6;
int alarmCode[6];
boolean alarmIsEnabled = true;
boolean alarmIsPlaying = false; // set to false
int alarmHour = 9;
int alarmMinute = 40;
Button button[14];
void setup(void)
{
Serial.begin(9600);
pinMode(0, INPUT);
tft.reset();
uint16_t identifier = tft.readID();
tft.begin(identifier);
tft.setRotation(1);
tft.fillScreen(white);
// Generate Alarm. The alarm is a randomly generated array.
// On each Index a number between 1 and 4 is stored.
// Later the beeps will be generated in a sequence resulting
// as an output of iterating through this array.
for (int i = 0; i < alarmSize; i++)
{
int number = floor(random(1,5));
alarmCode[i] = number;
Serial.println(number);
}
//Generate Buttons
button[0].init("1",310,50,40,40,true);
button[1].init("2",360,50,40,40,true);
button[2].init("3",410,50,40,40,true);
button[3].init("4",310,100,40,40,true);
button[4].init("5",360,100,40,40,true);
button[5].init("6",410,100,40,40,true);
button[6].init("7",310,150,40,40,true);
button[7].init("8",360,150,40,40,true);
button[8].init("9",410,150,40,40,true);
button[9].init("<",310,200,40,40,true);
button[10].init("Ok",360,200,40,40,true);
button[11].init("Cancel",370,287,100,30,false);
button[12].init("Set Alarm",40,287,135,30,false);
button[13].init("alarm on",290,30,55,18,true);
}
void loop(void)
{
updateTime();
mechanics();
render();
}
void updateTime()
{
timeSeconds = int( floor( millis()/1000 ) ) % 60;
}
String timeToAlarm()
{
int currentTimeValue = timeHours * 60 + timeMinutes;
int alarmTimeValue = alarmHour * 60 + alarmMinute;
int returnedTime;
if (currentTimeValue < alarmTimeValue)
{
returnedTime = alarmTimeValue - currentTimeValue;
}
else
{
returnedTime = 24*60 - ( alarmTimeValue - currentTimeValue );
}
return String( int( floor( returnedTime/60 ) ) )+"h "+String( returnedTime%60 )+"m";
}
void render()
{
switch (displayMode)
{
case 0:
// String(timeHours)+":"+String(timeMinutes)+":"+String(textSeconds)
tft.setCursor(40, 30);
tft.setTextColor(WHITE,white);
tft.setTextSize(5);
if (timeHours < 10) tft.print('0');
tft.print(timeHours);
tft.print(":");
if (timeMinutes < 10) tft.print('0');
tft.print(timeMinutes);
tft.print(":");
if (timeSeconds < 10) tft.print('0');
tft.print(timeSeconds);
tft.setCursor(40, 85);
tft.setTextColor(RED,white);
tft.setTextSize(2);
tft.print("Alarm Time: ");
tft.setTextColor(WHITE,white);
tft.setTextSize(2);
if (alarmHour < 10) tft.print('0');
tft.print(alarmHour);
tft.print(":");
if (alarmMinute < 10) tft.print('0');
tft.print(alarmMinute);
tft.setCursor(40, 105);
tft.setTextColor(RED,white);
tft.setTextSize(2);
tft.print("Time Left: ");
tft.setTextColor(WHITE,white);
tft.setTextSize(2);
tft.print(timeToAlarm());
tft.setTextSize(2);
tft.setTextColor(RED,white);
tft.setCursor( button[12].getX() + 15, button[12].getY() + 5 );
tft.print( button[12].getText() );
if( alarmIsEnabled )
{
tft.setTextSize(1);
tft.drawRect( button[13].getX(), button[13].getY(), button[13].getWidth(), button[13].getHeight(), GREEN );
tft.setTextColor(GREEN,white);
tft.setCursor( button[13].getX() + 4, button[13].getY() + 5 );
tft.print( button[13].getText() );
}
else
{
tft.fillRect( button[13].getX(), button[13].getY(), button[13].getWidth(), button[13].getHeight(), white );
}
break;
case 1:
tft.setTextSize(2);
for(int i = 0; i < 12; i++)
{
if( button[i].getText() == "Cancel" )
{
tft.setTextColor(RED,white);
}
else
{
tft.setTextColor(WHITE,white);
}
if(button[i].getBorder() )
{
tft.drawRect( button[i].getX(), button[i].getY(), button[i].getWidth(), button[i].getHeight(), WHITE );
}
if( button[i].getText() == "Ok" )
{
tft.setCursor( button[i].getX() + 11, button[i].getY() + 14 );
}
else if( button[i].getText() == "Cancel" )
{
tft.setCursor( button[i].getX() + 15, button[i].getY() + 5 );
}
else
{
tft.setCursor( button[i].getX() + 15, button[i].getY() + 14 );
}
tft.print( button[i].getText() );
}
break;
}
}
void mechanics()
{
if(alarmIsPlaying)
{
playAlarm();
}
if( analogRead(A5) == 0 )
{
alarmIsEnabled = true;
}
if( analogRead(A5) > 0 )
{
alarmIsEnabled = false;
}
Serial.println( alarmIsEnabled );
}
void setAlarm(int h, int m)
{
alarmHour = h;
alarmMinute = m;
}
void triggerAlarm()
{
// The alarm is triggered when the hour equals "alarmHour", minute
// equals "alarmMinute" and the second is zero. "Second" must be 0!
// This function's if statement is being entered for 1 minute.
// So, if the user disables the alarm within 1min the function would
// be called again and the alarm would start all over again.
/*if( hour() == alarmHour && minute() == alarmMinute && alarmIsEnabled && second() == 0 ) { alarmIsPlaying = true; }*/
}
void disableAlarm()
{
// reset some variables
alarmIteration = 0;
}
void playAlarm()
{
if( millis() - tick > 300 )
{
// the alarmCode array is iterated through. a tone should be played
// after every 300ms. The tone lasts for 250ms with a puase of 50ms
// the frequency between each tone differs in 1000hertz.
switch (alarmCode[alarmIteration])
{
case 1:
tone(11, 1000, 250);
break;
case 2:
tone(11, 2000, 250);
break;
case 3:
tone(11, 3000, 250);
break;
case 4:
tone(11, 4000, 250);
break;
}
// alarmIteration is the Iterator because we cannot use a for-loop
// it is set to zero, after the alarmSequence has finished.
// after that, there should be a 1 second pause before the alarm
// is played again.
alarmIteration++;
if( alarmIteration > alarmSize-1 )
{
alarmIteration = 0;
tick = millis() + 1000;
}
else
{
tick = millis();
}
}
}