Code to big. Noob At work!

Hello,
is started my first Arduino project.
In short a loading bar Clock or Linear clock.

I work in after school care and whantet a Clock wich is just a line getting longer until it is full and the day is over.
So it runs from 8 to 16 o´clock
Every 5 minutes a led turn on slowly filling the line

I rebuild this Projekt : Word clock with Arduino and Neopixel - Techniccontroller
and wanted to change the code so it works how i want it.

Components:
NeoPixel-Strip
AZDelivery Nano V3.0 with Atmega328 CH340 compatible with Arduino
AZDelivery Real Time Clock RTC DS3231 I2C Real Time Clock Compatible with Arduino

#include "RTClib.h"             
#include "DCF77.h"             
#include <Time.h>               
#include <TimeLib.h>
#include <Adafruit_GFX.h>       
#include <Adafruit_NeoMatrix.h> 
#include <Adafruit_NeoPixel.h>  
#include <EEPROM.h>

I deletet the rest as it was to big and the folloing code is the thing this post is about 
(sourcecode(?) : think_wordclock/wordclock_german.ino at main · techniccontroller/think_wordclock · GitHub )


       }
  else if((hours >= 15 && hours < 16) and (minutes >= 40 && minutes < 50))
  {
      //1540
      Serial.print("1540 ");
gridAddPixel(1,0);
gridAddPixel(2,0);
gridAddPixel(3,0);
gridAddPixel(4,0);
gridAddPixel(5,0);
gridAddPixel(6,0);
gridAddPixel(7,0);
gridAddPixel(8,0);
gridAddPixel(9,0);
gridAddPixel(10,0);
gridAddPixel(11,0);
gridAddPixel(12,0);
gridAddPixel(13,0);
gridAddPixel(14,0);
gridAddPixel(15,0);
gridAddPixel(16,0);
gridAddPixel(17,0);
gridAddPixel(18,0);
gridAddPixel(19,0);
gridAddPixel(20,0);
gridAddPixel(21,0);
gridAddPixel(22,0);
gridAddPixel(23,0);
gridAddPixel(24,0);
gridAddPixel(25,0);
gridAddPixel(26,0);
gridAddPixel(27,0);
gridAddPixel(28,0);
gridAddPixel(29,0);
gridAddPixel(30,0);
gridAddPixel(31,0);
gridAddPixel(32,0);
gridAddPixel(33,0);
gridAddPixel(34,0);
gridAddPixel(35,0);
gridAddPixel(36,0);
gridAddPixel(37,0);
gridAddPixel(38,0);
gridAddPixel(39,0);
gridAddPixel(40,0);
gridAddPixel(41,0);
gridAddPixel(42,0);
gridAddPixel(43,0);
gridAddPixel(44,0);
gridAddPixel(45,0);
gridAddPixel(46,0);
gridAddPixel(47,0);



       }
  else if((hours >= 15 && hours < 16) and (minutes >= 50 && minutes < 60))
  {
      //1550
      Serial.print("1550 ");
gridAddPixel(1,0);
gridAddPixel(2,0);
gridAddPixel(3,0);
gridAddPixel(4,0);
gridAddPixel(5,0);
gridAddPixel(6,0);
gridAddPixel(7,0);
gridAddPixel(8,0);
gridAddPixel(9,0);
gridAddPixel(10,0);
gridAddPixel(11,0);
gridAddPixel(12,0);
gridAddPixel(13,0);
gridAddPixel(14,0);
gridAddPixel(15,0);
gridAddPixel(16,0);
gridAddPixel(17,0);
gridAddPixel(18,0);
gridAddPixel(19,0);
gridAddPixel(20,0);
gridAddPixel(21,0);
gridAddPixel(22,0);
gridAddPixel(23,0);
gridAddPixel(24,0);
gridAddPixel(25,0);
gridAddPixel(26,0);
gridAddPixel(27,0);
gridAddPixel(28,0);
gridAddPixel(29,0);
gridAddPixel(30,0);
gridAddPixel(31,0);
gridAddPixel(32,0);
gridAddPixel(33,0);
gridAddPixel(34,0);
gridAddPixel(35,0);
gridAddPixel(36,0);
gridAddPixel(37,0);
gridAddPixel(38,0);
gridAddPixel(39,0);
gridAddPixel(40,0);
gridAddPixel(41,0);
gridAddPixel(42,0);
gridAddPixel(43,0);
gridAddPixel(44,0);
gridAddPixel(45,0);
gridAddPixel(46,0);
gridAddPixel(47,0);
gridAddPixel(48,0);

}
  else if((hours >= 16 && hours < 17) and (minutes >= 00 && minutes < 10))
  {
      //1600
      Serial.print("1600 ");
      gridAddPixel(1,0);
gridAddPixel(2,0);
gridAddPixel(3,0);
gridAddPixel(4,0);
gridAddPixel(5,0);
gridAddPixel(6,0);
gridAddPixel(7,0);
gridAddPixel(8,0);
gridAddPixel(9,0);
gridAddPixel(10,0);
gridAddPixel(11,0);
gridAddPixel(12,0);
gridAddPixel(13,0);
gridAddPixel(14,0);
gridAddPixel(15,0);
gridAddPixel(16,0);
gridAddPixel(17,0);
gridAddPixel(18,0);
gridAddPixel(19,0);
gridAddPixel(20,0);
gridAddPixel(21,0);
gridAddPixel(22,0);
gridAddPixel(23,0);
gridAddPixel(24,0);
gridAddPixel(25,0);
gridAddPixel(26,0);
gridAddPixel(27,0);
gridAddPixel(28,0);
gridAddPixel(29,0);
gridAddPixel(30,0);
gridAddPixel(31,0);
gridAddPixel(32,0);
gridAddPixel(33,0);
gridAddPixel(34,0);
gridAddPixel(35,0);
gridAddPixel(36,0);
gridAddPixel(37,0);
gridAddPixel(38,0);
gridAddPixel(39,0);
gridAddPixel(40,0);
gridAddPixel(41,0);
gridAddPixel(42,0);
gridAddPixel(43,0);
gridAddPixel(44,0);
gridAddPixel(45,0);
gridAddPixel(46,0);
gridAddPixel(47,0);
gridAddPixel(48,0);
gridAddPixel(49,0);
  }
  
  Serial.println();
}

