Code Optimiztion

Hi guys I currently have a project for an automated high power led lighting system which has 4 channels controlled using pwm and an rtc clock.

My issue is that this is my first time using an arduino for such a major thing and rtc. I do have a basic code layout but i need to optimize it since it is using up 95% of the memory so i was wondering if someone could help me improve it.

#include <Wire.h>
#include "Sodaq_DS3231.h"
//declare times and corresponding led brightness
const char* ledTime[] = {"00:00", "00:15", "00:30", "00:45", "01:00", "01:15", "01:30", "01:45", "02:00", "02:15", "02:30", "02:45", "03:00", "03:15", "03:30", "03:45", "04:00", "04:15", "04:30", "04:45", "05:00", "05:15", "05:30", "05:45", "06:00", "06:15", "06:30", "06:45", "07:00", "07:15", "07:30", "07:45", "08:00", "08:15", "08:30", "08:45", "09:00", "09:15", "09:30", "09:45", "10:00", "10:15", "10:30", "10:45", "11:00", "11:15", "11:30", "11:45", "12:00", "12:15", "12:30", "12:45", "13:00", "13:15", "13:30", "13:45", "14:00", "14:15", "14:30", "14:45", "15:00", "15:15", "15:30", "15:45", "16:00", "16:15", "16:30", "16:45", "17:00", "17:15", "17:30", "17:45", "18:00", "18:15", "18:30", "18:45", "19:00", "19:15", "19:30", "19:45", "20:00", "20:15", "20:30", "20:45", "21:00", "21:15", "21:30", "21:45", "22:00", "22:15", "22:30", "22:45", "23:00", "23:15", "23:30", "23:45"};
int ch1[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,5,12,24,36,48,60,72,84,96,108,120,132,144,156,168,180,192,204,216,228,240,232,224,216,208,200,192,184,176,168,160,152,144,136,128,120,112,104,96,88,80,72,64,56,48,40,32,24,16,5,0,0,0,0,0,0,0,0,0,0};
int ch2[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,18,36,54,72,90,108,126,144,162,180,198,216,234,252,237,222,207,192,177,162,147,132,117,102,87,72,57,42,27,12,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int ch3[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,9,18,27,36,45,54,63,72,81,90,99,108,117,126,135,144,153,162,153,144,135,126,117,108,99,90,81,72,63,54,45,36,27,18,9,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int ch4[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,6,18,36,54,72,90,108,126,144,150,139,128,117,106,95,84,73,62,51,40,29,18,7,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int fan[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,25,50,75,100,125,150,175,200,255,255,200,175,150,125,100,75,50,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(3, OUTPUT);

  // Start rtc stuff
  Wire.begin();
  rtc.begin();

}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{

  DateTime now = rtc.now(); //get the current date-time
  
  int HR = now.hour();
  int MIN = now.minute();
  String timeNow= String(HR)+":"+String(MIN);
  
  for (int x=0; x<sizeof(ledTime); x=x+1){
    if (String(ledTime[x]) == timeNow){
       analogWrite(5, ch4[x]);
       analogWrite(6, ch3[x]);
       analogWrite(9, ch2[x]);
       analogWrite(10, ch1[x]);
       analogWrite(3, fan[x]);
       x = sizeof(ledTime);
    }
  }

}/* --(end main loop )-- */

I’m guessing that the memory usage is cause of the arrays, is there a better way to set it up.

Also in the loop I go through to check if the current timematches any in the array using a for loop, does the arduino ide have something like python where you can get an index from a value.

Where do the values come from originally? Was there some formula that computed them?

And ledTime isn't this just a incredibly wasteful way of saying we want something every 15 minutes? Or is it seconds?

-jim lee

No values are just from an excel spreadsheet just randomly and adjusted to how i needed them.

When I originally started using the time to find the indexed seemed simple and like a great idea, but looking back at it yeah it does seem terrible.
Switched up a bit got rid of the ledTime and instead used every 15 mins. mem usage down to 58 :slight_smile:

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{

  DateTime now = rtc.now(); //get the current date-time

  int HR = now.hour();

  //  String timeNow = String(HR) + ":" + String(MIN);

  if (now.minute() == 00) {
    int quarter = 0;
  }
  if  (now.minute() == 15) {
    int quarter = 1;
  }
  if  (now.minute() == 30) {
    int quarter = 2;
  }
  if  (now.minute() == 45) {
    int quarter = 3;
  }

  int index = (HR * 4) + quarter;
  analogWrite(5, ch4[index]);
  analogWrite(6, ch3[index]);
  analogWrite(9, ch2[index]);
  analogWrite(10, ch1[index]);
  analogWrite(3, fan[index]);


}/* --(end main loop )-- */

Its funny how just talking to someone about something can open ways you might never think about lol.
Anything else i can improve on.

Not a good coder, but can you change this

int ch1… // all values are 8-bit

to this

byte ch1… // byte datatype halves the used memory

Leo…

Note the number of leading zeros in each array, then clip them out of the arrays. Then when accessing an array value. For example:

for (int i=0;i<=maxi;i++) {

   if (i<ch1StartIndex) {
      use(0);
   } else {
      use(ch1[i-ch1StartIndex]);
   }
   ...
   ...

-jim lee

Just look it up howto place the big fixed arrays in prom NOT ram.

Mark

Thanks for all the tips guys, changed all the arrays from int to bytes, got rid of all the zeroes and called a function to write it using the start index as jimlee suggested.

I know my coding is terrible but practise makes perfect i guess.

Heres my final code for now

#include <Wire.h>
#include "Sodaq_DS3231.h"

byte ch1[] = {1, 5, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 168, 180, 192, 204, 216, 228, 240, 232, 224, 216, 208, 200, 192, 184, 176, 168, 160, 152, 144, 136, 128, 120, 112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 5};
byte ch2[] = {8, 18, 36, 54, 72, 90, 108, 126, 144, 162, 180, 198, 216, 234, 252, 237, 222, 207, 192, 177, 162, 147, 132, 117, 102, 87, 72, 57, 42, 27, 12, 5};
char ch3[] = {1, 9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108, 117, 126, 135, 144, 153, 162, 153, 144, 135, 126, 117, 108, 99, 90, 81, 72, 63, 54, 45, 36, 27, 18, 9, 3};
byte ch4[] = {6, 18, 36, 54, 72, 90, 108, 126, 144, 150, 139, 128, 117, 106, 95, 84, 73, 62, 51, 40, 29, 18, 7, 2};
//temp until temp sensor comes in
byte fan[] = {25, 50, 75, 100, 125, 150, 175, 200, 255, 255, 200, 175, 150, 125, 100, 75, 50, 25};
int quarter = 0, oldQuarter = 9;
void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(3, OUTPUT);

  // Start rtc stuff
  Wire.begin();
  rtc.begin();

}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{

  DateTime now = rtc.now(); //get the current date-time
  int HR = now.hour();

  if (now.minute() == 00) {
    quarter = 0;
  }
  if  (now.minute() == 15) {
    quarter = 1;
  }
  if  (now.minute() == 30) {
    quarter = 2;
  }
  if  (now.minute() == 45) {
    quarter = 3;
  }
  // only run if time has changed +15 mins
  if (oldQuarter != quarter) {
    int index = (HR * 4) + quarter;
    indexCalc(index);
    oldQuarter = quarter;
  }


}/* --(end main loop )-- */

void indexCalc(int x ) {
  int index = x, ch1Start = 35, ch2Start = 42, ch3Start = 39, ch4Start = 48, fanStart = 48;
  // CH1 write
  if (index < ch1Start || index > sizeof(ch1) + ch1Start) {
    analogWrite(5, 0);
  } else {
    analogWrite(5, ch1[index - ch1Start]);
  }
  // CH2 write
  if (index < ch2Start || index > sizeof(ch2) + ch2Start) {
    analogWrite(6, 0);
  } else {
    analogWrite(6, ch2[index - ch2Start]);
  }
  // CH3 write
  if (index < ch3Start || index > sizeof(ch3) + ch3Start) {
    analogWrite(9, 0);
  } else {
    analogWrite(9, ch3[index - ch3Start]);
  }
  // CH4 write
  if (index < ch4Start || index > sizeof(ch4) + ch4Start) {
    analogWrite(10, 0);
  } else {
    analogWrite(10, ch4[index - ch4Start]);
  }
  // Fan write
  if (index < fanStart || index > sizeof(fan) + fanStart) {
    analogWrite(3, 0);
  } else {
    analogWrite(3, fan[index - fanStart]);
  }
}