Some new stuff
  1. Arduino Park Control
  2. Arduino USB2serial programming the bare bone
  3. Arduino Projects Videos added
  4. Arduino Xbee projects
  5. Arduino dB Meter 
  6. Trying to get the IOIO going. 
  7. IOIO is going....bluetooth
  8. Attiny 2313 bluetooth bee RC car
  9. Compass and temperature added
  10. AVR programmer
  11. USB2Serial and Ethernet POE
  12. Arduino LPG Sensor
  13. Program the Arduino Mini 05
  14. Lipo Rider Pro in action
  15. EZ Robot Builder kit arrived 10 Nov 2012
  16. Arduino VA Meter!
  17. 05 Nov 2012 Project Sentry Gun.
  18. Attiny85 PIR Sensor 10 Dec 2012 
  19. 0-30VDC3A and 2-28VDC10A bench PSU's
  20. 19 Dec 2012 Arduino Voltmeter LCD
  21. 29 Dec 2012 Arduino Tachometer&Speedometer LCD
  22. 03 Jan 2013 Arduino AC Phase Control. 
  23. 18 Jan 2013 Arduino Distance Meter
  24. 22 Jan 2013 Arduino Digital Clock and Date
  25. 31 Jan 2013 Arduino Digital Clock and Date 8x2 LCD
  26. Feb 2013 LCD and Backpack
  27. Feb 2013 7 Segment Serial Clock Sparkfun. Distance Sensor with "newping" library
  28. Feb 2013 Lelo Remote
  29. 01/03/2013 Arduino Adafruit 7 Seg Digital Clock with RTC and backpack 
  30. Arduino Frequency Meter 26/03/2013
  31. Another RC  Car controlled by Arduino 06/04/2013
  32. My quadcopter project update 25 April 2013
  33. My Aqua Quad Copter Flying 26 April 2013enlightened
  34. Another monster RC truck;strong one! 11July2013cool
  35. X-Frame Quad Copter 31 July 2013cool
  36. Arduino GSM Sheild added.28/8/2013
  37. 1.8inch TFT screen 13/09/2013cool
  38. 04/11/2014 added PID Soldering Iron Control
  39. Added bluetooth servo control 04/11/2014
  40. ESP8266 first try.16/03/2015
  41. Arduino Uno Quadcopter 19 May 2015
  42. IOT WemosD1 Amp Meter

 

 



Arduino LCD 7 Segment 16x2 Down Timer

I got this code from this website:

http://valentin.tablan.net/blog/anarduinotimer

But the code is modified by me to work properly on LCD. The existing code looks like very old code before my time cool

Video HERE

#include
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
#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

// voltage values for the buttons
const int  ADC_KEY_VOLTAGE[5] ={30, 150, 360, 535, 760 };
// how many butons we have
const int NUM_KEYS = 5;
// constants for the buttons
const int RIGHT = 0;
const int UP = 1;  
const int DOWN = 2;
const int LEFT = 3;
const int OK = 4;
// backlight pin
const int BKLIGHT_PIN = 10;
// speaker pin
const int TONE_PIN = 2;
// tune notes
const int TUNE_NOTES[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};
// tune note durations
const int TUNE_TIMES[] = {4, 8, 8, 4,4,4,4,4};
// tune length (no of notes)
const int TUNE_LENGTH = 8;
// delay before key repeating starts
const int KEY_REPEAT_DELAY = 1000;
// delay between individual key repeats
const int KEY_REPEAT_INTERVAL = 100;


int oldkey=-1;
// State is one of:
// 0: waiting for input
// 1: counting down
// 2: flashing screen
// 3: playing tone
// 4: end state: user is up
int state = 0;
// current brightness for the backlight
int brightness = 255;
// minutes value
int mins = 0;
// seconds value
int secs = 0;
// time alarm is due
unsigned long alarmTime = 0;
// time we started counting down
unsigned long startTime = 0;
// time we started playing the tune
unsigned long tuneStart = 0;
// last time we started dimming the screen
unsigned long dimmingStart = 0;
// last time a new key press event started
unsigned long keyPressStart = 0;
// is the user already up (we've been in state 4 before) 
boolean awake = false;

void setup() { 
  pinMode(BKLIGHT_PIN, OUTPUT); // pin for backlight control
  lcd.begin(16, 2);                   // set the lcd dimension
  lcd.clear();
  lcd.print("Enter time:");
  analogWrite(BKLIGHT_PIN, brightness);
}

void loop() {
  int key = getKey();
  
  if(state == 0) { // reading input
    if(key == UP){
      mins++;
      if(mins >= 100) mins = 99;
    } else if(key == DOWN){
      mins--;
      if(mins < 0) mins = 0;
    } else if(key == RIGHT) {
      secs += 10;
      if(secs >= 60) secs = 0;
    } else if(key == OK) {
      state = 1;
      startTime = 0;
    }
    // update display
    displayTime(mins, secs);
  } else if(state == 1) { // counting down
    unsigned long time = millis();
    if(startTime == 0) { // just started counting down
      startTime = time;
      // calculate the target time
      alarmTime = startTime+ 60000ul * mins + 1000ul * secs;
      lcd.clear();
      lcd.print("Remaining time:");
      // start dimming the lights
      dimmingStart = time;
    }
    if(alarmTime > time) {
      // we're still waiting
      // if any key is pressed, turn on the light
      if(key >= 0) {
        dimmingStart = time;  
      }
      if(time - dimmingStart < 5000) {
        // if it's the first 5 seconds of dimming: turn down the brightness
        brightness = (5000 - (time - startTime)) * 255 / 5000;
      } else {
        brightness = 0;
      }
      unsigned long remainingTime = alarmTime - time; 
      // update the time shown
      displayTime(remainingTime / 60000, (remainingTime % 60000) / 1000);
    } else {
      // we're done waiting
      state = 2;
    }
  } else if(state == 2) {  // blink for 10 seconds
    if(key >= 0) { // if any key pressed, stop
      state = 4;
    } else {
      unsigned long elapsedTime = millis() - alarmTime;
      if(elapsedTime < 10000) {
        int sign = (elapsedTime / 1000) %2;
        brightness = (elapsedTime % 1000) / 4;
        if(sign == 1) brightness = 255 - brightness; 
      } else {
        brightness = 255;
        state = 3;
      }
    }
  } else if(state == 3) { // play a tune
    if(key >= 0) { // if any key pressed, stop
      noTone(TONE_PIN);
      state = 4;
    } else {
      unsigned long time = millis();
      if(tuneStart == 0) tuneStart = time;
      long tuneTime = time - tuneStart;
      for(int i = 0; i < TUNE_LENGTH && tuneTime > 0; i++) {
        tuneTime -= (1000 / TUNE_TIMES[i]);        
        if(tuneTime <= 0) {
          // current note
          tone(TONE_PIN, TUNE_NOTES[i]);
        }
      }
      if(tuneTime > 0) {
        // song over;
        noTone(TONE_PIN);
        if(tuneTime > 1000) {
          // play again, after a second
          tuneStart = 0;
        }
      }
    }
  } else if(state == 4) { // end state
    if(!awake) {
      brightness = 255;
      lcd.clear();
      lcd.print(":)");
      lcd.setCursor(2, 0);
      lcd.print("You are awake!");
      awake = true;
    }
    if(key >= 0) {
      reset();
    }
  }
  analogWrite(BKLIGHT_PIN, brightness);
  delay(20);
}

void reset() {
  state = 0;
  brightness = 255;
  mins = 0;
  secs = 0;
  alarmTime = 0;
  startTime = 0;
  tuneStart = 0;
  dimmingStart = 0;
  awake = false;
  lcd.clear();
  lcd.print("Enter time:");
  analogWrite(10, brightness);
}

// Read a key (if any was pressed)
// Convert ADC value to key number
int getKey() {
  int k = decodeKey();
  if (k == -1) {
    // no key pressed
    keyPressStart = 0;
  } else {
    // possible new key press event: debounce and re-read
    delay(20);
    k = decodeKey();
    if (k == -1){
      // false start:  No valid key pressed
      keyPressStart = 0;
    } else {
      // key press confirmed
      if(keyPressStart == 0) {
        // it's a new event
        keyPressStart = millis();
        oldkey = k;
      } else {
        if(oldkey == k) {
          // long press of oldKey
          unsigned long duration = millis() - keyPressStart;
          if(duration >= KEY_REPEAT_DELAY) {
            // return a key, and schedule a new one after KEY_REPEAT_INTERVAL
            keyPressStart = keyPressStart + duration - KEY_REPEAT_DELAY - KEY_REPEAT_INTERVAL;
          } else {
            // we're waiting, so we report no key press
            k = -1;
          }
        } else {
          // new key now pressed
          keyPressStart = millis();
          oldkey = k;
        }
      }   
    }
  }
  return k;
}

// Reads a key input from the input pin
int decodeKey() {
  unsigned int input = analogRead(0);    // read the value from the sensor
  int k;
  for (k = 0; k < NUM_KEYS; k++) {
    if (input < ADC_KEY_VOLTAGE[k]) {
      //return k;
      break;
    }
  }
  if (k >= NUM_KEYS) {
    k = -1;     // No valid key pressed
  }
  return k;
}

void displayTime(int mins, int secs) {
  lcd.setCursor(2, 1);  //line=2, x=0
  char minMsg[3] = {'0' + (mins / 10), '0' + (mins % 10), 0};
  lcd.print(minMsg);
  lcd.print(":");
  char secMsg[3] = {'0' + (secs / 10), '0' + (secs % 10), 0};
  //String(secs).toCharArray(secMsg, 3);
  lcd.print(secMsg);
}

Back Back to top