Problem comparing unsigned long variables

I have a problem with, or don’t understand unsigned long variables. Or the type casting.
With testing a array lookup, I ran into a problem.

#include <Wire.h>  // Comes with Arduino IDE
#include <DS1302.h>

// Init the DS1302
DS1302 rtc(2, 3, 4);

Time t;

void setup() {
  // put your setup code here, to run once:
  // Set the clock to run-mode, and disable the write protection
  rtc.halt(false);
  rtc.writeProtect(false);
  Serial.begin(9600);

}

unsigned long channel1Data[][2] = { { 0      ,100 },  //time in seconds from midnight, light intensity
                       { 3600    ,50  },  //1
                       { 2*3600  ,10  },
                       { 3*3600  ,90  },
                       { 4*3600  ,10  },
                       { 5*3600  ,70  },
                       { 6*3600  ,20  },
                       { 7*3600  ,90  },
                       { 8*3600  ,0  },
                       { 9*3600  ,99  },
                       { 10*3600 ,12  },
                       { 11*3600 ,100  },
                       { 12*3600 ,12  },
                       { 14*3600 ,90  },
                       { 17*3600 ,11  },
                       { 19*3600 ,98  },
                       { 22*3600 ,2  },
                       { 24*3600  ,82  },
                     };


void loop() {
  // put your main code here, to run repeatedly:

/*
  rtc.setDOW(FRIDAY);        // Set Day-of-Week to FRIDAY
  rtc.setTime(21, 50, 0);     // Set the time to 12:00:00 (24hr format)
  rtc.setDate(6, 8, 2010);   // Set the date to August 6th, 2010
*/
  t = rtc.getTime();
  
  unsigned long secondsToday = (unsigned long) t.hour*3600L;
  secondsToday += t.min*60;
  secondsToday += t.sec;
  String str = String(secondsToday);
  Serial.println(rtc.getTimeStr());
  int count = 0;
  while (channel1Data[count][0] < secondsToday  ) {
    count++;
  }
Serial.println(secondsToday);
   unsigned long timeDifference = secondsToday - channel1Data[count][0] ;
Serial.println(timeDifference);  //Does not compute?

Serial.println(count);  //which element in array
  delay(1500);
//while (true);
}

The array channel1Data contains times, saved as seconds since midnight, with corresponding light intensities. (yeah another fish tank controller)

The code looks up a value greater than the current time. But it never gets past 10 o clock, and the timedifference between the looked up value and now are not correct.

Also the variable count, used in the while loop does strange things.

The output is for example:

22:30:14
81014
110550
10

The secondsToday are correct, but the difference is bigger then there are seconds in a day.

I read a lot about types and casting, but maybe I made some other noob error.

I am rather new to C++, so bear with me.

because Arduino (uno) is 16bit, when compiler sees constants like this:

24*3600

it by default treats them as 16 bit numbers. so your math overflows the int type:

86400 is greater than 65536 (max int)

try

 24*3600UL

tells compiler, this is an unsigned long so treat me thusly.

while (channel1Data[count][0] < secondsToday)
    count++;

When you exit the loop, channel1Data[count][0] >= secondsToday otherwise the loop continues.
So that means that secondsToday - channel1Data[count][0] is potentially negative.

also,

FYI you can mix data types in an array using a struct:

struct myStruct{
  unsigned long timeValue;
  byte intensity;
};

myStruct timeBright[] = 
{ {        0UL, 100}, 
  {     3600UL,  50}, //1
  { 2 * 3600UL,  10},
  { 3 * 3600UL,  90},
  { 4 * 3600UL,  10}, // ** see example 
  { 5 * 3600UL,  70},
  { 6 * 3600UL,  20},
  { 7 * 3600UL,  90},
  { 8 * 3600UL,   0},
  { 9 * 3600UL,  99},
  {10 * 3600UL,  12},
  {11 * 3600UL, 100},
  {12 * 3600UL,  12},
  {14 * 3600UL,  90},
  {17 * 3600UL,  11},
  {19 * 3600UL,  98},
  {22 * 3600UL,   2},
  {24 * 3600UL,  82},
};

and access the elements like the:

unsigned long myTimeValue = timeBright[4].timeValue;
byte myBriteness = timeBright[4].intensity;

Hello and welcome

If I understand it right, your code can be greatly simplified.

Store one light intensity value for each hour of the day. Array size reduced from 144 bytes to 24 bytes...

uint8_t channel1Data[] = { 100, 50, 10, etc };

Get the current light intensity with

channel1Data[ t.hour ]

At all: Thanks!

@guix:
Would be, but the array will not be neat on every hour. Still thanks for the tip.

@BulldogLowell:
Thanks, I didn't know struct but now I do.