Skip to content

Instantly share code, notes, and snippets.

@Hackin7
Last active Dec 8, 2020
Embed
What would you like to do?
For the Clock and Sensor Box on Instructables
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <SPI.h>
#ifdef ADAFRUIT_HALLOWING
#define TFT_CS 39 // Hallowing display control pins: chip select
#define TFT_RST 37 // Display reset
#define TFT_DC 38 // Display data/command select
#define TFT_BACKLIGHT 7 // Display backlight pin
#else
// For the breakout board, you can use any 2 or 3 pins.
// These pins will also work for the 1.8" TFT shield.
#define TFT_CS 10
#define TFT_RST 7 // Or set to -1 and connect to Arduino RESET pin
#define TFT_DC 6
#endif
// OPTION 1 (recommended) is to use the HARDWARE SPI pins, which are unique
// to each board and not reassignable. For Arduino Uno: MOSI = pin 11 and
// SCLK = pin 13. This is the fastest mode of operation and is required if
// using the breakout board's microSD card.
// For 1.44" and 1.8" TFT with ST7735 (including HalloWing) use:
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
#include "DHT.h"
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define TRIG_PIN A0
#define ECHO_PIN A1
#include <IRremote.h>
#define RECV_PIN 5
IRrecv irrecv(RECV_PIN);
decode_results results;
#include <NewTone.h>
#define TONE_PIN 8
#define BUTTON_PIN 2
#include <Wire.h>
#include "Adafruit_MPR121.h"
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
Adafruit_MPR121 cap = Adafruit_MPR121();
#include "RTClib.h"
RTC_DS1307 rtc;
/*
#include <SD.h>
File myFile;
*/
void setup() {
//Ultrasonic
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
irrecv.enableIRIn(); // Start the receiver
Serial.begin(9600);
tft.initR(INITR_BLACKTAB);
tft.setRotation(3);
tft.setCursor(0, 0);
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(2);
tft.println("Loading");
pinMode(BUTTON_PIN,INPUT_PULLUP);
while (!cap.begin(0x5A)) {
Serial.println("MPR121 not found, check wiring?");
delay(500);
}
while (! rtc.begin()) {
Serial.println("Couldn't find RTC");
delay(500);
}
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
if (! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
//rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
delay(1000);
tft.fillScreen(ST77XX_BLACK);
dht.begin();
}
short int mode=0;
void loop() {
// put your main code here, to run repeatedly:
switch(mode){
case 0:
showTime();break;
case 1:
timer();break;
case 2:
showDist();break;
case 3:
tempHumid();break;
case 4:
IRRead();break;
case 5:
keyboard();break;
//case 6:
// spamgame();
}
backgroundTimer();
if (press()){
mode+=1;tft.fillScreen(ST77XX_BLACK);tft.setCursor(0, 0);
if(mode==5)keyboardScreen();
}
if (mode>5)mode=0;
}
bool previous = HIGH; // the previous reading from the input pin
bool press(){
bool reading = digitalRead(BUTTON_PIN);
bool state = LOW; // the current state of the output pin
//Just Release
if (reading == HIGH && previous == LOW){// && millis() - time > debounce) {
state=HIGH;
}
//Serial.print(state);Serial.print(reading);Serial.println(previous);
previous=reading;
return state;
}
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
int touched(int pin){//Pin 0 to 11
// Get the currently touched pads
uint16_t currtouched = cap.touched();
if (currtouched & _BV(pin))return 1;
return 0;
}
String twoDigit(long val){
if (val/10==0){return "0"+String(val);}
return String(val);
}
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
bool stopwatchState=false;
long stopwatchStart = 0,stopwatchTimed=0;
void showTime() {
tft.setCursor(0, 0);
tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
tft.setTextSize(3);
DateTime now = rtc.now();
tft.print(twoDigit(now.hour()));
tft.print(':');
tft.print(twoDigit(now.minute()));
tft.print(':');
tft.println(twoDigit(now.second()));
tft.setTextSize(2);
tft.print(now.year(), DEC);
tft.print('/');
tft.print(now.month(), DEC);
tft.print('/');
tft.println(now.day(), DEC);
tft.print("(");
tft.print(daysOfTheWeek[now.dayOfTheWeek()]);
tft.println(") \n");
tft.println("Stopwatch:");
tft.setTextSize(1);tft.println("1:Start 2:Stop 3:Resume");tft.println();
tft.setTextSize(2);
if (touched(4)){
stopwatchStart = millis();stopwatchState=true;stopwatchTimed=0;
tft.print(" ");
}
if (touched(5)){stopwatchState=false;stopwatchTimed+=(millis()-stopwatchStart);}
if (touched(6)){stopwatchStart = millis();stopwatchState=true;}
long showTimed;
if (stopwatchState){showTimed=millis()-stopwatchStart+stopwatchTimed;}
else{showTimed=stopwatchTimed;}
tft.print(showTimed/1000);tft.print("s ");
tft.print(showTimed%1000);tft.println("ms ");
}
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 4978
int notes[]= { NOTE_B2,//1
NOTE_C3,NOTE_CS3,NOTE_D3,NOTE_DS3,NOTE_E3,NOTE_F3,NOTE_F3,NOTE_FS3,NOTE_G3,NOTE_GS3,NOTE_A3,NOTE_AS3,NOTE_B3,NOTE_C4,
NOTE_C4,NOTE_CS4,NOTE_D4,NOTE_DS4,NOTE_E4,NOTE_F4,NOTE_F4,NOTE_FS4,NOTE_G4,NOTE_GS4,NOTE_A4,NOTE_AS4,NOTE_B4,NOTE_C5,
NOTE_C5,NOTE_CS5,NOTE_D5,NOTE_DS5,NOTE_E5,NOTE_F5,NOTE_F5,NOTE_FS5,NOTE_G5,NOTE_GS5,NOTE_A5,NOTE_AS5,NOTE_B5,NOTE_C6
};
short int note = 15;
short int sharp = 0;
bool keyboardState[] = {0,0,0};//Up, Down, togglesharp toggle
void keyboardScreen(){
tft.setCursor(0, 0);
tft.setTextColor(ST77XX_GREEN, ST77XX_BLACK);tft.setTextSize(3);
tft.println("Keyboard");
tft.setTextColor(ST77XX_MAGENTA, ST77XX_BLACK); tft.setTextSize(2);
String notesList=" C3D E F G A B C4D E F G A B C5D E F G A B C6";
tft.println(notesList.substring(note,note+16)+" ");
if (sharp==1)tft.print("# ");
else if (sharp==-1)tft.print("b ");
else tft.print(" ");
}
void keyboard(){
/*uint16_t currtouched = cap.touched();
int noteDown=currtouched & _BV(2);
int noteUp=currtouched & _BV(3);
*/
int noteDown=touched(2);
int noteUp=touched(3);
if (touched(1)){sharp=1;keyboardScreen();}
else if (touched(0)){sharp=-1;keyboardScreen();}
else{
sharp=0;
tft.setCursor(0, 0);tft.setTextColor(ST77XX_MAGENTA, ST77XX_BLACK);
tft.setTextSize(3);tft.println("");tft.setTextSize(2);tft.println("\n\n ");
}
if (noteDown != keyboardState[0]){
keyboardState[0]=noteDown;
if (!noteDown){note -= 2;if(note<1){note=1;}keyboardScreen();}
}
if (noteUp != keyboardState[1]){
keyboardState[1]=noteUp;
if (!noteUp){note += 2;if (note>29){note=29;}keyboardScreen();}
}
int keys[]={touched(4),touched(5),touched(6),touched(7),
touched(8),touched(9),touched(10),touched(11)};
/*
int keys[]={currtouched & _BV(4),currtouched & _BV(5),currtouched & _BV(6),currtouched & _BV(7),
currtouched & _BV(8),currtouched & _BV(9),currtouched & _BV(10),currtouched & _BV(11)};*/
if (keys[0]){NewTone(TONE_PIN, notes[note+sharp]);}
else if (keys[1]){NewTone(TONE_PIN, notes[note+2+sharp]);}
else if (keys[2]){NewTone(TONE_PIN, notes[note+4+sharp]);}
else if (keys[3]){NewTone(TONE_PIN, notes[note+6+sharp]);}
else if (keys[4]){NewTone(TONE_PIN, notes[note+8+sharp]);}
else if (keys[5]){NewTone(TONE_PIN, notes[note+10+sharp]);}
else if (keys[6]){NewTone(TONE_PIN, notes[note+12+sharp]);}
else if (keys[7]){NewTone(TONE_PIN, notes[note+14+sharp]);}
if (!keys[0] and !keys[1] and !keys[2] and !keys[3] and
!keys[4] and !keys[5] and !keys[6] and !keys[7])
noNewTone(TONE_PIN);
}
bool timerState=false;
long timerStart=0,timerRemaining=0,showTimer;//in ms
short int score[4] = {0,0,0,0};
bool prevstate[4] = {0,0,0,0};
bool state[4] = {0,0,0,0};
void timer(){
tft.setCursor(0, 0);
tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
tft.setTextSize(3);
tft.println("Timer");
tft.setTextSize(1);
tft.println("-+:1min <>:10min 7_8:1s");
tft.println("1:Start/Resume\n 2:Pause/Stop 3:Clear");tft.println();
if (touched(10)){timerRemaining-=1000;}
if (touched(11)){timerRemaining+=1000;}
if (touched(0)){timerRemaining-=60000;}
if (touched(1)){timerRemaining+=60000;}
if (touched(2)){timerRemaining-=600000;}
if (touched(3)){timerRemaining+=600000;}
if (timerRemaining<0){timerRemaining=0;}
if (touched(4)){timerStart = millis();timerState=true;}
if (touched(5)){timerState=false;timerRemaining-=(millis()-timerStart);}
if (touched(6)){timerRemaining=0;}
tft.setTextSize(2);
if (timerState){showTimer=(timerRemaining+timerStart-millis());}
else{showTimer=timerRemaining;}//(toTime+timerStart);}
//tft.println(toTime);
tft.print(twoDigit(showTimer/60000));
tft.println(" min ");
tft.print(twoDigit(showTimer%60000/1000));
tft.println(" s ");
/*
if (touched(9)){
tft.setCursor(0, 0);
tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
tft.setTextSize(3);
tft.println("Spam");
tft.setTextSize(2);
tft.println("Pad 1, 4, 8 or the button");
tft.setTextSize(1);
tft.println("Press Pad 6 to exit");
while (true){
state[0] = touched(4) ;
if ((state[0] == 1) and (state[0] != prevstate[0])){score[0]+=1;}
if (state[0] != prevstate[0]){prevstate[0] = state[0];}
state[1] = touched(7);
if ((state[1] == 1) and (state[1] != prevstate[1])){score[1]+=1;}
if (state[1] != prevstate[1]){prevstate[1] = state[1];}
state[2] = touched(11);
if ((state[2] == 1) and (state[2] != prevstate[2])){score[2]+=1;}
if (state[2] != prevstate[2]){prevstate[2] = state[2];}
state[3] = press();
if ((state[3] == 1) and (state[3] != prevstate[3])){score[3]+=1;}
if (state[3] != prevstate[3]){prevstate[3] = state[3];}
if (touched(9)){break;}
}
}
/*
tft.setTextSize(1);
tft.println("Pad 4:"+String(score[1]));
tft.println("Pad 5:"+String(score[2]));
tft.println("Pad 6:"+String(score[3]));
tft.println("Button:"+String(score[4]));
*/
}
void backgroundTimer(){
if (showTimer<=0 and timerState==true){
timerState=false;
tft.fillScreen(ST77XX_RED);
tft.setCursor(0,0);
tft.setTextSize(4);
tft.setTextColor(ST77XX_GREEN, ST77XX_RED);
tft.println("Time's\nUp!");
tft.setTextSize(1);tft.println("Hold pad 2 to break\n");
/*
tft.println("Pad 4:"+String(score[1]));
tft.println("Pad 5:"+String(score[2]));
tft.println("Pad 6:"+String(score[3]));
tft.println("Button:"+String(score[4]));*/
//Beep
while (true){
NewTone(TONE_PIN, notes[note+14+sharp]);
delay(500);
noNewTone(TONE_PIN);
delay(500);
if (touched(5))break;
}
tft.fillScreen(ST77XX_BLACK);
short int score[3] = {0,0,0};
}
}
void tempHumid(){
tft.setCursor(0, 0);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
/*
if (isnan(h) || isnan(t) || isnan(f)) {
tft.println("Failed to read from DHT sensor!");
return;
}*/
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
tft.setTextColor(ST77XX_BLUE, ST77XX_BLACK);tft.setTextSize(2);
tft.println("Humidity: ");
tft.println(h);
tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK);tft.setTextSize(2);tft.println("Temperature: ");
tft.print(t);
tft.println(" *C ");
tft.setTextSize(1);tft.print(f);
tft.println(" *F");//\t");
tft.setTextColor(ST77XX_RED, ST77XX_BLACK);tft.setTextSize(2);tft.println("Heat index: ");
tft.print(hic);
tft.println(" *C ");
tft.setTextSize(1);tft.print(hif);
tft.println(" *F");
}
void showDist(){
tft.setCursor(0, 0);
tft.setTextColor(ST77XX_MAGENTA, ST77XX_BLACK);tft.setTextSize(2);
tft.println("Distance:");
tft.setTextColor(ST77XX_GREEN, ST77XX_BLACK);tft.setTextSize(3);
tft.print(sonicDist());tft.println("cm ");
}
int sonicDist(){
digitalWrite(TRIG_PIN, LOW);delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
unsigned long duration= pulseIn(ECHO_PIN, HIGH);
int distance= duration/29/2;
return distance;
}
short int slot;
unsigned int irSignals[8];
short int irProtocols[8];
IRsend irsend;
void IRRead() {
tft.setCursor(0, 0);
tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
tft.setTextSize(2);
tft.println("IR Sensor");
tft.println("Code");
tft.println();
/*if (touched(4)){slot=0;}
else if (touched(5)){slot=1;}
else if (touched(6)){slot=2;}
else if (touched(7)){slot=3;}
else if (touched(8)){slot=4;}
else if (touched(9)){slot=5;}
else if (touched(10)){slot=6;}
else if (touched(11)){slot=7;}
tft.println("Slot "+String(slot+1)+"/8 ");*/
tft.print("Code:");tft.print(irSignals[slot], HEX);tft.println(" ");
tft.print("Protocol:");tft.print(irProtocols[slot],DEC);tft.println(" ");
if (irrecv.decode(&results)) {
if (results.value != 0xFFFFFFFF){
Serial.println(results.value, HEX);
Serial.println(slot);
switch (results.decode_type){
case NEC:tft.println("NEC ");break;
case SONY:tft.println("SONY ");break;
case RC5:tft.println("RC5 ");break;
case RC6:tft.println("RC6 ");break;
case UNKNOWN:tft.println("UNKNOWN");break;
default: tft.println("Other ");break;
}
irSignals[slot]=results.value;
irProtocols[slot]=results.decode_type;
}
irrecv.resume(); // Receive the next value
}
//delay(100);
if (touched(1)){
tft.println("\nPlay ");
int khz=38;
irsend.sendRaw(irSignals[slot],20,khz);
}
else if (touched(0)){
irsend.sendSony(0xa8bca, 20);
}
else{
tft.println("\n+ to play");
irrecv.enableIRIn();
}
}
/*
void spamgame(){
tft.setCursor(0,0);
tft.setTextSize(2);
tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
tft.println("Multiplayer\n(4-Player)\nSpamGame");
tft.setTextSize(1);
tft.println("\nSpam buttons/pads\nfor high score\n\nPress + to start");
if (!touched(1))return;
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(0,0);
tft.setTextSize(2);
tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);//tft.setTextColor(ST77XX_GREEN, ST77XX_BLACK);
tft.println("Going to\nStart");
delay(2000);
tft.fillScreen(ST77XX_RED);
tft.setCursor(20,20);
tft.setTextSize(4);
tft.setTextColor(ST77XX_GREEN, ST77XX_RED);
tft.println("SPAM!");
short int score[4] = {0,0,0,0};
bool prevstate[4] = {0,0,0,0};
bool state[4] = {0,0,0,0};
long timing = 60000; //in ms
long startmillis = millis(); //start time
tft.setTextSize(2);tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
while ((millis() - startmillis) <= timing){
if (((millis() - startmillis)==45000) or
((millis() - startmillis)==30000) or
((millis() - startmillis)==15000) ){
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(0,0);
tft.setTextSize(2);
tft.println("Time Left");
tft.println((String((timing - (millis() - startmillis ))/1000))+"s");
tft.println();
tft.println("Scores: ");
tft.setTextSize(1);
tft.println("Button:"+String(score[0]));
tft.println("Player 1:"+String(score[1]));
tft.println("Player 2:"+String(score[2]));
tft.println("Player 3:"+String(score[3]));
}
state[0] = digitalRead(BUTTON_PIN) ;
if ((state[0] == 1) and (state[0] != prevstate[0])){score[0]+=1;}
if (state[0] != prevstate[0]){prevstate[0] = state[0];}
state[1] = touched(4);
if ((state[1] == 1) and (state[1] != prevstate[1])){score[1]+=1;}
if (state[1] != prevstate[1]){prevstate[1] = state[1];}
state[2] = touched(5);
if ((state[2] == 1) and (state[2] != prevstate[2])){score[2]+=1;}
if (state[2] != prevstate[2]){prevstate[2] = state[2];}
state[3] = touched(6);
if ((state[3] == 1) and (state[3] != prevstate[3])){score[3]+=1;}
if (state[3] != prevstate[3]){prevstate[3] = state[3];}
}
//Endgame
tft.fillScreen(ST77XX_RED);
tft.setCursor(0,0);
tft.setTextSize(4);
tft.setTextColor(ST77XX_GREEN, ST77XX_RED);
tft.println("Time's");
tft.println("Up!");
delay(5000);
//Score
tft.setTextColor(ST77XX_WHITE, ST77XX_BLACK);
tft.fillScreen(ST77XX_BLACK);
tft.setCursor(0,0);
tft.setTextSize(3);
tft.println("Final");
tft.println("Score:");
tft.setTextSize(2);
tft.println("Button:"+String(score[0]));
tft.println("Player 2:"+String(score[1]));
tft.println("Player 3:"+String(score[2]));
tft.println("Player 4:"+String(score[3]));
delay(5000);
tft.fillScreen(ST77XX_BLACK);
}*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment