Teensy 3.5 adding snooze function to my existing project doesn't compile

Status
Not open for further replies.

spe672

Member
I've studied the SnoozePWM example as best I can, as well as dug though the forums for hours...hopefully this hasn't been asked and fixed already. I get an error when compiling the snoozeSleepPWM code pieces when mixed with my own code. I've added the missing .h and .cpp files to the folder where the .ino is stored. This has also affected my SD library calls, not sure why. I had to add the "#include <SD_t3.h>" line to get rid of a new SD error; this is not the purpose of this question though.

Basically, I (think I) want to set up the snooze feature to run after completing my main task to sleep a portion of every second. I'm still working on setting up what I want each loop to accomplish; there's probably better architecture possible, but I can't even get the thing to compile so I can improve it. I don't necessarily need to function every second, but I can't work that out till I get this to compile. I think there is a file location issue, but don't know where else to copy files so the compiler can see them.

The error is as shown in picture below with some of the file structure. I'm wondering if the Arduino IDE can't access what it needs to. Basically, I have lookup tables for my latitude which turn on a lighting system at sundown and add some fading; it turns off at sunrise. I'm monitoring the battery voltage and intend to provide load-shed of high current draw circuitry if the solar cell can't keep up with use every night. When it charges back up enough, it will continue with the main task. That portion isn't implemented yet, since the snooze code won't compile. I've added the SnoozeSleepPWM.h that it's complaining about to the working directory; it's also in the Teensy folder and I'm not sure why it can't see it in the Arduino folder structure. The SD card is collecting battery data every 10 mins and solar data every minute while the sun is up. This is all intended to be dumped through bluetooth upon a request which has not been implemented yet either since I'm stuck on this problem. Please help me get the compile issue resoled.

Thank you
Andy

Screen Shot 2019-01-25 at 12.37.44 AM.jpg

Screen Shot 2019-01-25 at 12.38.51 AM.png

I'm also not sure how to put my source code into a text box...advice appreciated. :)

#include <SD_t3.h>
#include <SD.h>



// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include <RTClibExtended.h>



//#include <Snooze.h>
#include <SnoozeBlock.h>
//#include "SnoozeSleepPWM.h"
#include "SnoozeSleepPWM.ccp"

//SnoozeTimer timer;
SnoozeSleepPWM pwm;// 'sleep' mode pwm driver

// Connect the timer and pwm drivers
SnoozeBlock config(timer, pwm);

//#include "Adafruit_BLE_UART.h"
//
//#define ADAFRUITBLE_REQ 10
//#define ADAFRUITBLE_RDY 2
//#define ADAFRUITBLE_RST 9
//
//Adafruit_BLE_UART uart = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);

File myFile;
File daylight;
File battery;
File v_consumed;

RTC_DS3231 rtc;

//char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

