Archive of Arduino Projects
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
3.8 KiB

//YWROBOT
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <TM1638.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
TM1638 module(3, 2, 4);
#define NO_MODULES 1
TM1638* modules[NO_MODULES] = {
&module
};
byte modes[NO_MODULES];
unsigned long trump_reset;
unsigned long startTime;
unsigned long study_reset;
const int buttondelay = 150; // millis delay for button bounceback
const int backlight_pin = 7;
const int rPin = 10; // RGB pins
const int gPin = 9;
const int bPin = 8;
int rval = 0;
int gval = 0;
int bval = 0;
bool backlight_status = 1;
int daysSinceLastReset = 0;
int studySessions = 0;
void update(TM1638* module, byte* mode) {
byte buttons = module->getButtons();
unsigned long runningSecs = (millis() - startTime) / 1000;
float studyMins = (millis() - study_reset) / (1000.0*60);
float trumpDays = (millis() - trump_reset) / (1000.0*60*60*24);
if(module->getButtons() == 128 ){
backlight_status = !backlight_status;
digitalWrite(backlight_pin, backlight_status);
delay(buttondelay);
// module->clearDisplay();
}
// button pressed - change mode
if (buttons != 0) {
*mode = buttons;
}
// STUDY TIMER ON LCD
lcd.setCursor(6,3);
lcd.print(int(studyMins));
lcd.setCursor(3,3);
lcd.print(studySessions);
lcd.setCursor(0,3);
lcd.print(daysSinceLastReset);
module->setLEDs(*mode);
switch (*mode) {
case 1 << 0: // STUDY TIMER SUMMARY
char s[8];
studySessions = int(studyMins/90);
daysSinceLastReset = studySessions%16;
sprintf(s, "%2d.%2d.%2d", daysSinceLastReset, studySessions, int(studyMins)%90 );
module->setDisplayToString(s);
break;
case 1 << 1:
module->setDisplayToDecNumber(10000*studyMins, 1 << 4, false);
break;
case 1 << 2:
module->setDisplayToDecNumber(1000000*trumpDays, 1 << 6, false);
break;
case 1 << 3:
module->setDisplayToDecNumber(runningSecs, 1 << 5, false);
break;
case 1 << 4: // Button 5
module->clearDisplay();
module->clearDisplayDigit((runningSecs - 1) % 8, 0);
module->setDisplayDigit(runningSecs % 8, runningSecs % 8, 0);
break;
case 1 << 5: // reset study timer
study_reset = millis();
*mode = 1 << 1;
delay(buttondelay);
module->clearDisplay();
break;
case 1 << 6: // reset trump timer
trump_reset = millis();
*mode = 1 << 2;
delay(buttondelay);
module->clearDisplay();
break;
case 1 << 7: // Button 8, reset backlight
module->clearDisplay();
break;
case 65:
module->setDisplayToError();
break;
}
}
void setup()
{
for (int i = 0; i < NO_MODULES; i++) {
modules[i]->setupDisplay(true, 7);
modes[i] = 0;
}
startTime = millis();
study_reset = millis();
trump_reset = millis();
lcd.init(); // initialize the lcd
pinMode(backlight_pin, OUTPUT);
digitalWrite(backlight_pin, backlight_status);
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(2,0);
lcd.print("ACTION EXPRESSES");
lcd.setCursor(6,1);
lcd.print("PRIORITY.");
lcd.setCursor(15,2);
lcd.print("ABK");
}
void loop()
{
for (int i = 0; i < NO_MODULES; i++) {
update(modules[i], &modes[i]);
}
// RGB LED
rval = max( rval + rand()%3 - 1, 0); // markov chain
gval = max( gval + rand()%3 - 1, 0);
bval = max( bval + rand()%3 - 1, 0);
rval = min(rval, 255);
gval = min(gval, 255);
bval = min(bval, 255);
lcd.setCursor(10,2);
lcd.print(rval);
digitalWrite(rPin, 0.6*rval);
lcd.setCursor(5,2);
lcd.print(gval);
digitalWrite(gPin, 0.3*gval);
lcd.setCursor(0,2);
digitalWrite(bPin, 0.1*bval);
lcd.print(bval);
}