/* * File: main.c * Authors: Cory Baxes and Tim Stroup * Project: Green Machine (Chlamydomonas Reinhardtii and/or Chlorella) by Chatfield Senior High School * * Created on October 6, 2014, 12:53 PM * Modified on December 12, 2014, 1:27 PM */ #include #include int lightsOn = 0; void checkSystems(void) { if (powerDriverA.isFailure()) { dataLog.add("Error with Power Driver A on initialization.", 900); } if (!camera.isOk()) { dataLog.add("Error with camera on initialization.", 901); } /*if (!co2.status()) { dataLog.add("Error with Hydrogen sensor on initialization.", 31); }*/ } void startAgitation(void) { // turn the Hydrogen sensor off to save power for motor //co2.off(); powerDriverA.on(); } void stopAgitation(void) { powerDriverA.off(); // turn the Hydrogen sensor back on //co2.on(); } void cycleLights(void) { if (lightsOn == 0) { ledR.dutycycle(75); ledB.dutycycle(75); lightsOn = 1; } else { ledR.dutycycle(0); ledB.dutycycle(0); lightsOn = 0; } } void logHydrogen(void) { // turn Hydrogen sensor back on for reading //co2.on(); // read Hydrogen sensor value. 10 readings 100ms apart unsigned hydrogenValue = co2.get(10,100); // turn off Hydrogen sensor during power intensive operations //co2.off(); // log the Hydrogen sensor value dataLog.add("Hydrogen sensor value = ", hydrogenValue); } Boolean takePicture(void) { char filename[32] = {0}; sprintf(filename, "%s.jpg", dateTime.getStamp()); // the ':' character is an invalid character, so it needs to be changed String temp = filename; while(*temp) { if(*temp == ':') *temp = '.'; ++temp; } camera.on(); wait(1000); return camera.getPix(filename); camera.off(); } void storeLightSensor(void) { Uint valQ1 = resistiveSensors.getQ1(10,100); Uint valQ2 = resistiveSensors.getQ2(10,100); Uint valQ3 = resistiveSensors.getQ3(10,100); Uint valQ4 = resistiveSensors.getQ4(10,100); //char time[128] = {""}; //sprintf(time, "Time: %s", dateTime.getStamp()); //dataLog.add(time, 0); dataLog.add("Quadrant 1 = ", valQ1); dataLog.add("Quadrant 2 = ", valQ2); dataLog.add("Quadrant 3 = ", valQ3); dataLog.add("Quadrant 4 = ", valQ4); } void storeLightSensorQ1(void) { Uint valQ1 = resistiveSensors.getQ1(10,100); char time[128] = {""}; sprintf(time, "Time: %s", dateTime.getStamp()); dataLog.add(time, 0); dataLog.add("Quadrant 1 = ", valQ1); } void storeValues(void) { usb.disconnect(); // log the start of the data cycle dataLog.add("Data cycle readings at ", dateTime.getStamp()); // turn off Hydrogen sensor during power intensive operations //co2.off(); //take reading storeLightSensor(); wait(500); if (lightsOn == 1) { if(takePicture()) // log if error occurred while attempting to take picture { char temp[128] = {0}; sprintf(temp,"Error taking picture at time: %s ", dateTime.getStamp()); dataLog.add(temp, 48); } } else { dataLog.add("No picture taken due to Light Cycle at ", dateTime.getStamp()); } //wait(2000); //logHydrogen(); wait(500); usb.connect(); // turn Hydrogen sensor back on //co2.on(); } int main_camera(void) { // Initializes NESI modules nesi.init(); usb.connect(); wait(1000); Keystroke key = KEYSTROKE_NONE; while(1) { // get keystroke key = button.getStroke(); if(key == KEYSTROKE_SINGLE) // if button is tapped once { // disable button keystroke detection button.disableStroke(); // disconnect USB SD card (in case routine writes to SD memory) usb.eject(); char temp[128] = {0}; if(takePicture()) // log if error occurred while attempting to take picture sprintf(temp,"Error taking picture at time: %s ", dateTime.getStamp()); else sprintf(temp,"Successfully took picture at time: %s ", dateTime.getStamp()); dataLog.add(temp, 0x1010); // reconnect USB SD card (to read files) usb.connect(); // enable button keystroke detection button.enableStroke(); while(1) { usb.process(); } } } return 0; } int main_DEBUG(void) { // initialize all modules nesi.init(); /*while (1) { cycleLights(); wait(1000); }*/ /*cycleLights(); usb.eject(); wait(5000); storeLightSensorQ1(); wait(3000); storeLightSensorQ1(); wait(3000); storeLightSensorQ1(); wait(3000); storeLightSensorQ1(); wait(3000); storeLightSensorQ1(); wait(3000); cycleLights(); */ /*usb.connect(); wait(1000); while(1) { usb.process(); }*/ powerDriverA.on(); return 0; } //ACTUAL MAIN FOR PROGRAM int main(void) { // initialize all modules nesi.init(); // Define timestamp variable DateAndTime timeTemp; DateAndTime lastTime; // checks Camera, Power Driver B, and hydrogen sensor for problems checkSystems(); // start the Hydrogen sensor //co2.on(); // connects to USB host usb.connect(); // CYCLES while(1) { timeTemp = dateTime.get(); // ORIGINAL CYCLE VALUES // LOG DATA: if((timeTemp.hour % 1) == 0) // AGITATION CYCLE: if((timeTemp.minute % 30) == 0) // LIGHT CYCLE: if((timeTemp.hour % 12) == 0) // light cycle every 12 hours if((timeTemp.hour % 12) == 0 && timeTemp.minute == 0 && (timeTemp.second == 0 || (lastTime.second == 59 && timeTemp.second != 59))) { cycleLights(); } // log data every hour if((timeTemp.hour % 1) == 0 && timeTemp.minute == 0 && (timeTemp.second == 0 || (lastTime.second == 59 && timeTemp.second != 59))) { storeValues(); } // agitation cycle every 30 minutes if((timeTemp.minute % 30) == 0 && (timeTemp.second == 0 || (lastTime.second == 59 && timeTemp.second != 59))) { startAgitation(); // let it agitate for 60 seconds // CHANGE TO 60 SECONDS wait(60000); stopAgitation(); } usb.process(); lastTime = timeTemp; } return 0; }