Hello all,
I am an arduino noob, and am having a heck of a time figuring out how to write a simple script to adjust the brightness of some LEDs depending on the time of day. Ideally, I would like to set it up using TimeLord.h since that seems to be a fairly good library. However, I'm honestly not sure where to even start.
This is the project I'm working on.
And the code used is below (removed some to fit character count, full code here): any guidance on where to start, or even just have it be one value from 6a-6p and a second from 6p-6a would be great.
Thanks in advance for any help!
#include <ESP8266WiFi.h>
#include <FastLED.h>
#include <vector>
using namespace std;
#define FASTLED_ESP8266_RAW_PIN_ORDER
#define DEBUG false
int status = WL_IDLE_STATUS;
// Define the array of leds
CRGB leds[NUM_AIRPORTS];
#define DATA_PIN 5
#define LED_TYPE WS2811
#define COLOR_ORDER RGB
#define BRIGHTNESS 20
std::vector<unsigned short int> lightningLeds;
std::vector<String> airports({
"KKIC", // order of LEDs, starting with 1 should be KKIC; use VFR, WVFR, MVFR, IFR, LIFR for key; NULL for no airport
"KMRY", // 2
"KSNS", // 3
"KCVH", // 4
"KE16", // 5
"KWVI", // 6
"KRHV", // 7
"KSJC", // 8
"KNUQ", // 9
"KPAO", // 10
"KSQL", // 11
"KHAF", // 12
"KSFO", // 13
"KOAK", // 14
"KHWD", // 15
"KLVK", // 16
"KC83", // 17
"NULL", // 18 empty
"KCCR", // 19
"KSUU", // 20
"KVCB", // 21
"KAPC", // 22
"KDVO", // 23
"KO69", // 24
"KSTS", // 25
"KHES", // 26
"NULL", // 27 empty
"KUKI", // 28
"KRBL", // 29
"NULL", // 30 empty
"KCIC", // 31
"NULL", // 32 empty
"KOVE", // 33
"NULL", // 34 empty
"KMYV", // 35
"KBAB", // 36
"KAUN", // 37
"KLHM", // 38
"KSMF", // 39
"KEDU", // 40
"KSAC", // 41
"KMCC", // 42
"KMHR", // 43
"NULL", // 44 empty
"KPVF", // 45
"NULL", // 46 empty
"KBLU", // 47
"KTRK", // 48
"NULL", // 49 empty
"KTVL", // 50
"KO22", // 51
"KCPU", // 52
"KJAQ", // 53
"NULL", // 54 empty
"KSCK", // 55
"KMOD", // 56
"NULL", // 57 empty
"KMER", // 58
"KMCE", // 59
"NULL", // 60 empty
"KMAE", // 61
"KO88" // 62
});
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(74880);
//pinMode(D1, OUTPUT); //Declare Pin mode
//while (!Serial) {
// ; // wait for serial port to connect. Needed for native USB
//}
pinMode(LED_BUILTIN, OUTPUT); // give us control of the onboard LED
digitalWrite(LED_BUILTIN, LOW);
// Initialize LEDs
FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_AIRPORTS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(BRIGHTNESS);
}
void loop() {
digitalWrite(LED_BUILTIN, LOW); // on if we're awake
int c;
const unsigned int lightningLoopThreshold = REQUEST_INTERVAL / LIGHTNING_INTERVAL;
return;
}
Serial.println("OK!");
if (ledStatus) fill_solid(leds, NUM_AIRPORTS, CRGB::Purple); // indicate status with LEDs
FastLED.show();
ledStatus = false;
}
}
FastLED.show();
delay(25);
for (unsigned short int i = 0; i < lightningLeds.size(); ++i) {
unsigned short int currentLed = lightningLeds[i];
leds[currentLed] = lightning[i]; // restore original color
}
FastLED.show();
digitalWrite(LED_BUILTIN, HIGH);
delay(LIGHTNING_INTERVAL); // pause during the interval
}
if (lightningLeds.size() == 0 || !DO_LIGHTNING || lightningLoops >= lightningLoopThreshold) {
if (DEBUG) {
fill_gradient_RGB(leds, NUM_AIRPORTS, CRGB::Red, CRGB::Blue); // Just let us know we're running
FastLED.show();
}
Serial.println("Getting METARs ...");
if (getMetars()) {
Serial.println("Refreshing LEDs.");
FastLED.show();
if (DO_LIGHTNING && lightningLeds.size() > 0) {
lightningLoops = 0;
Serial.println("There is lightning, so no long sleep.");
} else {
Serial.print("No lightning; Going into sleep for: ");
Serial.println(REQUEST_INTERVAL);
digitalWrite(LED_BUILTIN, HIGH);
delay(REQUEST_INTERVAL); // delay 10 seconds to give it time to go
}
} else {
delay(RETRY_TIMEOUT); // try again if unsuccessful
}
}
}
doColor(currentAirport, led, currentWind.toInt(), currentGusts.toInt(), currentCondition, currentWxstring);
}
currentAirport = ""; // Reset everything when the airport changes
readingAirport = true;
currentCondition = "";
currentWind = "";
currentGusts = "";
currentWxstring = "";
} else if (readingAirport) {
if (!currentLine.endsWith("<")) {
currentAirport += c;
} else {
readingAirport = false;
for (unsigned short int i = 0; i < NUM_AIRPORTS; i++) {
if (airports[i] == currentAirport) {
led = i;
}
}
}
} else if (currentLine.endsWith("<wind_speed_kt>")) {
readingWind = true;
} else if (readingWind) {
if (!currentLine.endsWith("<")) {
currentWind += c;
} else {
readingWind = false;
}
} else if (currentLine.endsWith("<wind_gust_kt>")) {
readingGusts = true;
} else if (readingGusts) {
if (!currentLine.endsWith("<")) {
currentGusts += c;
} else {
readingGusts = false;
}
} else if (currentLine.endsWith("<flight_category>")) {
readingCondition = true;
} else if (readingCondition) {
if (!currentLine.endsWith("<")) {
currentCondition += c;
} else {
readingCondition = false;
}
} else if (currentLine.endsWith("<wx_string>")) {
readingWxstring = true;
} else if (readingWxstring) {
if (!currentLine.endsWith("<")) {
currentWxstring += c;
} else {
readingWxstring = false;
}
}
t = millis(); // Reset timeout clock
} else if ((millis() - t) >= (READ_TIMEOUT * 1000)) {
Serial.println("---Timeout---");
fill_solid(leds, NUM_AIRPORTS, CRGB::Cyan); // indicate status with LEDs
FastLED.show();
ledStatus = true;
client.stop();
return false;
}
}
}
// need to doColor this for the last airport
doColor(currentAirport, led, currentWind.toInt(), currentGusts.toInt(), currentCondition, currentWxstring);
// Do the key LEDs now if they exist
for (int i = 0; i < (NUM_AIRPORTS); i++) {
// Use this opportunity to set colors for LEDs in our key then build the request string
if (airports[i] == "VFR") leds[i] = CRGB::Green;
else if (airports[i] == "WVFR") leds[i] = CRGB::Yellow;
else if (airports[i] == "MVFR") leds[i] = CRGB::Blue;
else if (airports[i] == "IFR") leds[i] = CRGB::Red;
else if (airports[i] == "LIFR") leds[i] = CRGB::Magenta;
}
client.stop();
return true;
}
void doColor(String identifier, unsigned short int led, int wind, int gusts, String condition, String wxstring) {
CRGB color;
Serial.print(identifier);
Serial.print(": ");
Serial.print(condition);
Serial.print(" ");
Serial.print(wind);
Serial.print("G");
Serial.print(gusts);
Serial.print("kts LED ");
Serial.print(led);
Serial.print(" WX: ");
Serial.println(wxstring);
if (wxstring.indexOf("TS") != -1) {
Serial.println("... found lightning!");
lightningLeds.push_back(led);
}
if (condition == "LIFR" || identifier == "LIFR") color = CRGB::Magenta;
else if (condition == "IFR") color = CRGB::Red;
else if (condition == "MVFR") color = CRGB::Blue;
else if (condition == "VFR") {
if ((wind > WIND_THRESHOLD || gusts > WIND_THRESHOLD) && DO_WINDS) {
color = CRGB::Yellow;
} else {
color = CRGB::Green;
}
} else color = CRGB::Black;
leds[led] = color;
}