Need help with the code to finish a project

Hello i am working on a project for my kids room. It is the world map that has a strip of ws2812 on top. What i am trying to do it to make it show the suns position relative to earth. So basically have individual leds light up to the time zones where there is daylight on earth. ( hope that make sense). I am using ds3231 real time clock so in case it gets unglued or power outage it can return to its correct position.

#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <Sodaq_DS3231.h>m n

#define PIN 6

//year, month, date, hour, min, sec and week-day(starts from 0 and goes to 6)
//writing any non-existent time-data may interfere with normal operation of the RTC.
//Take care of week-day also.

//DateTime dt(2017, 3, 1, 11, 28, 01, 3);

char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

dht DHT;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, PIN, NEO_GRB + NEO_KHZ800);


    void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

       Serial.begin(57600);
    Wire.begin();
    rtc.begin();
    //rtc.setDateTime(dt); //Adjust date-time as defined 'dt' above 
}


  void loop() {



      DateTime now = rtc.now(); //get the current date-time
     
      
      strip.setPixelColor(0, 255, 255, 255);
      strip.show();
     

    
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.date(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.print(' ');
  Serial.print(weekDay[now.dayOfWeek()]);
  Serial.println();
  }

so basically i need a led on a strip to come on (white color) accordingly every hour and then go off and have the next one come on. Sounds simple right? but i have no idea how to do if statements. Thank you in advance

24 WS2812s?
Then hourly you need to send 3 x 24 = 72 bytes of data, 3 bytes/LED, whether on or off.
For all on, you'd be marching 255,255,255 thru the locations where an LED is to be on. (up to 12 at a time? Maybe dimmer at the edges where it's just sunrise or just sunset?)

I haven't used the Adafruit library, I was playing with the FastLED.library for sending out info to 43 LEDs at a time, but making them all the same color.

You can get lots of help with if statements by using Google or your favorite search engine. You can also get lots of help by explaining to us in words what you want your if statement to do.

You should also fix the title of your post. fish is not the same as finish.

vaj4088:
You can get lots of help with if statements by using Google or your favorite search engine. You can also get lots of help by explaining to us in words what you want your if statement to do.

You should also fix the title of your post. fish is not the same as finish.

google search was my first choice and didnt find anything.
So basically if its 1300hrs do this strip.setPixelColor(18, 255, 255, 255)
yeah i saw the spelling error but i don't see a place to edit the title.

Searching using Google and search terms

C++ if

got me many results including a helpful first result.

How about something like

if (hour == 13) {
  strip.setPixelColor(18,255,255,255);
}

?

Does the library then not change the rest of them?

The description of the Adafruit Neopixel library is here: Arduino Library Use | Adafruit NeoPixel Überguide | Adafruit Learning System

It looks from the OP that there are 24 leds in the chain.
There has to be a mapping table (or simple formula) to convert Hour into LED-Number. In the easiest case Hour equals LED-Number.

In the loop(), detect a change of hour, set all the leds off and then immediately set the desired led on with colour white.
That is it.

6v6gt:
The description of the Adafruit Neopixel library is here: Arduino Library Use | Adafruit NeoPixel Überguide | Adafruit Learning System

It looks from the OP that there are 24 leds in the chain.
There has to be a mapping table (or simple formula) to convert Hour into LED-Number. In the easiest case Hour equals LED-Number.

In the loop(), detect a change of hour, set all the leds off and then immediately set the desired led on with colour white.
That is it.

Yes that is my plan but i have no idea how to put that in a code form.

@Swordfish
I've embedded some code in the code you supplied. It should immediately light the led for the current hour on the strip, counting the first led as 0.
I've assumed all the Real Time Clock code works and I can't test it.

#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include <Sodaq_DS3231.h>m n

#define PIN 6

//year, month, date, hour, min, sec and week-day(starts from 0 and goes to 6)
//writing any non-existent time-data may interfere with normal operation of the RTC.
//Take care of week-day also.

//DateTime dt(2017, 3, 1, 11, 28, 01, 3);

char weekDay[][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

dht DHT;

Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, PIN, NEO_GRB + NEO_KHZ800);


    void setup() {
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'

       Serial.begin(57600);
    Wire.begin();
    rtc.begin();
    //rtc.setDateTime(dt); //Adjust date-time as defined 'dt' above
}


  void loop() {



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

      // new code - assumes that the rtc code works
      // assumes that the led strip position 0 corresponds with the position for 0 hours on the map
      // and the strip runs east to west
      // if not we need a correcting formula here.

      static int lastHour = -1 ;  // initialised only once at start !

      if ( now.hour() != lastHour ) {
         lastHour = now.hour() ;
         for (i = 0 ; i < 24 ; i++ ) {
             // set all pixels off
             strip.setPixelColor(i, 0, 0, 0);
         }
         // set pixel for current hour on    
         strip.setPixelColor(now.hour(), 255, 255, 255);
         strip.show();
      }

   
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.date(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.print(' ');
  Serial.print(weekDay[now.dayOfWeek()]);
  Serial.println();
  }

If that does not work, the next stage is to create a script exclusively for the led strip and get that working before integrating the clock part.

@6v6gt

Thank you.

The code you gave me had a error ( 'i' was not declared in this scope)

But i was able to do it this way.

 if ( now.hour() == 1 ) {
          strip.setPixelColor(10, 50, 50, 50);
          strip.setPixelColor(11, 255, 255, 255);
          strip.setPixelColor(12, 50, 50, 50);
         }
               if ( now.hour() == 2 ) {
          strip.setPixelColor(10, 0, 0, 0);
          strip.setPixelColor(11, 50, 50, 50);
          strip.setPixelColor(12, 255, 255, 255);
          strip.setPixelColor(13, 50, 50, 50);
          }

Thanks you @vaj4088

Thanks again everyone.

OK. Good. It sounds like you are having some success.

The error I made was here:

for (i = 0 ; i < 24 ; i++ ) {

it should have been

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

@6v6gt yes that worked. Thank you :slight_smile: