diff --git a/GUI_Windows/Arduino/NanolabV1.1/NanolabV1.1.ino b/GUI_Windows/Arduino/NanolabV1.1/NanolabV1.1.ino index aa88631..ac0ef42 100644 --- a/GUI_Windows/Arduino/NanolabV1.1/NanolabV1.1.ino +++ b/GUI_Windows/Arduino/NanolabV1.1/NanolabV1.1.ino @@ -3,7 +3,7 @@ #include // Required for 16 MHz Adafruit Trinket #endif -int pin = 6; +int pin = 9; int numPixels =16; @@ -30,7 +30,7 @@ void setup() { Serial.begin(9600); - pixels->fill(pixels->Color(214, 83, 211), 0, 15); //this is the colour the lights glow + pixels->fill(pixels->Color(214, 83, 211), 0, 16); //this is the colour the lights glow //i found the rgb on reddit if its not optimal than ¯\_(ツ)_/¯ @@ -61,8 +61,12 @@ void loop() { case 'C' : pixels->clear(); pixels->show(); - - + pixels->clear(); + pixels->show(); + pixels->clear(); + pixels->show(); + pixels->clear(); + pixels->show(); break; diff --git a/TIME_TO_DUAL/TIME_TO_DUAL.ino b/TIME_TO_DUAL/TIME_TO_DUAL.ino index 169fb87..880bbee 100644 --- a/TIME_TO_DUAL/TIME_TO_DUAL.ino +++ b/TIME_TO_DUAL/TIME_TO_DUAL.ino @@ -19,9 +19,9 @@ int pump = 16; int fan = 17; -unsigned long timeNow = 0; +unsigned long bootSeconds = 0; -unsigned long timeLast = 0; +unsigned long bootSecondsLast = 0; //Time start Settings: @@ -45,143 +45,202 @@ int dailyErrorBehind = 0; // set the average number of milliseconds your microco int correctedToday = 1; // do not change this variable, one means that the time has already been corrected today for the error in your boards crystal. This is true for the first day because you just set the time when you uploaded the sketch. +///////////////////////////////////////////////// +// declare time-tracking functions for later use +///////////////////////////////////////////////// +bool incrSeconds(); +bool incrMinutes(); +bool incrHours(); +bool incrDays(); + +/////////////////////////////////////////////////// +// NOTE: I changed indentation to be clearer below +/////////////////////////////////////////////////// void setup() { // put your setup code here, to run once: -pinMode(pump, OUTPUT); + pinMode(pump, OUTPUT); -pixels = new Adafruit_NeoPixel(numPixels, pin, pixelFormat); + pixels = new Adafruit_NeoPixel(numPixels, pin, pixelFormat); -pixels->begin(); + pixels->begin(); -Serial.begin(115200); + Serial.begin(115200); -pixels->fill(pixels->Color(84, 64, 205)); //this is the colour the lights glow - //i found the rgb on reddit if its not optimal than ¯\_(ツ)_/¯ + pixels->fill(pixels->Color(84, 64, 205)); //this is the colour the lights glow + //i found the rgb on reddit if its not optimal than ¯\_(ツ)_/¯ } void loop() { // put your main code here, to run repeatedly: -timeNow = millis()/1000; // the number of milliseconds that have passed since boot - -seconds = timeNow - timeLast; - -//the number of seconds that have passed since the last time 60 seconds was reached. - -if (seconds == 60) { - -timeLast = timeNow; - -minutes = minutes + 1; } + // NOTE: millis() is a special system function + // + // "Returns the number of milliseconds passed since the Arduino + // board began running the current program. This number will overflow + // (go back to zero), after approximately 50 days." + // -//if one minute has passed, start counting milliseconds from zero again and add one minute to the clock. + // update the total seconds after boot. (NOTE: okay that it stores same value on most loops) + bootSeconds = millis()/1000; // keep always-correct count on booted seconds, but it will roll back to zero -if (minutes == 60){ + // + // update "event" flags for use later + // + bool newSecond = incrSeconds(); + bool newMinute = newSecond && incrMinutes(); + bool newHour = newMinute && incrHours(); + bool newDay = newHour && incrDays(); -minutes = 0; + // NOW: runs only *once* for each new second, minute, hour, day + // if (newSecond) .... + // if (newMinute) .... + // if (newHour) .... + // if (newDay) .... -hours = hours + 1; } + /////////////////////////////////////////////////////////////////////////////////// + // vvvvvvvvvvvvvvvvvvvvv CHANGE BELOW TO USE EVENT-FLAGS vvvvvvvvvvvvvvvvvvvvv + /////////////////////////////////////////////////////////////////////////////////// + //if 24 hours have passed, add one day -// if one hour has passed, start counting minutes from zero and add one hour to the clock - - -if (hours == 24){ - -hours = 0; - -days = days + 1; - -} + if (hours ==(24 - startingHour) && correctedToday == 0){ -//if 24 hours have passed, add one day + delay(dailyErrorFast*1000); -if (hours ==(24 - startingHour) && correctedToday == 0){ + seconds = seconds + dailyErrorBehind; -delay(dailyErrorFast*1000); + correctedToday = 1; } -seconds = seconds + dailyErrorBehind; + //every time 24 hours have passed since the initial starting time + // and it has not been reset this day before, add milliseconds or + // delay the program with some milliseconds. -correctedToday = 1; } + //Change these varialbes according to the error of your board. -//every time 24 hours have passed since the initial starting time and it has not been reset this day before, add milliseconds or delay the program with some milliseconds. + // The only way to find out how far off your boards internal clock is, + // is by uploading this sketch at exactly the same time as the real time, + // letting it run for a few days and then determining how many + // seconds slow/fast your boards internal clock is on a daily + // average. (24 hours). -//Change these varialbes according to the error of your board. + // Let the sketch know that a new day has started for what concerns + // correction, if this line was not here the arduiono would continue + // to correct for an entire hour that is 24 - startingHour. -// The only way to find out how far off your boards internal clock is, is by uploading this sketch at exactly the same time as the real time, letting it run for a few days + if (hours == 24 - startingHour + 2) { -// and then determining how many seconds slow/fast your boards internal clock is on a daily average. (24 hours). + correctedToday = 0; } -if (hours == 24 - startingHour + 2) { - -correctedToday = 0; } - -//let the sketch know that a new day has started for what concerns correction, if this line was not here the arduiono // would continue to correct for an entire hour that is 24 - startingHour. + //let the sketch know that a new day has started for what concerns + // correction, if this line was not here the arduiono would continue + // to correct for an entire hour that is 24 - startingHour. switch (hours) { - case 4: - while (minutes >= Ptime){ //turns the pump on for whatever Ptime is = too - digitalWrite(pump, HIGH); - } - digitalWrite(pump, LOW); - - break; - - case 6: //this turns the light ring on - pixels->setBrightness(255); - pixels->show(); - break; - - case 8: //fan on - digitalWrite(fan, HIGH); - break; - + case 4: + /// FIXME: DANGER DANGER DANGER. loop() is already a good-enough loop!!! + // + // ALL SUB-LOOPS MUST *START* AND *END* THEMSELVES WITH ONLY IN-LOOP CODE ALONE. + // Repeated actions in a loop need very good reason to BLOCK all other code. + // + // Most repeated things normally work by *sharing* one huge "event-loop" and turning on + // and off what is needed to repeat. These "on" and "off" decisions are then all that are + // needed to do correctly. No more sub-loops are needed unless one task requires + // everything else wait. + // + // NOTE: any across-loop-iteration memory must be declared *outside* the loop() function to + // stay the same. Anything declared inside the loop() function is forgotten each time. + // + // Many activities can all *share* one large "event-loop" to keep any housekeeping activities. + // Things we think of can use it: Delays, Spacing work out, Detecting big events. One big loop. + //////// + while (minutes >= Ptime){ //turns the pump on for whatever Ptime is = too + digitalWrite(pump, HIGH); + Ptime ++; + } + digitalWrite(pump, LOW); + + break; + + case 6: //this turns the light ring on + pixels->setBrightness(255); + pixels->show(); + break; + + case 8: //fan on + digitalWrite(fan, HIGH); + break; + + + case 18: //this turns the light ring off + pixels->setBrightness(0); + pixels->show(); + break; - case 18: //this turns the light ring off - pixels->setBrightness(0); - pixels->show(); - break; + } - } + if (Serial.available() > 0) { //Serial functions, it waits for the serial port to be avilible + int DoThis = Serial.read(); // then switches case based on set the character sent - if (Serial.available() > 0) { //Serial functions, it waits for the serial port to be avilible - int DoThis = Serial.read(); // then switches case based on set the character sent + switch (DoThis) { - switch (DoThis) { + case 'L' : + pixels->setBrightness(50); + pixels->show(); + break; - case 'L' : - pixels->setBrightness(50); - pixels->show(); - break; + case 'T' : + Serial.print("The time is: "); - case 'T' : - Serial.print("The time is: "); + Serial.print(days); - Serial.print(days); + Serial.print(":"); - Serial.print(":"); + Serial.print(hours); - Serial.print(hours); + Serial.print(":"); - Serial.print(":"); + Serial.print(minutes); - Serial.print(minutes); + Serial.print(":"); - Serial.print(":"); + Serial.println(seconds); + break; - Serial.println(seconds); - break; + } } +} - } +bool incrSeconds() // the number of milliseconds that have passed since boot +{ + // same second? + if (bootSeconds == bootSecondsLast) return false; //// RETURN false + ++seconds; // jump normal seconds forward + ++bootSecondsLast; // jump *handled* bootSeconds forward + return true; +} +bool incrMinutes() +{ + // same minute? + if (seconds < 60) return false; //// RETURN false + ++minutes; + seconds -= 60; // grab out one minute from seconds + return true; } +bool incrHours() +{ + /// try writing this using "minutes" "hours" and 60 +} +bool incrDays() +{ + /// try writing this using "hours" "days" and 24 +} diff --git a/basic_hydrofuge_schedule/basic_hydrofuge_schedule.ino b/basic_hydrofuge_schedule/basic_hydrofuge_schedule.ino new file mode 100644 index 0000000..422648c --- /dev/null +++ b/basic_hydrofuge_schedule/basic_hydrofuge_schedule.ino @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include "SCMD.h" +#include "SCMD_config.h" //Contains #defines for common SCMD register names and values +#include "Wire.h" + + +int fan = 8; //fan pin + +int pump = 6; //pump pin + +int numPixels = 16; //Led / neopixel settings +int led = 9; +int pixelFormat = NEO_GRB + NEO_KHZ800; + +Adafruit_NeoPixel *pixels; + +AlarmId id; + +SCMD myMotorDriver; //This creates the main object of one motor driver and connected peripherals. + +void setup() { + Serial.begin(9600); + + pinMode(8, INPUT_PULLUP); //Use to halt motor movement (ground) + pinMode(pump, OUTPUT); + pinMode(fan, OUTPUT); + + pixels = new Adafruit_NeoPixel(numPixels, led, pixelFormat); + pixels->begin(); + + //*****initialize the driver get wait for idle*****// + while ( myMotorDriver.begin() != 0xA9 ) //Wait until a valid ID word is returned + { + Serial.println( "ID mismatch, trying again" ); + delay(500); + } + Serial.println( "ID matches 0xA9" ); + + setTime(5, 59, 40, 1, 1, 11); // set time to Saturday 8:29:00am Jan 1 2011 + + // create the alarms, to trigger at specific times + Alarm.alarmRepeat(4, 0, 0, pumpOn); + Alarm.alarmRepeat(5, 0, 0, fanOn); + Alarm.alarmRepeat(6, 0, 0, ledOn); + Alarm.alarmRepeat(18, 0, 0, ledFanOff); +} + +void loop() { + //load bearing void loop dont ask why idfk +} + + +// functions to be called when an alarm triggers: +void pumpOn() { + //pump needs to turn on 30s and then wait to let the plant soak up water + //and then run in reverse to drain +} + + +void fanOn() { +} + +void ledOn() { + pixels->fill(pixels->Color(214, 83, 211), 0, 15); + pixels->show(); + Serial.println("turn lights on"); +} + +void ledFanOff() { + pixels->clear(); + pixels->show(); + + pixels->clear(); + pixels->show(); + digitalWrite(fan, LOW); + Serial.println("turn lights off"); +} + + +void printDigits(int digits) { + Serial.print(":"); + if (digits < 10) + Serial.print('0'); + Serial.print(digits); +} \ No newline at end of file