//Arrays
//"0" in array position 0 to shift all data to position 1.
//Day of year calculation starts on 1 and is used to access the data.
int Civil_Dawn_Mins [ ] = {0, 435, 435, 436, 436, 436, 436, 436, 436, 436, 436, 436, 436, 435, 435, 435, 435, 434, 434, 434, 433, 433, 432, 432, 431, 431, 430, 430, 429, 428, 428, 427, 426, 425, 425, 424, 423, 422, 421, 420, 419, 418, 417, 416, 415, 414, 413, 412, 411, 409, 408, 407, 406, 405, 403, 402, 401, 399, 398, 397, 395, 394, 393, 391, 390, 389, 387, 386, 384, 443, 441, 440, 438, 437, 435, 434, 432, 431, 429, 428, 426, 425, 423, 422, 420, 419, 417, 416, 414, 412, 411, 409, 408, 406, 405, 403, 402, 400, 399, 397, 396, 394, 393, 391, 390, 388, 387, 385, 384, 382, 381, 380, 378, 377, 375, 374, 373, 371, 370, 369, 368, 366, 365, 364, 363, 362, 360, 359, 358, 357, 356, 355, 354, 353, 352, 351, 350, 349, 348, 347, 347, 346, 345, 344, 343, 343, 342, 342, 341, 340, 340, 339, 339, 338, 338, 338, 337, 337, 337, 337, 336, 336, 336, 336, 336, 336, 336, 336, 336, 336, 336, 336, 336, 337, 337, 337, 337, 338, 338, 339, 339, 339, 340, 340, 341, 341, 342, 343, 343, 344, 344, 345, 346, 347, 347, 348, 349, 350, 350, 351, 352, 353, 354, 354, 355, 356, 357, 358, 359, 360, 361, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 396, 397, 398, 399, 400, 401, 402, 403, 403, 404, 405, 406, 407, 408, 409, 409, 410, 411, 412, 413, 414, 415, 416, 416, 417, 418, 419, 420, 421, 422, 423, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 413, 414, 415, 416, 417, 418, 419, 420, 421, 421, 422, 423, 424, 425, 425, 426, 427, 427, 428, 429, 429, 430, 431, 431, 432, 432, 433, 433, 433, 434, 434, 434, 435, 435,435};
int Sunrise_Mins [ ] = {0, 465,465,465,465,465,465,465,465,465,465,465,464,464,464,464,463,463,463,462,462,461,461,460,460,459,458,458,457,456,455,455,454,453,452,451,450,449,448,447,446,445,444,443,442,441,440,439,437,436,435,434,432,431,430,429,427,426,425,423,422,420,419,418,416,415,413,412,410,469,467,466,464,463,461,460,458,457,455,454,452,451,449,448,446,445,443,442,440,439,437,436,434,433,431,430,428,427,425,424,422,421,419,418,417,415,414,412,411,410,408,407,406,404,403,402,401,399,398,397,396,395,393,392,391,390,389,388,387,386,385,384,383,382,381,380,379,379,378,377,376,376,375,374,374,373,372,372,371,371,370,370,370,369,369,368,368,368,368,368,367,367,367,367,367,367,367,367,367,367,367,368,368,368,368,368,369,369,369,370,370,371,371,371,372,372,373,374,374,375,375,376,377,377,378,378,379,380,381,381,382,383,384,384,385,386,387,388,388,389,390,391,392,392,393,394,395,396,397,398,398,399,400,401,402,403,404,404,405,406,407,408,409,410,410,411,412,413,414,415,415,416,417,418,419,420,421,421,422,423,424,425,426,426,427,428,429,430,431,431,432,433,434,435,436,437,437,438,439,440,441,442,443,443,444,445,446,447,448,449,450,451,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,416,417,418,419,420,421,422,423,424,425,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450,451,451,452,453,454,455,455,456,457,457,458,459,459,460,460,461,461,462,462,463,463,463,464,464,464,464};
int Sunset_Mins [ ] = {0, 1041,1041,1042,1043,1044,1045,1046,1047,1047,1048,1049,1050,1051,1052,1053,1054,1055,1056,1058,1059,1060,1061,1062,1063,1064,1065,1066,1067,1068,1070,1071,1072,1073,1074,1075,1076,1077,1078,1080,1081,1082,1083,1084,1085,1086,1087,1088,1089,1090,1091,1093,1094,1095,1096,1097,1098,1099,1100,1101,1102,1103,1104,1105,1106,1107,1108,1109,1110,1171,1172,1173,1174,1175,1175,1176,1177,1178,1179,1180,1181,1182,1183,1184,1185,1186,1187,1187,1188,1189,1190,1191,1192,1193,1194,1195,1196,1197,1198,1198,1199,1200,1201,1202,1203,1204,1205,1206,1207,1208,1208,1209,1210,1211,1212,1213,1214,1215,1216,1217,1218,1219,1219,1220,1221,1222,1223,1224,1225,1226,1227,1228,1229,1229,1230,1231,1232,1233,1234,1235,1235,1236,1237,1238,1239,1239,1240,1241,1242,1242,1243,1244,1245,1245,1246,1247,1247,1248,1248,1249,1250,1250,1251,1251,1251,1252,1252,1253,1253,1253,1254,1254,1254,1254,1255,1255,1255,1255,1255,1255,1255,1255,1255,1255,1255,1255,1255,1254,1254,1254,1254,1253,1253,1253,1252,1252,1251,1251,1250,1250,1249,1249,1248,1247,1247,1246,1245,1244,1243,1243,1242,1241,1240,1239,1238,1237,1236,1235,1234,1233,1232,1231,1230,1228,1227,1226,1225,1224,1222,1221,1220,1218,1217,1216,1214,1213,1212,1210,1209,1208,1206,1205,1203,1202,1200,1199,1197,1196,1194,1193,1191,1190,1188,1187,1185,1184,1182,1181,1179,1177,1176,1174,1173,1171,1170,1168,1167,1165,1163,1162,1160,1159,1157,1156,1154,1153,1151,1150,1148,1147,1145,1144,1142,1141,1139,1138,1136,1135,1134,1132,1131,1129,1128,1127,1125,1124,1123,1122,1120,1119,1118,1117,1115,1114,1113,1112,1111,1050,1049,1048,1047,1046,1045,1044,1043,1042,1041,1040,1040,1039,1038,1038,1037,1036,1036,1035,1034,1034,1033,1033,1033,1032,1032,1032,1031,1031,1031,1031,1031,1030,1030,1030,1030,1030,1030,1031,1031,1031,1031,1031,1032,1032,1032,1033,1033,1034,1034,1035,1035,1036,1036,1037,1038,1038,1039,1040,1040};
int Civil_Dusk_Mins [ ] = {0, 1070, 1071, 1071, 1072, 1073, 1074, 1075, 1076, 1076, 1077, 1078, 1079, 1080, 1081, 1082, 1083, 1084, 1085, 1086, 1087, 1088, 1089, 1090, 1091, 1092, 1093, 1094, 1095, 1096, 1097, 1098, 1099, 1101, 1102, 1103, 1104, 1105, 1106, 1107, 1108, 1109, 1110, 1111, 1112, 1113, 1114, 1115, 1116, 1117, 1118, 1119, 1120, 1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128, 1129, 1130, 1131, 1132, 1133, 1134, 1135, 1136, 1197, 1198, 1199, 1200, 1201, 1202, 1203, 1203, 1204, 1205, 1206, 1207, 1208, 1209, 1210, 1211, 1212, 1213, 1214, 1215, 1216, 1217, 1218, 1218, 1219, 1220, 1221, 1222, 1223, 1224, 1225, 1226, 1227, 1228, 1229, 1230, 1231, 1232, 1233, 1234, 1235, 1236, 1237, 1238, 1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246, 1247, 1248, 1249, 1250, 1251, 1252, 1253, 1254, 1255, 1256, 1257, 1258, 1259, 1260, 1260, 1261, 1262, 1263, 1264, 1265, 1266, 1267, 1268, 1269, 1270, 1270, 1271, 1272, 1273, 1274, 1274, 1275, 1276, 1277, 1277, 1278, 1279, 1279, 1280, 1281, 1281, 1282, 1282, 1283, 1283, 1284, 1284, 1284, 1285, 1285, 1285, 1285, 1286, 1286, 1286, 1286, 1286, 1286, 1286, 1286, 1286, 1286, 1286, 1286, 1286, 1286, 1285, 1285, 1285, 1285, 1284, 1284, 1283, 1283, 1282, 1282, 1281, 1281, 1280, 1279, 1279, 1278, 1277, 1276, 1276, 1275, 1274, 1273, 1272, 1271, 1270, 1269, 1268, 1267, 1266, 1265, 1264, 1263, 1262, 1260, 1259, 1258, 1257, 1255, 1254, 1253, 1252, 1250, 1249, 1248, 1246, 1245, 1243, 1242, 1241, 1239, 1238, 1236, 1235, 1233, 1232, 1230, 1229, 1227, 1226, 1224, 1223, 1221, 1219, 1218, 1216, 1215, 1213, 1212, 1210, 1208, 1207, 1205, 1204, 1202, 1201, 1199, 1197, 1196, 1194, 1193, 1191, 1190, 1188, 1187, 1185, 1183, 1182, 1180, 1179, 1177, 1176, 1174, 1173, 1171, 1170, 1168, 1167, 1166, 1164, 1163, 1161, 1160, 1159, 1157, 1156, 1155, 1153, 1152, 1151, 1149, 1148, 1147, 1146, 1145, 1144, 1142, 1141, 1140, 1139, 1138, 1077, 1076, 1075, 1074, 1073, 1072, 1071, 1071, 1070, 1069, 1068, 1068, 1067, 1066, 1066, 1065, 1064, 1064, 1063, 1063, 1062, 1062, 1062, 1061, 1061, 1061, 1060, 1060, 1060, 1060, 1060, 1060, 1059, 1059, 1059, 1059, 1060, 1060, 1060, 1060, 1060, 1060, 1061, 1061, 1061, 1062, 1062, 1062, 1063, 1063, 1064, 1064, 1065, 1066, 1066, 1067, 1068, 1068, 1069,1069};
int Naut_Dusk_Mins [ ] = {0, 1103, 1103, 1104, 1105, 1106, 1106, 1107, 1108, 1109, 1110, 1111, 1112, 1112, 1113, 1114, 1115, 1116, 1117, 1118, 1119, 1120, 1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128, 1129, 1130, 1131, 1132, 1133, 1134, 1135, 1136, 1137, 1138, 1139, 1140, 1141, 1142, 1143, 1144, 1145, 1146, 1147, 1148, 1149, 1150, 1151, 1152, 1153, 1154, 1155, 1156, 1157, 1158, 1159, 1160, 1160, 1161, 1162, 1163, 1164, 1165, 1166, 1227, 1228, 1229, 1230, 1231, 1232, 1233, 1234, 1235, 1236, 1237, 1238, 1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246, 1247, 1248, 1249, 1250, 1251, 1252, 1253, 1254, 1255, 1256, 1257, 1258, 1259, 1260, 1261, 1262, 1263, 1264, 1265, 1266, 1267, 1269, 1270, 1271, 1272, 1273, 1274, 1275, 1276, 1277, 1279, 1280, 1281, 1282, 1283, 1284, 1285, 1286, 1288, 1289, 1290, 1291, 1292, 1293, 1294, 1295, 1296, 1297, 1299, 1300, 1301, 1302, 1303, 1304, 1305, 1306, 1307, 1308, 1309, 1310, 1311, 1312, 1312, 1313, 1314, 1315, 1316, 1316, 1317, 1318, 1319, 1319, 1320, 1321, 1321, 1322, 1322, 1323, 1323, 1323, 1324, 1324, 1324, 1325, 1325, 1325, 1325, 1325, 1325, 1325, 1325, 1325, 1325, 1325, 1325, 1325, 1325, 1324, 1324, 1324, 1323, 1323, 1322, 1322, 1321, 1321, 1320, 1320, 1319, 1318, 1317, 1317, 1316, 1315, 1314, 1313, 1312, 1311, 1310, 1309, 1308, 1307, 1306, 1305, 1304, 1303, 1301, 1300, 1299, 1298, 1296, 1295, 1294, 1292, 1291, 1290, 1288, 1287, 1285, 1284, 1282, 1281, 1279, 1278, 1276, 1275, 1273, 1272, 1270, 1269, 1267, 1266, 1264, 1262, 1261, 1259, 1257, 1256, 1254, 1253, 1251, 1249, 1248, 1246, 1244, 1243, 1241, 1240, 1238, 1236, 1235, 1233, 1231, 1230, 1228, 1227, 1225, 1223, 1222, 1220, 1219, 1217, 1215, 1214, 1212, 1211, 1209, 1208, 1206, 1205, 1203, 1202, 1200, 1199, 1197, 1196, 1195, 1193, 1192, 1190, 1189, 1188, 1186, 1185, 1184, 1183, 1181, 1180, 1179, 1178, 1177, 1175, 1174, 1173, 1172, 1171, 1170, 1169, 1108, 1107, 1106, 1105, 1104, 1104, 1103, 1102, 1101, 1101, 1100, 1099, 1098, 1098, 1097, 1097, 1096, 1096, 1095, 1095, 1094, 1094, 1094, 1093, 1093, 1093, 1093, 1092, 1092, 1092, 1092, 1092, 1092, 1092, 1092, 1092, 1092, 1092, 1093, 1093, 1093, 1093, 1094, 1094, 1094, 1095, 1095, 1095, 1096, 1096, 1097, 1097, 1098, 1099, 1099, 1100, 1100, 1101, 1102,1102};



//constants
const int whitePin1 = 9;
const int whitePin2 = 30;
const int redPin = 3;
const int yellowPin = 5;
const int greenPin = 6;
const int chipSelect = BUILTIN_SDCARD;
const int solarpin = A0;
const int battpin = A1;

// variables:
int batt_ADC_count; // ADC Measured battery voltage. Actual voltage scaled down through voltage divider
//int lightvoltage; // currently unused light sensor
int solar_ADC_count; // ADC measured solar cell voltage. Actual voltage scaled down through voltage divider
float solar_voltage; // measured voltage from the solar cell
int Today_in_Mins; // calculated current time in minutes
int currentDay; // RTC collected day
int currentMonth; // RTC collected month
int currentYear; // RTC collected year
int currentSecond; // RTC collected second
int Today_in_Days; // calculated day of the year. Leap year not currently dealt with...
int Current_Year; // RTC collected year
int On_Bright_Today; // turn on time based on the day of the year per the database
int On_Dim_Today; // the time dimming should be at 10%
int Off_Today; // Turn off time in the morning
int off_dim_Today; // the time dimming should start to brighten up from 10%
int dim_mins; // Difference on minutes between civil and nautical dusk
float dim_Value = 100; // variable for adjusting dimming between civil and nautical dusk;
float dim_Percent = 0; // calculated dimming percentage to subtract between civil and nautical dusk;
float batt_voltage; // calculated voltage from the ADC read value
int off_hour; // calculated turn off hour for serial print
float off_min; // calculated turn off min for serial print
int on_hour; // calculated turn on hour for serial print
float on_min; // calculated turn on min for serial print
int on_dim_hour; // calculated hour 10% brightness for serial print
float on_dim_min; // calculated min 10% brightness for serial print
int isItDaytime; // 1 is Day, 0 is night
float sunrise_voltage = 0; // voltage collected at Sunrise
float sunset_voltage = 0; // voltage collected at sunset
float Daily_V_Delta; // voltage charged or consumed
int onboardLED = 13;

