Go Down

Topic: Clock-Thermometer on Arduino, 4 digit 7 segment display, temperature sensor LM35 (Read 3946 times) previous topic - next topic

DarkGenius




Code: [Select]
#define DIGIT1 A2
#define DIGIT2 A3
#define DIGIT3 A4
#define DIGIT4 A5
#define SEGMENTA 2
#define SEGMENTB 3
#define SEGMENTC 4
#define SEGMENTD 5
#define SEGMENTE 6
#define SEGMENTF 7
#define SEGMENTG 8
#define COLON A1
#define DELAYTIME 50
#define BUTTONMIN 12
#define BUTTONHOUR 13
#include <DS1302.h>
unsigned short hours, minutes, seconds, temp;
unsigned long lastTime;
DS1302 rtc(9, 10, 11);
Time t;
int lastButtonStatemin = LOW;
int lastButtonStatehour = LOW;
boolean tempFlag;
int tempPin = A0;
int tempc;
void setup() {               
  pinMode(DIGIT1, OUTPUT);
  pinMode(DIGIT2, OUTPUT);
  pinMode(DIGIT3, OUTPUT);
  pinMode(DIGIT4, OUTPUT);
  pinMode(SEGMENTA, OUTPUT);
  pinMode(SEGMENTB, OUTPUT);
  pinMode(SEGMENTC, OUTPUT);
  pinMode(SEGMENTD, OUTPUT);
  pinMode(SEGMENTE, OUTPUT);
  pinMode(SEGMENTF, OUTPUT);
  pinMode(SEGMENTG, OUTPUT);
  pinMode(COLON, OUTPUT);
  pinMode(BUTTONMIN, INPUT);
  pinMode(BUTTONHOUR, INPUT);
  rtc.halt(false);
  rtc.writeProtect(false);
  analogReference(INTERNAL);
}
void loop() {
  t = rtc.getTime();
  minutes = t.min % 10;
  seconds = t.sec;
  int temp = analogRead(tempPin)/ 10.5;
  if (minutes == 0 && seconds >= 10 && seconds < 20){
    clock_all_off();
    clock_show_digit(0, 10);
    delayMicroseconds(150);
    unsigned short delaytime;
    unsigned short num_leds[10] = {
    6, 3, 5, 5, 4, 5, 6, 3, 7, 6           };
    unsigned short digit[4];
    if (tempFlag){
      tempFlag = false;
      tempc = temp;}
    digit[1] = tempc / 10;
    digit[2] = tempc % 10;
    if (temp > 10) {
      clock_all_off();
      clock_show_digit(1, digit[1]);
      delaytime = num_leds[digit[1]] * 50;   
      delayMicroseconds(delaytime);
    }
    clock_all_off();
    clock_show_digit(2, digit[2]);
    delaytime = num_leds[digit[2]] * 50;   
    delayMicroseconds(delaytime);
    clock_all_off();
    clock_show_digit(3, 11);
  }
  else{
    tempFlag = true;
    clock_show_time(t.hour, t.min);
    if (t.sec % 2 == 0) {
      clock_show_colon();
    }
    if (digitalRead(BUTTONMIN) != lastButtonStatemin) {
      lastButtonStatemin = digitalRead(BUTTONMIN);
      if (digitalRead(BUTTONMIN) == HIGH) {
        minutes = t.min;
        hours = t.hour;
        if (minutes == 59) {
          minutes = 0;
          rtc.setTime(hours, minutes , 0);
        }
        else {
          minutes++;
          rtc.setTime(hours, minutes , 0);
        }
      }
    }
    if (digitalRead(BUTTONHOUR) != lastButtonStatehour) {
      lastButtonStatehour = digitalRead(BUTTONHOUR);
      if (digitalRead(BUTTONHOUR) == HIGH) {
        minutes = t.min;
        hours = t.hour;
        if (hours == 23) {
          hours = 0;
          rtc.setTime(hours, minutes , 0);
        }
        else {
          hours++;
          rtc.setTime(hours, minutes , 0);
        }
      }
    }
  }
}
void clock_show_time(unsigned short hours, unsigned short minutes) {
  unsigned short i;
  unsigned short delaytime;
  unsigned short num_leds[10] = {
    6, 3, 5, 5, 4, 5, 6, 3, 7, 6           };
  unsigned short digit[4];
  unsigned short hide_leading_hours_digit;
  if (hours > 99) hours = 99;
  if (minutes > 59) minutes = 59;
  if (hours < 10 && hours >= 0) {
    hide_leading_hours_digit = 1;
  }
  else {
    hide_leading_hours_digit = 0;
  }
  digit[0] = hours / 10;
  digit[1] = hours % 10;
  digit[2] = minutes / 10;
  digit[3] = minutes % 10;
  for (i = hide_leading_hours_digit; i < 4; i++) {
    clock_all_off();
    clock_show_digit(i, digit[i]);
    delaytime = num_leds[digit[i]] * DELAYTIME;   
    delayMicroseconds(delaytime);
  }
  clock_all_off();
}
void clock_all_off(void) {
  digitalWrite(DIGIT1, LOW);
  digitalWrite(DIGIT2, LOW);
  digitalWrite(DIGIT3, LOW);
  digitalWrite(DIGIT4, LOW);
  digitalWrite(SEGMENTA, LOW);
  digitalWrite(SEGMENTB, LOW);
  digitalWrite(SEGMENTC, LOW);
  digitalWrite(SEGMENTD, LOW);
  digitalWrite(SEGMENTE, LOW);
  digitalWrite(SEGMENTF, LOW);
  digitalWrite(SEGMENTG, LOW);
  digitalWrite(COLON, LOW);
}
void clock_show_digit(unsigned short position, unsigned short value) {
  byte a;
  byte b;
  byte c;
  byte d;
  byte e;
  byte f;
  byte g;
  switch (position) {
  case 0:
    digitalWrite(DIGIT1, HIGH);
    break;
  case 1:
    digitalWrite(DIGIT2, HIGH);
    break;
  case 2:
    digitalWrite(DIGIT3, HIGH);
    break;
  case 3:
    digitalWrite(DIGIT4, HIGH);
    break;
  }
  a = !(value == 1 || value == 4 || value == 10);
  b = !(value == 5 || value == 6 || value == 10);
  c = !(value == 2 || value == 10 || value == 11);
  d = !(value == 1 || value == 4 || value == 7 || value == 11);
  e =  (value == 0 || value == 2 || value == 6 || value == 8 || value == 10);
  f = !(value == 1 || value == 2 || value == 3 || value == 7);
  g = !(value == 0 || value == 1 || value == 7);
  if (a) digitalWrite(SEGMENTA, HIGH);
  if (b) digitalWrite(SEGMENTB, HIGH);
  if (c) digitalWrite(SEGMENTC, HIGH);
  if (d) digitalWrite(SEGMENTD, HIGH);
  if (e) digitalWrite(SEGMENTE, HIGH);
  if (f) digitalWrite(SEGMENTF, HIGH);
  if (g) digitalWrite(SEGMENTG, HIGH);
}
void clock_show_colon(void) {
  unsigned short delaytime;
  digitalWrite(COLON, HIGH); 
  delaytime = DELAYTIME * 4;
  delayMicroseconds(delaytime);
  digitalWrite(COLON, LOW);
}

casemod

Nice project!

Did you build the LED matrix from scratch or is that something that can be purchased?
CircuitGuru
http://www.circuitguru.co.uk/

benjamin_acquah

hello guys please i verify the code but it says exit status 1 and error compiling what is wrong

benjamin_acquah

Hello please am using DS1307 RTC can you please help me to modify the code

ferro2020

The IC1 input and output gates have been replaced.
The IC2 gates are correct.
The correct connection of the power supply IC1 and IC2:
GND = Negative
CD + = positive.

The IC1 and IC2 power should be connected.

Go Up