Trying to make a simple Lixie clock and cant Figure out the code

I just added tags and updated the time to 60 seconds for anyone who doesn't understand the code or just wants it a little easier.

What does that mean?

Your code is over 350 lines long; it should be less than 100.

You appear to have missed this one and does it now compile ?

I tried what you guys said and fixed some of the problems with my code

const int m1D0 = 1;
const int m1D1 = 2;
const int m1D2 = 3;
const int m1D3 = 4;
const int m1D4 = 5;
const int m1D5 = 6;
const int m1D6 = 7;
const int m1D7 = 8;
const int m1D8 = 9;
const int m1D9 = 10;
const int m10D0 = 11;
const int m10D1 = 12;
const int m10D2 = 13;
const int m10D3 = 41;
const int m10D4 = 42;
const int m10D5 = 43;
const int m10D6 = 44;
const int m10D7 = 45;
const int m10D8 = 46;
const int m10D9 = 47;
const int h1D0 = 48;
const int h1D1 = 22;
const int h1D2 = 23;
const int h1D3 = 24;
const int h1D4 = 25;
const int h1D5 = 26;
const int h1D6 = 27;
const int h1D7 = 28;
const int h1D8 = 29;
const int h1D9 = 30;
const int h10D0 = 31;
const int h10D1 = 32;
const int h10D2 = 33;
const int h10D3 = 34;
const int h10D4 = 35;
const int h10D5 = 36;
const int h10D6 = 37;
const int h10D7 = 38;
const int h10D8 = 39;
const int h10D9 = 40;
int min1;
int min10;
int hour1;
int hour10;

void setup() {
pinMode(m1D0, OUTPUT);
pinMode(m1D1, OUTPUT);
pinMode(m1D2, OUTPUT);
pinMode(m1D3, OUTPUT);
pinMode(m1D4, OUTPUT);
pinMode(m1D5, OUTPUT);
pinMode(m1D6, OUTPUT);
pinMode(m1D7, OUTPUT);
pinMode(m1D8, OUTPUT);
pinMode(m1D9, OUTPUT);
pinMode(m10D0, OUTPUT);
pinMode(m10D1, OUTPUT);
pinMode(m10D2, OUTPUT);
pinMode(m10D3, OUTPUT);
pinMode(m10D4, OUTPUT);
pinMode(m10D5, OUTPUT);
pinMode(m10D6, OUTPUT);
pinMode(m10D7, OUTPUT);
pinMode(m10D8, OUTPUT);
pinMode(m10D9, OUTPUT);
pinMode(h1D0, OUTPUT);
pinMode(h1D1, OUTPUT);
pinMode(h1D2, OUTPUT);
pinMode(h1D3, OUTPUT);
pinMode(h1D4, OUTPUT);
pinMode(h1D5, OUTPUT);
pinMode(h1D6, OUTPUT);
pinMode(h1D7, OUTPUT);
pinMode(h1D8, OUTPUT);
pinMode(h1D9, OUTPUT);
pinMode(h10D0, OUTPUT);
pinMode(h10D1, OUTPUT);
pinMode(h10D2, OUTPUT);
pinMode(h10D3, OUTPUT);
pinMode(h10D4, OUTPUT);
pinMode(h10D5, OUTPUT);
pinMode(h10D6, OUTPUT);
pinMode(h10D7, OUTPUT);
pinMode(h10D8, OUTPUT);
pinMode(h10D9, OUTPUT);
hour10 = 1;
hour1 = 2;
min10 = 0;
min1 = 0;
}

