/* LucidTronix color compass
* Rainbow colored rtc clock
* For instructions, details and schematic, See:
* http://www.lucidtronix.com/tutorials/29
* Uses the RTClib.h library from adafruit
*/
#include <ST7735.h>
#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>
#include <color.h>
#define cs 10 // for MEGAs you probably want this to be pin 53
#define dc 9
#define rst 8 // you can also connect this to the Arduino reset
ST7735 tft = ST7735(cs, dc, rst);
// Reference the I2C LibraUry
#include <Wire.h>
int btnPin0 = 0;
int btnPin1 = 1;
int btnPin2 = 2;
int mode = 0;
int num_modes = 4;
int cur_sec = 0;
int cur_min = 0;
int millisecond_six = 0;
unsigned int last_sec = 0;
unsigned int last_press = 0;
RTC_DS1307 RTC;
DateTime cur_time, alarm_time;
char* stringy = "nothinghere";
void setup(){
Serial.begin(57600);
pinMode(btnPin2,INPUT);
pinMode(btnPin1,INPUT);
pinMode(btnPin0,INPUT);
tft.initR(); // initialize a ST7735R chip
tft.writecommand(ST7735_DISPON);
delay(1000);
tft.fillScreen(BLACK);
tft.drawString(4, 4, "Starting up...", CYAN,1);
Wire.begin(); // Start the I2C interface.
tft.drawString(4, 64,"Hello?!", GREEN);
RTC.begin();
if (! RTC.isrunning()) {
tft.drawString(14, 94,"RTC is NOT running!",RED);
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
delay(500);
tft.fillScreen(WHITE);
}
void loop(){
cur_time = RTC.now();
display_time_digital(2, 4,1);
display_date(2,135,1);
color_clock(64,80);
check_buttons();
}
void display_time_digital(int ax, int ay, int text_size){
print_integer(ax,ay, cur_time.hour(), text_size);
tft.drawString(ax+(text_size*10), ay,":",MAGENTA, text_size);
print_integer(ax+(text_size*15),ay, cur_time.minute(), text_size);
tft.drawString(ax+(text_size*25), ay,":",RED,text_size);
print_integer(ax+(text_size*30),ay, cur_time.second(), text_size);
if (cur_sec != cur_time.second() ){
last_sec = millis();
cur_sec = cur_time.second();
tft.fillRect(ax+(text_size*30), ay-2, text_size*12, text_size*10,WHITE);
}
}
void display_date(int ax, int ay,int text_size){
print_integer(ax,ay, cur_time.month(), text_size);
tft.drawString(ax+(text_size*10), ay,"/",GREEN,text_size);
print_integer(ax+(text_size*15),ay, cur_time.day(), text_size);
tft.drawString(ax+(text_size*25), ay,"/",YELLOW,text_size);
print_integer(ax+(text_size*30),ay, cur_time.year(), text_size);
}
void color_clock(int ax, int ay){
int hour_angle = cur_time.hour()*30;
int minute_angle = cur_time.minute()*6;
int second_angle = cur_time.second()*6;
if (cur_min != cur_time.minute() ){
cur_min = cur_time.minute();
tft.fillScreen(WHITE);
tft.drawString(58, 8,"12",BLACK,2);
}
if (cur_sec != cur_time.second() ){
last_sec = millis();
cur_sec = cur_time.second();
}
millisecond_six = constrain(((millis()-last_sec) / 167),0,6);
int second_angle_hi_res = (cur_sec*6) + millisecond_six;
draw_orbiter(ax,ay,hour_angle, 20,3);
draw_ray(ax,ay,hour_angle, 20);
draw_orbiter(ax,ay,minute_angle, 25,2);
draw_ray(ax,ay,minute_angle, 25);
draw_orbiter(ax,ay,second_angle_hi_res, 48,5);
}
void draw_ray(int ax, int ay, int angle, int radius ){
float ray_x = sin(((float)angle*PI)/180.0f)*(float)radius;
float ray_y = -cos(((float)angle*PI)/180.0f)*(float)radius;
int c = get_color_from_angle(angle);
tft.drawLine(ax,ay,ax+ray_x, ay+ray_y, c);
}
void draw_orbiter(int ax, int ay, int angle, int radius, int a_size){
float ray_x = sin(((float)angle*PI)/180.0f)*(float)radius;
float ray_y = -cos(((float)angle*PI)/180.0f)*(float)radius;
int c = get_color_from_angle(angle);
tft.fillCircle(ax+ray_x, ay+ray_y, a_size, c);
}
int get_color_from_angle(int angle){
Color cur_color=Color(0,0,0);
cur_color.convert_hcl_to_rgb((float)angle/360,0.95,0.7);
return cur_color.get_color_16bit();
}
void print_integer(int ax, int ay, int to_print, int text_size){
char aBuf[6];
String output = String(to_print);
output.toCharArray(aBuf,6);
tft.drawString(ax, ay,aBuf,BLUE,text_size);
}
void check_buttons(){
if ( digitalRead(btnPin0) == HIGH) tft.fillScreen(WHITE);
//else if ( digitalRead(btnPin1) == HIGH) tft.fillScreen(BLACK);
else if ( digitalRead(btnPin2) == HIGH) tft.fillScreen(RED);
}
Comments: