Artificial Intelligence
Please fill the required field.

arduino code for seven segment display that works with RTC ds1302.


#include <DS1302.h>//include DS1302 library DS1302 rtc(5, 6, 7); //set DS1302 pins int digitPins[7] = {0, 1, 2, 3, 4, 5, 6}; //define digit pins on arduino int segments[7] = { A0, A1, A2, A3, A4, A5, A6}; //define segment pins on arduino void setup() { //initialize all digit pins as outputs for (int i = 0; i < 7; i++) { pinMode(digitPins[i], OUTPUT); } //initialize all segment pins as outputs for (int i = 0; i < 7; i++) { pinMode(segments[i], OUTPUT); } rtc.begin(); //initialize RTC } void loop() { //get current time from RTC int hour = rtc.getHour(h12); //get hour int minute = rtc.getMinute(); //get minute int second = rtc.getSecond(); //get second //split current hour into two digits int hourFirst = hour / 10; //get first digit of hour int hourSecond = hour % 10; //get second digit of hour //display first digit of hour digitalWrite(digitPins[0], HIGH); //turn on first digit pin displayDigit(hourFirst); //display first digit of hour delay(5); //delay of 5ms digitalWrite(digitPins[0], LOW); //turn off first digit pin //display second digit of hour digitalWrite(digitPins[1], HIGH); //turn on second digit pin displayDigit(hourSecond); //display second digit of hour delay(5); //delay of 5ms digitalWrite(digitPins[1], LOW); //turn off second digit pin //split current minute into two digits int minuteFirst = minute / 10; //get first digit of minute int minuteSecond = minute % 10; //get second digit of minute //display first digit of minute digitalWrite(digitPins[2], HIGH); //turn on third digit pin displayDigit(minuteFirst); //display first digit of minute delay(5); //delay of 5ms digitalWrite(digitPins[2], LOW); //turn off third digit pin //display second digit of minute digitalWrite(digitPins[3], HIGH); //turn on fourth digit pin displayDigit(minuteSecond); //display second digit of minute delay(5); //delay of 5ms digitalWrite(digitPins[3], LOW); //turn off fourth digit pin } //function to display digit on seven segment display void displayDigit(int digit) { //define segments to display digits from 0 to 9 on seven segment display byte numbers[10] = { B00111111, //0 B00000110, //1 B01011011, //2 B01001111, //3 B01100110, //4 B01101101, //5 B01111101, //6 B00000111, //7 B01111111, //8 B01101111 //9 }; //loop through all segments for (int i = 0; i < 7; i++) { //turn on segment based on number to be displayed if (bitRead(numbers[digit], i)) { digitalWrite(segments[i], HIGH); } //turn off all other segments else { digitalWrite(segments[i], LOW); } } }