Yea it works! but it is way to big! especially if i wanted to make 5 min increments.

Easy solution would be to just buy a bigger Arduino i guess, but I want to learn Programming.

My idear is like a command that says "turn led 1 to 5 on" would reduce it by a lot but,
maybe a thing were it says "every 5 minutes turn on the next led" would be better.
Thing is i don't even know how to search for these things ^^°

So if you have a recommendation for me that would be great
Thank you in Advance!!!
Aaron

gridAddPixel(1,0);
gridAddPixel(2,0);
gridAddPixel(3,0);
gridAddPixel(4,0);
gridAddPixel(5,0);
gridAddPixel(6,0);
gridAddPixel(7,0);
gridAddPixel(8,0);
gridAddPixel(9,0);
gridAddPixel(10,0);
gridAddPixel(11,0);
gridAddPixel(12,0);
gridAddPixel(13,0);
gridAddPixel(14,0);
gridAddPixel(15,0);
gridAddPixel(16,0);
gridAddPixel(17,0);
gridAddPixel(18,0);
gridAddPixel(19,0);
gridAddPixel(20,0);
gridAddPixel(21,0);
gridAddPixel(22,0);
gridAddPixel(23,0);
gridAddPixel(24,0);
gridAddPixel(25,0);
gridAddPixel(26,0);
gridAddPixel(27,0);
gridAddPixel(28,0);
gridAddPixel(29,0);
gridAddPixel(30,0);
gridAddPixel(31,0);
gridAddPixel(32,0);
gridAddPixel(33,0);
gridAddPixel(34,0);
gridAddPixel(35,0);
gridAddPixel(36,0);
gridAddPixel(37,0);
gridAddPixel(38,0);
gridAddPixel(39,0);
gridAddPixel(40,0);
gridAddPixel(41,0);
gridAddPixel(42,0);
gridAddPixel(43,0);
gridAddPixel(44,0);
gridAddPixel(45,0);
gridAddPixel(46,0);
gridAddPixel(47,0);
for (int i = 1; i < 48; i++) {
  gridAddPixel(i,0);
}

Another thing to consider is calculating the time of day in minutes. From that you can compute how many pixels should be lit and use the aforementioned for loop to set them.

so you have an RTC (which one) and you want to turn on LEDs based on the time of the day

do a bit of maths: multiply the hour by 60, add the number of minutes and you have something that is growing from 0 (at 0h00) to 1439 (at 23h59).

@8:00 is 480
@16:00 is 960

Lets call this result currentTime

so if currentTime is <= 480 no led on (nbLedsOn = 0)
else if currentTime is >= 960 all leds are on (nbLedsOn = NumberOfLeds)

in between it's a simple map() function

nbLedsOn = map(currentTime, 480,960,0,NumberOfLeds);

then depending on the pixels type you turn on nbLedsOn in your strip (and to avoid too many calls, do that only if nbLedsOn has changed)

--> probably less than 10 lines of code. Would that be good enough?

if you have a DS3231 you can set an alarm every 5 minutes (or whatever makes sense depending on the number of LEDs) that will call your function where you update the number of LEDs and the display