int waitSeconds;
int waitCounts;

////**********snooze stuff****************
SnoozeTimer timer;
SnoozeSleepPWM pwm;// 'sleep' mode pwm driver
//
//// Connect the timer and pwm drivers
SnoozeBlock config(timer, pwm);
////***************************************


void setup() {
// put your setup code here, to run once:
pinMode(whitePin1, OUTPUT);
pinMode(whitePin2, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(onboardLED, OUTPUT);

Serial.println("Set Lights OFF");
analogWrite(whitePin1, 0);
analogWrite(whitePin2, 0);

//******************SD CARD init*************************
Serial.print("Initializing SD card...");

if (!SD.begin(chipSelect)) {
Serial.println("Initialization Failed");
return;
}

Serial.println("Initialization done");



//this compiles, but lights don't come on. needs some work
analogWriteFrequency(9, 375000); // Teensy 3.0 pin 3 also changes to 375 kHz
analogWriteFrequency(30, 375000); // Teensy 3.0 pin 3 also changes to 375 kHz


// initialize USB communication at 38400 bits per second:
// Serial.begin(38400);



if (! rtc.begin()) {
Serial.println(F("Couldn't find RTC"));
while (1);
}

if (rtc.lostPower()) {
Serial.println(F("RTC lost power, lets set the time!"));
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}

//****************************bluetooth stuff
// Serial.begin(9600);
// while(!Serial); // Leonardo/Micro should wait for serial init
// Serial.println(F("Adafruit Bluefruit Low Energy nRF8001 Callback Echo demo"));
//
// uart.setRXcallback(rxCallback);
// uart.setACIcallback(aciCallback);
// // uart.setDeviceName("NEWNAME"); /* 7 characters max! */
// uart.begin();

//******************snooze stuff******************
// pinMode(LED_BUILTIN, OUTPUT);
// timer.setTimer(30);
// delay(300);
//**************************************************

}


void loop() {



//print the current measured time
DateTime now = rtc.now();
currentYear = now.year();
currentMonth = now.month();
currentDay = now.day();
currentSecond = now.second();


//access arrays and place values into today's variable
off_dim_Today = Civil_Dawn_Mins [Today_in_Days];
Off_Today = Sunrise_Mins [Today_in_Days]; //**************************
On_Bright_Today = Sunset_Mins [Today_in_Days]; //***************************
On_Dim_Today = Civil_Dusk_Mins [Today_in_Days];
dim_mins = ((On_Dim_Today - On_Bright_Today) * 0.30); //calculate time difference between sundown and civil dusk and turn on at 30% of that time difference
dim_Percent = 90.0 / dim_mins; //***insert fixed value during prototyping to speed up dim time*** //turn time difference into a percentage to subtract every loop
On_Bright_Today = On_Bright_Today + dim_mins; //shift the on time to a 30% midpoint between sundown and civil dusk


//*********Sync to 00 seconds in RTC****************

waitSeconds= 60 - currentSecond;
Serial.print("current second ");
Serial.println(currentSecond);
Serial.print("wait this many seconds ");
Serial.println(waitSeconds);
waitCounts = waitSeconds * 1000;
Serial.print("wait counts ");
Serial.println(waitCounts);

do
{


//**********************************************************
//Battery Measurements and Calculations
//Teensy can't directly measure analog voltages over 3.3V; the battery goes up to 4.2V or a little higher
//There is a 2.7K and 10K divider to take the voltage down to a usable range.
//****************revist this to be sure it's set correctly****************

//expected functional battery range, 4.15V to 3.25V. Trip points are at the 1/3 and 2/3 points of this voltage range
if (batt_ADC_count >= 941) { //3.855V
digitalWrite(yellowPin, LOW);
digitalWrite(redPin, LOW);

digitalWrite(greenPin, HIGH);
delay(10);
digitalWrite(greenPin, LOW);
delay(990);
}
else if (batt_ADC_count <= 941 && batt_ADC_count >= 868) {
digitalWrite(greenPin, LOW);
digitalWrite(redPin, LOW);

digitalWrite(yellowPin, HIGH);
delay(10);
digitalWrite(yellowPin, LOW);
delay(990);
}

else if (batt_ADC_count <= 868) { //3.556V
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);

digitalWrite(redPin, HIGH);
delay(10);
digitalWrite(redPin, LOW);
delay(990);

}

waitSeconds--;
Serial.print(waitSeconds);
Serial.print(" ");


} while (waitSeconds > 0);


//Calculate how many days have passed since January 1
if (currentMonth == 1) {
Today_in_Days = currentDay;
}
else if (currentMonth == 2) {
Today_in_Days = (currentDay + 31);
}
else if (currentMonth == 3) {
Today_in_Days = (currentDay + 59);
}
else if (currentMonth == 4) {
Today_in_Days = (currentDay + 90);
}
else if (currentMonth == 5) {
Today_in_Days = (currentDay + 120);
}
else if (currentMonth == 6) {
Today_in_Days = (currentDay + 151);
}
else if (currentMonth == 7) {
Today_in_Days = (currentDay + 181);
}
else if (currentMonth == 8) {
Today_in_Days = (currentDay + 212);
}
else if (currentMonth == 9) {
Today_in_Days = (currentDay + 243);
}
else if (currentMonth == 10) {
Today_in_Days = (currentDay + 273);
}
else if (currentMonth == 11) {
Today_in_Days = (currentDay + 304);
}
else if (currentMonth == 12) {
Today_in_Days = (currentDay + 334);
}

//Leap year check
if (((currentYear % 400 == 0) || (currentYear % 4 == 0)) && (currentMonth >= 3)){
Today_in_Days = Today_in_Days + 1;
}

//lowbattery: //if the battery is getting dangerously low, load shed lighting system.
// //***************Need to figure out how to sleep for a while and check again to see if it's charged up.
// //oscillators will likely consume a lot of power*******************************
// if (batt_ADC_count < 434){
// analogWrite(whitePin1, 0);
// analogWrite(whitePin2, 0);
// analogWrite(redPin, 10);
// analogWrite(yellowPin, 0);
// analogWrite(greenPin, 0);
// delay(1000);
// analogWrite(redPin,0);
// delay(1000);
// goto lowbattery;
// }

//********************************************************serial output diagnostics***********************************

//Battery Measurements and Calculations
//Teensy can't directly measure analog voltages over 3.3V; the battery goes up to 4.2V or a little higher
//There is a 2.7K and 10K divider to take the voltage down to a usable range.
//****************revist this to be sure it's set correctly****************

batt_ADC_count = analogRead(battpin);
batt_voltage = (((batt_ADC_count / 1023.0) * 3.3) * (12.7 / 10.0));
Serial.println();
Serial.print("Voltage at Battery ");
Serial.print(batt_ADC_count);
Serial.print(" (");
Serial.print(batt_voltage);
Serial.print("V");
Serial.println(")");


//calculate how many minutes have passed since midnight
Today_in_Mins = ((now.hour() * 60) + now.minute());

//display calculated mins since midnight and days since Jan 1 for diagnostic purposes
Serial.print(F("Elapsed minutes today: "));
Serial.print(Today_in_Mins, DEC);
Serial.print(" (");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(")");
Serial.println();
Serial.print(F("Elapsed days this year: "));
Serial.print(Today_in_Days, DEC);
Serial.println();

//Display the turn off time in mins and also convert to (hours:mins) ****how to get rid of the floating point X.XX???****
Serial.print(F("Turn off Minute: "));
Serial.print(Off_Today, DEC);
off_hour = (Off_Today / 60.0);
Serial.print(" (");
Serial.print(off_hour);
Serial.print(":");
off_min = (((Off_Today / 60.0) - off_hour) * 60.0);
Serial.print(off_min);
Serial.print(")");
Serial.println();

//Display the turn on time in mins and also convert to (hours:mins) *****how to get rid of floating point X.XX??*****
Serial.print(F("Turn On Minute: "));
Serial.print(On_Bright_Today, DEC);
Serial.print(" (");
on_hour = (On_Bright_Today / 60.0);
Serial.print(on_hour);
Serial.print(":");
on_min = (((On_Bright_Today / 60.0) - on_hour) * 60.0);
Serial.print(on_min);
Serial.print(")");
Serial.println();

//Display the dim down from 100% to XX% in mins and also convert to (hours:mins) *****how to get rid of floating point X.XX??*****
Serial.print(F("Full Dimmed Minute: "));
Serial.print(On_Dim_Today, DEC);
Serial.print(" (");
on_dim_hour = (On_Dim_Today / 60.0);
Serial.print(on_dim_hour);
Serial.print(":");
on_dim_min = (((On_Dim_Today / 60.0) - on_dim_hour) * 60.0);
Serial.print(on_dim_min);
Serial.print(")");
Serial.println();
//***********************************************************************************************************************

//************************************************Light On or Off determination******************************************

//determine if minutes are between Civial dusk on time and Nautical dusk off times
//loop that will decrease brightness a percentage each minute from 100% to 10% across the time difference between
//civil and nautical dusk and 10% is the minimum.
if (Today_in_Mins >= On_Bright_Today && Today_in_Mins <= On_Dim_Today) {
Serial.println("Light bright-to-dim branch");
Serial.println(dim_mins);
Serial.println();
isItDaytime = 0;

if (dim_Value >= 10) { //Loop that dims the light every minute a percentage based on the time between Cival and Nautical dusk
analogWrite(whitePin1, dim_Value);
analogWrite(whitePin2, dim_Value);
Serial.print("Dim Value ");
Serial.print(dim_Value);
Serial.println();
Serial.print("Dim Percentage ");
Serial.print(dim_Percent);
Serial.println();

dim_Value = dim_Value - dim_Percent;

}
}
//determine if mintues are between civil dawn off time and civil dusk on time (Daytime)
//set lights to OFF during day
else if (Today_in_Mins >= Off_Today && Today_in_Mins < On_Bright_Today) {

analogWrite(whitePin1, 0);
analogWrite(whitePin2, 0);
dim_Value = 100;
isItDaytime = 1;
Serial.println("Light Off Branch");
Serial.println();
}
//otherwise, turn on the light at 10% brightness.
//***********this could be based on a dim setting that I choose based on the Moon brightness and light sensor depending on cloudiness
else {
Serial.println("Light Dim else branch");
Serial.println();
analogWrite(whitePin1, 10);
analogWrite(whitePin2, 10);
// pin, value, resolution
pwm.configure(30, 10, 8);
pwm.configure(9, 10, 8);


dim_Value = 100;
isItDaytime = 0;
}

//**********************************************************************************************************

//Load battery voltage at sunrise and sunset into variables to measure power consumed and gained

if (Today_in_Mins == On_Bright_Today) {

sunset_voltage = batt_voltage;

}

if (Today_in_Mins == Off_Today) {

sunrise_voltage = batt_voltage;

}


//***********************************************Solar Panel************************************************

if (isItDaytime == 1) {
//Solar Panel Measurements and Calculations
//Teensy can't directly measure analog voltages over 3.3V; the solar panel goes up to 7.7V open ckt
//There is a 26.8K and 20K divider to take the voltage down to a usable range.
solar_ADC_count = analogRead(solarpin);
solar_voltage = (((solar_ADC_count / 1023.0) * 3.3) * (46.8 / 20.0));
Serial.print("Voltage at Solar Panel ");
Serial.print(solar_ADC_count);
Serial.print(" (");
Serial.print(solar_voltage);
Serial.print("V");
Serial.println(")");
}

//************************SD CARD********************************

//Create/Open file
if(now.minute() % 10 == 0 ) { //write data every 10 mins

battery = SD.open("battery.txt", FILE_WRITE);

// if the file opened okay, write to it:
if (battery) {
Serial.println("Writing to Battery file...");
// Write to file
battery.print(currentDay);
battery.print(",");
battery.print(Today_in_Mins);
battery.print(",");
battery.print(Current_Year);
battery.print(",");
battery.print(batt_voltage);
battery.println();
battery.close(); // close the file
Serial.println("Done.");
Serial.println();
}
// if the file didn't open, print an error:
else {
Serial.println("error opening battery.txt");
}
}

if(isItDaytime == 1) { //if night, don't write to the SD Card
//Create/Open file
daylight = SD.open("daylight.txt", FILE_WRITE);

// if the file opened okay, write to it:
if (daylight) {
Serial.println("Writing to Solar file...");
// Write to file
daylight.print(currentDay);
daylight.print(",");
daylight.print(Today_in_Mins);
daylight.print(",");
daylight.print(Current_Year);
daylight.print(",");
daylight.print(solar_voltage);
daylight.println();
daylight.close(); // close the file
Serial.println("Done.");
Serial.println();
}
// if the file didn't open, print an error:
else {
Serial.println("error opening battery.txt");
}
}

if(Today_in_Mins == Off_Today) { //write to the SD Card at sundown and sunrise
//Create/Open file
v_consumed = SD.open("v_consumed.txt", FILE_WRITE);

// if the file opened okay, write to it:
if (v_consumed) {
Serial.println("Writing to v_consumed file...");
// Write to file
v_consumed.print(currentDay);
v_consumed.print(",");
v_consumed.print(Today_in_Mins);
v_consumed.print(",");
v_consumed.print(Current_Year);
v_consumed.print(",");
v_consumed.print(sunrise_voltage);
v_consumed.print(",");
v_consumed.print(sunset_voltage);
v_consumed.print(",");
Daily_V_Delta = sunset_voltage - sunrise_voltage;
v_consumed.print(Daily_V_Delta);
v_consumed.println();
v_consumed.close(); // close the file
Serial.println("Done.");
Serial.println();
}
// if the file didn't open, print an error:
else {
Serial.println("error opening battery.txt");
}

Snooze.sleep(config);
}





}
/******************BLUETOOTH STUFF******************************************/
/*!
This function is called whenever select ACI events happen
*/
/**************************************************************************/
//void aciCallback(aci_evt_opcode_t event)
//{
// switch(event)
// {
// case ACI_EVT_DEVICE_STARTED:
// Serial.println(F("Advertising started"));
// break;
// case ACI_EVT_CONNECTED:
// Serial.println(F("Connected!"));
// break;
// case ACI_EVT_DISCONNECTED:
// Serial.println(F("Disconnected or advertising timed out"));
// break;
// default:
// break;
// }
//}
//
///**************************************************************************/
///*!
// This function is called whenever data arrives on the RX channel
//*/
///**************************************************************************/
//void rxCallback(uint8_t *buffer, uint8_t len)
//{
// Serial.print(F("Received "));
// Serial.print(len);
// Serial.print(F(" bytes: "));
// for(int i=0; i<len; i++)
// Serial.print((char)buffer);
//
// Serial.print(F(" ["));
//
// for(int i=0; i<len; i++)
// {
// Serial.print(" 0x"); Serial.print((char)buffer, HEX);
// }
// Serial.println(F(" ]"));
//
// /* Echo the same data back! */
// uart.write(buffer, len);
//}
//
 

Attachments

  • Screen Shot 2019-01-25 at 12.04.52 AM.jpg
    Screen Shot 2019-01-25 at 12.04.52 AM.jpg
    91.2 KB · Views: 42
Status
Not open for further replies.
Back
Top