(First of all, I have to say I know or understand basically nothing about programming, this is my first project/attempt, so please take that into account when making questions or explanations about complex things (basically explain it using very simple things please)
Hi everyone, so I´m making a sunrise alarm to wake up in the mornings more easily for a college project that I hand in in like 1-1.5 week (I´ve been trying to get the code to work for like a month but nothing goes my way).
The steps I want to achieve are:
- How to fade between colors (simulate sunrise) [I think I already have this one, I found [this website that generates code for leds and its effects](LED Strip Effects generator) ]
- how to get the rtc working [After trying many libraries and not knowing how to use them I got this one that sets up the time to the laptop automatically, and has the alarm example sketch, which I think I know how to use ¡]
- lastly, how to trigger the led to turn on and off at a certain time everyday
The electronics that I´m using are:
- Arduino UNO (elegoo brand)
- adafruit neopixel ring 16 RGB LEDs
- DS3231 RTC
- cables
- the project is fed with two 5V split usbs, one at 1A and the other at 2.4A (for extra juice)
( I believe the wiring is fine because things work independently, I just have to get the code for the whole thing right)
SO, the thing is I have 2 codes, one that makes the LEDs fade (from the generator from before, but these are not the final colors, I´ll change them) AND the other code with the RTC stuff that I think triggers an alarm.
The question is: how can I mix both codes into one??
Here I attach both codes
- LEDs fading (set on pin PMW 6). I know the code is super long but that´s how it was generated and I don´t know enough to shorten it.
#include <Adafruit_NeoPixel.h>
class Strip
{
public:
uint8_t effect;
uint8_t effects;
uint16_t effStep;
unsigned long effStart;
Adafruit_NeoPixel strip;
Strip(uint16_t leds, uint8_t pin, uint8_t toteffects, uint16_t striptype) : strip(leds, pin, striptype) {
effect = -1;
effects = toteffects;
Reset();
}
void Reset(){
effStep = 0;
effect = (effect + 1) % effects;
effStart = millis();
}
};
struct Loop
{
uint8_t currentChild;
uint8_t childs;
bool timeBased;
uint16_t cycles;
uint16_t currentTime;
Loop(uint8_t totchilds, bool timebased, uint16_t tottime) {currentTime=0;currentChild=0;childs=totchilds;timeBased=timebased;cycles=tottime;}
};
Strip strip_0(16, 6, 16, NEO_GRB + NEO_KHZ800);
struct Loop strip0loop0(6, true, 30);
//[GLOBAL_VARIABLES]
void setup() {
//Your setup here:
strip_0.strip.begin();
}
void loop() {
//Your code here:
strips_loop();
}
void strips_loop() {
if(strip0_loop0() & 0x01)
strip_0.strip.show();
}
uint8_t strip0_loop0() {
uint8_t ret = 0x00;
switch(strip0loop0.currentChild) {
case 0:
ret = strip0_loop0_eff0();break;
case 1:
ret = strip0_loop0_eff1();break;
case 2:
ret = strip0_loop0_eff2();break;
case 3:
ret = strip0_loop0_eff3();break;
case 4:
ret = strip0_loop0_eff4();break;
case 5:
ret = strip0_loop0_eff5();break;
}
if(ret & 0x02) {
ret &= 0xfd;
if(strip0loop0.currentChild + 1 >= strip0loop0.childs) {
strip0loop0.currentChild = 0;
if(++strip0loop0.currentTime >= strip0loop0.cycles) {strip0loop0.currentTime = 0; ret |= 0x02;}
}
else {
strip0loop0.currentChild++;
}
};
return ret;
}
uint8_t strip0_loop0_eff0() {
// Strip ID: 0 - Effect: Fade - LEDS: 16
// Steps: 5 - Delay: 2000
// Colors: 2 (255.0.0, 255.77.0)
// Options: duration=10000, every=1,
if(millis() - strip_0.effStart < 2000 * (strip_0.effStep)) return 0x00;
uint8_t r,g,b;
double e;
e = (strip_0.effStep * 2000) / (double)10000;
r = ( e ) * 255 + 255 * ( 1.0 - e );
g = ( e ) * 77 + 0 * ( 1.0 - e );
b = ( e ) * 0 + 0 * ( 1.0 - e );
for(uint16_t j=0;j<16;j++) {
if((j % 1) == 0)
strip_0.strip.setPixelColor(j, r, g, b);
else
strip_0.strip.setPixelColor(j, 0, 0, 0);
}
if(strip_0.effStep >= 5) {strip_0.Reset(); return 0x03; }
else strip_0.effStep++;
return 0x01;
}
uint8_t strip0_loop0_eff1() {
// Strip ID: 0 - Effect: Fade - LEDS: 16
// Steps: 5 - Delay: 2000
// Colors: 2 (255.77.0, 255.103.0)
// Options: duration=10000, every=1,
if(millis() - strip_0.effStart < 2000 * (strip_0.effStep)) return 0x00;
uint8_t r,g,b;
double e;
e = (strip_0.effStep * 2000) / (double)10000;
r = ( e ) * 255 + 255 * ( 1.0 - e );
g = ( e ) * 103 + 77 * ( 1.0 - e );
b = ( e ) * 0 + 0 * ( 1.0 - e );
for(uint16_t j=0;j<16;j++) {
if((j % 1) == 0)
strip_0.strip.setPixelColor(j, r, g, b);
else
strip_0.strip.setPixelColor(j, 0, 0, 0);
}
if(strip_0.effStep >= 5) {strip_0.Reset(); return 0x03; }
else strip_0.effStep++;
return 0x01;
}
uint8_t strip0_loop0_eff2() {
// Strip ID: 0 - Effect: Fade - LEDS: 16
// Steps: 5 - Delay: 2000
// Colors: 2 (255.103.0, 255.129.0)
// Options: duration=10000, every=1,
if(millis() - strip_0.effStart < 2000 * (strip_0.effStep)) return 0x00;
uint8_t r,g,b;
double e;
e = (strip_0.effStep * 2000) / (double)10000;
r = ( e ) * 255 + 255 * ( 1.0 - e );
g = ( e ) * 129 + 103 * ( 1.0 - e );
b = ( e ) * 0 + 0 * ( 1.0 - e );
for(uint16_t j=0;j<16;j++) {
if((j % 1) == 0)
strip_0.strip.setPixelColor(j, r, g, b);
else
strip_0.strip.setPixelColor(j, 0, 0, 0);
}
if(strip_0.effStep >= 5) {strip_0.Reset(); return 0x03; }
else strip_0.effStep++;
return 0x01;
}
uint8_t strip0_loop0_eff3() {
// Strip ID: 0 - Effect: Fade - LEDS: 16
// Steps: 5 - Delay: 2000
// Colors: 2 (255.129.0, 255.167.0)
// Options: duration=10000, every=1,
if(millis() - strip_0.effStart < 2000 * (strip_0.effStep)) return 0x00;
uint8_t r,g,b;
double e;
e = (strip_0.effStep * 2000) / (double)10000;
r = ( e ) * 255 + 255 * ( 1.0 - e );
g = ( e ) * 167 + 129 * ( 1.0 - e );
b = ( e ) * 0 + 0 * ( 1.0 - e );
for(uint16_t j=0;j<16;j++) {
if((j % 1) == 0)
strip_0.strip.setPixelColor(j, r, g, b);
else
strip_0.strip.setPixelColor(j, 0, 0, 0);
}
if(strip_0.effStep >= 5) {strip_0.Reset(); return 0x03; }
else strip_0.effStep++;
return 0x01;
}
uint8_t strip0_loop0_eff4() {
// Strip ID: 0 - Effect: Fade - LEDS: 16
// Steps: 500 - Delay: 20
// Colors: 2 (255.167.0, 54.194.237)
// Options: duration=10000, every=1,
if(millis() - strip_0.effStart < 20 * (strip_0.effStep)) return 0x00;
uint8_t r,g,b;
double e;
e = (strip_0.effStep * 20) / (double)10000;
r = ( e ) * 54 + 255 * ( 1.0 - e );
g = ( e ) * 194 + 167 * ( 1.0 - e );
b = ( e ) * 237 + 0 * ( 1.0 - e );
for(uint16_t j=0;j<16;j++) {
if((j % 1) == 0)
strip_0.strip.setPixelColor(j, r, g, b);
else
strip_0.strip.setPixelColor(j, 0, 0, 0);
}
if(strip_0.effStep >= 500) {strip_0.Reset(); return 0x03; }
else strip_0.effStep++;
return 0x01;
}
uint8_t strip0_loop0_eff5() {
// Strip ID: 0 - Effect: Fade - LEDS: 16
// Steps: 20 - Delay: 5
// Colors: 2 (54.194.239, 255.255.255)
// Options: duration=100, every=1,
if(millis() - strip_0.effStart < 5 * (strip_0.effStep)) return 0x00;
uint8_t r,g,b;
double e;
e = (strip_0.effStep * 5) / (double)100;
r = ( e ) * 255 + 54 * ( 1.0 - e );
g = ( e ) * 255 + 194 * ( 1.0 - e );
b = ( e ) * 255 + 239 * ( 1.0 - e );
for(uint16_t j=0;j<16;j++) {
if((j % 1) == 0)
strip_0.strip.setPixelColor(j, r, g, b);
else
strip_0.strip.setPixelColor(j, 0, 0, 0);
}
if(strip_0.effStep >= 20) {strip_0.Reset(); return 0x03; }
else strip_0.effStep++;
return 0x01;
}
- RTC alarm
// during an alarm the INT pin of the RTC is pulled low
//
// this is handy for minimizing power consumption for sensor-like devices,
// since they can be started up by this pin on given time intervals.
#include <Wire.h>
#include "ds3231.h"
#define BUFF_MAX 256
// time when to wake up
uint8_t wake_HOUR = 14;
uint8_t wake_MINUTE = 10;
uint8_t wake_SECOND = 39;
// how often to refresh the info on stdout (ms)
unsigned long prev = 5000, interval = 5000;
void set_alarm(void)
{
// flags define what calendar component to be checked against the current time in order
// to trigger the alarm - see datasheet
// A1M1 (seconds) (0 to enable, 1 to disable)
// A1M2 (minutes) (0 to enable, 1 to disable)
// A1M3 (hour) (0 to enable, 1 to disable)
// A1M4 (day) (0 to enable, 1 to disable)
// DY/DT (dayofweek == 1/dayofmonth == 0)
uint8_t flags[5] = { 0, 0, 0, 1, 1 };
// set Alarm1
DS3231_set_a1(wake_SECOND, wake_MINUTE, wake_HOUR, 0, flags);
// activate Alarm1
DS3231_set_creg(DS3231_CONTROL_INTCN | DS3231_CONTROL_A1IE);
}
void setup()
{
Serial.begin(9600);
Wire.begin();
DS3231_init(DS3231_CONTROL_INTCN);
DS3231_clear_a1f();
set_alarm();
}
void loop()
{
char buff[BUFF_MAX];
unsigned long now = millis();
struct ts t;
// once a while show what is going on
if ((now - prev > interval) && (Serial.available() <= 0)) {
DS3231_get(&t);
// display current time
snprintf(buff, BUFF_MAX, "%d.%02d.%02d %02d:%02d:%02d", t.year,
t.mon, t.mday, t.hour, t.min, t.sec);
Serial.println(buff);
// display a1 debug info
DS3231_get_a1(&buff[0], 59);
Serial.println(buff);
if (DS3231_triggered_a1()) {
// INT has been pulled low
Serial.println(" -> alarm1 has been triggered");
// clear a1 alarm flag and let INT go into hi-z
DS3231_clear_a1f();
}
prev = now;
}
}
Any suggestions, explanations, edits and help will be greatly appreciated!!!
(Also, this is way too much and don´t have time for it but in the future I´d like to also make it a sunset lamp, basically the same function but only changes adding a second alarm in the afternoon and changing the colors)