Hi thank you for your response!!

My rtc is a : AZDelivery Real Time Clock RTC DS3231
and i have a NeoPixel strip

maybe try something like this

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <RTClib.h>
RTC_DS3231 rtc;

#include <Adafruit_NeoPixel.h>
const byte pixelPin = 6;
const byte numberOfPixels = 96;
const unsigned int dayStart = 60u * 8 + 0;  // 8:00
const unsigned int dayEnd = 60u * 16 + 0;   // 16:00

byte numberOfActivePixels = 0;

Adafruit_NeoPixel pixels(numberOfPixels, pixelPin, NEO_GRB + NEO_KHZ800);

void updateDisplay() {
  byte newCount = 0;
  DateTime now = rtc.now();
  unsigned int currentTime = 60u * now.hour() + now.minute();
  if (currentTime <= dayStart) newCount = 0;
  else if (currentTime >= dayEnd) newCount = numberOfPixels;
  else newCount = map(currentTime, dayStart, dayEnd, 0, numberOfPixels);

  // has the count changed ?
  if (newCount != numberOfActivePixels) {
    numberOfActivePixels = newCount;
    for (byte p = 0; p < numberOfActivePixels; p++) pixels.setPixelColor(p, pixels.Color(0, 150, 0)); // bright green
    for (byte p = numberOfActivePixels; p < numberOfPixels; p++) pixels.setPixelColor(p, pixels.Color(0, 0, 0)); // black
    pixels.show();   // Send the updated pixel colors to the hardware.
  }
}

void setup() {
  Serial.begin(115200);
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC. Stop");
    while (true) yield();
  }
  // if clock is not right, sets the RTC to the date & time this sketch was compiled
  // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  pixels.begin();
  pixels.clear();
}

void loop() {
  updateDisplay();
  delay(5000); // go check every 5s
}

(typed here, I've no idea if it works :)) )

You need to adjust these 2 constants

const byte pixelPin = 6;
const byte numberOfPixels = 96;

also adapt to your specific strip Adafruit_NeoPixel pixels(numberOfPixels, pixelPin, NEO_GRB + NEO_KHZ800);

It worked!! YAY

it takes a long time too trouble shoot as nothing changes for 5 minutes XDD

sometimes some Radom leds are turned on some times nothing.
I think that is my wiring not the code.
My main problem with Troubleshooting is that im am not able to repeatably update my rtc so i will wait till tomorrow when the actual time is. As changing my computer time doesn't quiet seem to work.

I was gonna build a frame for the Clock and Upload the 3d printing files for it.
@J-M-L
If you allow it I will link your account when i uploade to www.prusaprinters.org (non commercial)
as you helped me out big time.

Thank you again,
I will update when I got it running ~

cool :slight_smile:

sometimes some Radom leds are turned on some times nothing

that's usually due to power. make sure you have what's required for your led strip

if you want to make it run faster for testing, just change the start (make it like 1 minute later than the moment you test) and end hours, may be like 5 minutes after the start. then all will go much faster :slight_smile:

Yea it was Power! the powerful is big enough but i gave it power from just one Direktion. now it gets power from both and it works fine. Thank you again.

I have but one question. is there a command were it shuts down the LED´s after the time is up? for power consumption sake?

Are you ok with me uploading the code to the www.prusaprinters.org (non commercial) website and crediting you for the code ?

Thanks again you made my day !!!

You can sure upload the code where it can be useful, sharing makes the world a better place.

To your question of powering off, it’s in that code

 if (currentTime <= dayStart) newCount = 0;
  else if (currentTime >= dayEnd) newCount = numberOfPixels;
  else newCount = map(currentTime, dayStart, dayEnd, 0, numberOfPixels);

what it says is if you are before the start of the day then no light is on, if you are after the end of the day all lights are on and in between it’s a linear function of time.

What you want to say is that after the end of the day all lights are off again, so it will be

 if (currentTime <= dayStart) newCount = 0;
  else if (currentTime >= dayEnd) newCount = 0;
  else newCount = map(currentTime, dayStart, dayEnd, 0, numberOfPixels);

(You might want to check against the end of day plus a few minutes later like 16h05 to actually see all lights on)

 if (currentTime <= dayStart) newCount = 0;
  else if (currentTime >= dayEnd+5) newCount = 0;
  else newCount = map(currentTime, dayStart, dayEnd, 0, numberOfPixels);
constrain(newCount, 0, numberOfPixels); // to ensure we don’t overflow

Works like a charm!
On and of at specified times.
Thank you so so much for all your help

( i'll update here once i finnische the design.)

1 Like

great!

Hi,
Be sure to include a circuit diagram and possibly a picture of your project.
Good to see you got it all working as you need it.

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

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.