void loop() {

if (min1>9) {
min1=0;
min10=min10+1;
} else {
}

if (min10>5) {
min10=0;
hour1=hour1 + 1;
} else {
}

if (hour1>9) {
hour10=1;
} else {
hour10=0;
}

if (hour1>12) {
hour10=0;
hour1=1;
} else {
}

if (min1 == 0) {
digitalWrite(m1D0, HIGH);
} else {
digitalWrite(m1D0, LOW);
}

if (min1 == 1) {
digitalWrite(m1D1, HIGH);
} else {
digitalWrite(m1D1, LOW);
}

if (min1 == 2) {
digitalWrite(m1D2, HIGH);
} else {
digitalWrite(m1D2, LOW);
}

if (min1 == 3) {
digitalWrite(m1D3, HIGH);
} else {
digitalWrite(m1D3, LOW);
}

if (min1 == 4) {
digitalWrite(m1D4, HIGH);
} else {
digitalWrite(m1D4, LOW);
}

if (min1 == 5) {
digitalWrite(m1D5, HIGH);
} else {
digitalWrite(m1D5, LOW);
}

if (min1 == 6) {
digitalWrite(m1D6, HIGH);
} else {
digitalWrite(m1D6, LOW);
}

if (min1 == 7) {
digitalWrite(m1D7, HIGH);
} else {
digitalWrite(m1D7, LOW);
}

if (min1 == 8) {
digitalWrite(m1D8, HIGH);
} else {
digitalWrite(m1D8, LOW);
}

if (min1 == 9) {
digitalWrite(m1D9, HIGH);
} else {
digitalWrite(m1D9, LOW);
}

if (min10 == 0) {
digitalWrite(m10D0, HIGH);
} else {
digitalWrite(m10D0, LOW);
}

if (min10 == 1) {
digitalWrite(m10D1, HIGH);
} else {
digitalWrite(m10D1, LOW);
}

if (min10 == 2) {
digitalWrite(m10D2, HIGH);
} else {
digitalWrite(m10D2, LOW);
}

if (min10 == 3) {
digitalWrite(m10D3, HIGH);
} else {
digitalWrite(m10D3, LOW);
}

if (min10 == 4) {
digitalWrite(m10D4, HIGH);
} else {
digitalWrite(m10D4, LOW);
}

if (min10 == 5) {
digitalWrite(m10D5, HIGH);
} else {
digitalWrite(m10D5, LOW);
}

if (min10 == 6) {
digitalWrite(m10D6, HIGH);
} else {
digitalWrite(m10D6, LOW);
}

if (min10 == 7) {
digitalWrite(m10D7, HIGH);
} else {
digitalWrite(m10D7, LOW);
}

if (min10 == 8) {
digitalWrite(m10D8, HIGH);
} else {
digitalWrite(m10D8, LOW);
}

if (min10 == 9) {
digitalWrite(m10D9, HIGH);
} else {
digitalWrite(m10D9, LOW);
}

if (hour1 == 0) {
digitalWrite(h10D0, HIGH);
} else {
digitalWrite(h1D0, LOW);
}

if (hour1 == 1) {
digitalWrite(h1D1, HIGH);
} else {
digitalWrite(h1D1, LOW);
}

if (hour1 == 2) {
digitalWrite(h1D2, HIGH);
} else {
digitalWrite(h1D2, LOW);
}

if (hour1 == 3) {
digitalWrite(h1D3, HIGH);
} else {
digitalWrite(h1D3, LOW);
}

if (hour1 == 4) {
digitalWrite(h1D4, HIGH);
} else {
digitalWrite(h1D4, LOW);
}

if (hour1 == 5) {
digitalWrite(h1D5, HIGH);
} else {
digitalWrite(h1D5, LOW);
}

if (hour1 == 6) {
digitalWrite(h1D6, HIGH);
} else {
digitalWrite(h1D6, LOW);
}

if (hour1 == 7) {
digitalWrite(h1D7, HIGH);
} else {
digitalWrite(h1D7, LOW);
}

if (hour1 == 8) {
digitalWrite(h1D8, HIGH);
} else {
digitalWrite(h1D8, LOW);
}

if (hour1 == 9) {
digitalWrite(h1D9, HIGH);
} else {
digitalWrite(h1D9, LOW);
}

if (hour10 == 0) {
digitalWrite(h10D0, HIGH);
} else {
digitalWrite(h10D0, LOW);
}

if (hour10 == 1) {
digitalWrite(h10D1, HIGH);
} else {
digitalWrite(h10D1, LOW);
}

if (hour10 == 2) {
digitalWrite(h10D2, HIGH);
} else {
digitalWrite(h10D2, LOW);
}

if (hour10 == 3) {
digitalWrite(h10D3, HIGH);
} else {
digitalWrite(h10D3, LOW);
}

if (hour10 == 4) {
digitalWrite(h10D4, HIGH);
} else {
digitalWrite(h10D4, LOW);
}

if (hour10 == 5) {
digitalWrite(h10D5, HIGH);
} else {
digitalWrite(h10D5, LOW);
}

if (hour10 == 6) {
digitalWrite(h10D6, HIGH);
} else {
digitalWrite(h10D6, LOW);
}

if (hour10 == 7) {
digitalWrite(h10D7, HIGH);
} else {
digitalWrite(h10D7, LOW);
}

if (hour10 == 8) {
digitalWrite(h10D8, HIGH);
} else {
digitalWrite(h10D8, LOW);
}

if (hour10 == 9) {
digitalWrite(h10D9, HIGH);
} else {
digitalWrite(h10D9, LOW);
}

delay(1000);
min1 = min1 + 1;

}

but now the digits 1, 7 and 8 only turn on for a millisecond and I have no idea why. I even tried changing the pins and still the same result.

You are probably making copy/paste errors. What about code tags </> ?

const int N_DIGITS = 4;
const int N_SEGMENTS = 10;

const byte digitPins [N_DIGITS][N_SEGMENTS] 
{{ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10},
 {11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
 {21, 22, 23, 24, 25, 26, 27, 28, 29, 30},
 {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}};

int digits [N_DIGITS] {1, 2, 0, 0};

void setup() 
{
  for (auto& row : digitPins) 
    for (auto& col: row)
      pinMode(col, OUTPUT);
}

void loop() 
{
  for (int digit = 0; digit < N_DIGITS; digit++) {
    int val = digits [digit];
    for (int pin = 0; pin < N_SEGMENTS; pin++) {
      digitalWrite (digitPins [digit][pin], val == pin ? HIGH : LOW);
    }
  }  
}

(uncompiled, untested)
Now, all you need to do is add in the logic to put the correct values into the digits array.
Like I said, fewer than 100 lines, total.

. . . and match the array digitPins to your actual pin allocations.
but, nonetheless, impressively compact.

Here's some code to fill in the time:

const unsigned long SECOND = 1000ul;
const unsigned long MINUTE = SECOND * 60;
const unsigned long HOUR   = MINUTE * 60;

void loop()
{
  unsigned minutes = (millis() / MINUTE) + 12 * 60;
  unsigned hours = minutes / 60;
  minutes = minutes % 60;

  digits[3] = minutes % 10;
  digits[2] = (minutes / 10) % 10;
  digits[1] = hours % 10;
  digits[0] = (hours / 10) % 10;

Yeah, oops.
Still, easier to reorder the contents of "digits" than rewrite tens of lines of code.

I should really have defined constants for hours-tens, hours-units etc.

An exercise for the reader.

I somehow knew that the thread would reach this level where it would be misused as an opportunity to showcase the finest code samples, irrespective of the learning or actual needs of the OP.
I can't resist either.

Arduino time library integration based on the code from @johnwasser

#include <TimeLib.h>
. . .
. . .

void setup() { 
. . .
  setTime(23, 59, 45, 16, 5, 2021);  // HH MM SS DD MM YYYY
. . .
}


void loop() {

  unsigned hours = hour() == 0 ?  12 : hour() > 12 ? hour() - 12 : hour() ;   // 12 hour clock
  unsigned minutes = minute()
  
  digits[3] = minutes % 10;
  digits[2] = (minutes / 10) % 10;
  digits[1] = hours % 10;
  digits[0] = (hours / 10) % 10;
. . .
. . .
}

corrected thanks to @anon73444976

I have to disagree - if I were to take the 360-odd lines of code, make a few tweaks and re-present it as a solution, the OP would be looking for a needle in a haystack.
Presenting a compact, workable framework instead, allows the user to explore and ask questions if necessary.

Also, you have hours as a variable and function

Also, the TimeLib functions are "hour()" and "minute()"

@ Masterpatches01

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

I'm pretty new to arduino and I only really know how to use the if statement and integers. So I have no idea how to use the byte. If someone could help me understand it better that would be such a great help.

'byte' (also known as uint8_t or 'unsigned char') is an unsigned, 8-bit integer. It's good for holding integer values between 0 and 255.

Hi,

Integers are your primary data-type for number storage.

On the Arduino Uno (and other ATmega based boards) an int stores a 16-bit (2-byte) value.

This yields a range of -32,768 to 32,767 (minimum value of -2^15 and a maximum value of (2^15) - 1).

On the Arduino Due and SAMD based boards (like MKR1000 and Zero), an int stores a 32-bit (4-byte) value.

This yields a range of -2,147,483,648 to 2,147,483,647 (minimum value of -2^31 and a maximum value of (2^31) - 1).

Tom... :grinning: :+1: :coffee: :australia:

sp. " "int"s are your primary data-type for number storage"

Remember, char, byte, int and long are all integer data-types.

Is this the current state of your project ? That is , it partly works but some digits are misbehaving.
If so, my advice is that you get it working using your existing code base and consider all the code samples that you have been, in the mean time, showered with, as future optimisation measures.

If you still need help, post the current code you are using, between code tags, and explain the symptoms.

I actually just fixed all the bugs and its finally working. thank you all for your help and if you guys want to make this then here is the code.

int min1 = 0;
int min10 = 0;
int hour1 = 0;
int hour10 = 0;

void setup()
{
pinMode(1, OUTPUT); // first minute digits 0
pinMode(2, OUTPUT); // 1
pinMode(3, OUTPUT); // 2
pinMode(4, OUTPUT); // 3
pinMode(5, OUTPUT); // 4
pinMode(6, OUTPUT); // 5
pinMode(7, OUTPUT); // 6
pinMode(8, OUTPUT); // 7
pinMode(9, OUTPUT); // 8
pinMode(10, OUTPUT); // 9
pinMode(11, OUTPUT); // second minute digits 0
pinMode(12, OUTPUT); // 1
pinMode(13, OUTPUT); // 2
pinMode(14, OUTPUT); // 3
pinMode(15, OUTPUT); // 4
pinMode(16, OUTPUT); // 5
pinMode(17, OUTPUT); // first hour digits 0
pinMode(18, OUTPUT); // 1
pinMode(19, OUTPUT); // 2
pinMode(20, OUTPUT); // 3
pinMode(21, OUTPUT); // 4
pinMode(22, OUTPUT); // 5
pinMode(23, OUTPUT); // 6
pinMode(24, OUTPUT); // 7
pinMode(25, OUTPUT); // 8
pinMode(26, OUTPUT); // 9
pinMode(27, OUTPUT); // second hour digits 0
pinMode(28, OUTPUT); // 1
hour10 = 1;
hour1 = 2;
min10 = 0;
min1 = 0;
}

void loop()
{

if (min1>9) {
min1 = 0;
min10 = min10 + 1;
}
if (min10>5) {
min10 = 0;
hour1 = hour1 + 1;
}
if (hour1 > 9) {
hour10 = 1;
} else {
hour10 = 0;
}
if (hour1 > 12) {
hour10 = 0;
hour1 = 1;
}

if (min1 == 0) {
digitalWrite(1, HIGH);
} else {
digitalWrite(1, LOW);
}
if (min1 == 1) {
digitalWrite(2, HIGH);
} else {
digitalWrite(2, LOW);
}
if (min1 == 2) {
digitalWrite(3, HIGH);
} else {
digitalWrite(3, LOW);
}
if (min1 == 3) {
digitalWrite(4, HIGH);
} else {
digitalWrite(4, LOW);
}
if (min1 == 4) {
digitalWrite(5, HIGH);
} else {
digitalWrite(5, LOW);
}
if (min1 == 5) {
digitalWrite(6, HIGH);
} else {
digitalWrite(6, LOW);
}
if (min1 == 6) {
digitalWrite(7, HIGH);
} else {
digitalWrite(7, LOW);
}
if (min1 == 7) {
digitalWrite(8, HIGH);
} else {
digitalWrite(8, LOW);
}
if (min1 == 8) {
digitalWrite(9, HIGH);
} else {
digitalWrite(9, LOW);
}
if (min1 == 9) {
digitalWrite(10, HIGH);
} else {
digitalWrite(10, LOW);
}
if (min10 == 0) {
digitalWrite(11, HIGH);
} else {
digitalWrite(11, LOW);
}
if (min10 == 1) {
digitalWrite(12, HIGH);
} else {
digitalWrite(12, LOW);
}
if (min10 == 2) {
digitalWrite(13, HIGH);
} else {
digitalWrite(13, LOW);
}
if (min10 == 3) {
digitalWrite(14, HIGH);
} else {
digitalWrite(14, LOW);
}
if (min10 == 4) {
digitalWrite(15, HIGH);
} else {
digitalWrite(15, LOW);
}
if (min10 == 5) {
digitalWrite(16, HIGH);
} else {
digitalWrite(16, LOW);
}
if (hour1 == 0) {
digitalWrite(17, HIGH);
} else {
digitalWrite(17, LOW);
}
if (hour1 == 1) {
digitalWrite(18, HIGH);
} else {
digitalWrite(18, LOW);
}
if (hour1 == 2) {
digitalWrite(19, HIGH);
} else {
digitalWrite(19, LOW);
}
if (hour1 == 3) {
digitalWrite(20, HIGH);
} else {
digitalWrite(20, LOW);
}
if (hour1 == 4) {
digitalWrite(21, HIGH);
} else {
digitalWrite(21, LOW);
}
if (hour1 == 5) {
digitalWrite(22, HIGH);
} else {
digitalWrite(22, LOW);
}
if (hour1 == 6) {
digitalWrite(23, HIGH);
} else {
digitalWrite(23, LOW);
}
if (hour1 == 7) {
digitalWrite(24, HIGH);
} else {
digitalWrite(24, LOW);
}
if (hour1 == 8) {
digitalWrite(25, HIGH);
} else {
digitalWrite(25, LOW);
}
if (hour1 == 9) {
digitalWrite(26, HIGH);
} else {
digitalWrite(26, LOW);
}
if (hour1 == 10) {
digitalWrite(17, HIGH);
} else {
digitalWrite(17, LOW);
}
if (hour1 == 11) {
digitalWrite(18, HIGH);
} else {
digitalWrite(18, LOW);
}
if (hour1 == 12) {
digitalWrite(19, HIGH);
} else {
digitalWrite(19, LOW);
}
if (hour10 == 0) {
digitalWrite(27, HIGH);
} else {
digitalWrite(27, LOW);
}
if (hour10 == 1) {
digitalWrite(28, HIGH);
} else {
digitalWrite(28, LOW);
}

delay(1000);
min1 = min1 + 1;
}

there is one "bug" left:

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan