Example #1
0
void system_init(){

	// Setup Pins
	pinMode(PIN_LED, OUTPUT);

	// Serial
	Serial.begin(SERIAL_SPEED);
	while(!Serial);
	
	// Startup Message
	LOG("\n\nUP! "); LOG(PROJECT_NAME); LOG("\n");

	// Setup HeartBeat
	heartBeat.setInterval(500);
	heartBeat.onRun(heartBeat_callback);

	// Setup Serial Debug
	serialDebug.setInterval(10);
	serialDebug.onRun(serialDebug_callback);

	// Battery Checker
	batteryChecker.setInterval(4000);
	batteryChecker.onRun(batteryChecker_callback);

	// Add Threads to System
	System.add(&heartBeat);
	System.add(&serialDebug);
	System.add(&batteryChecker);
};
void loop(){
  // run ThreadController
  // this will check every thread inside ThreadController,
  // if it should run. If yes, he will run it;
  controll.run();
  // Rest of code
 
}
void setup(){

  Wire.begin();
  Serial.begin(9600);
  delay(100);

  accelerometer->setupADXL345();
  gyroscope->setupL3G4200D(250); // Configure L3G4200  - 250, 500 or 2000 deg/sec
  magnetometer->setupHMC5883();
  barometricPressure->setupBMP085Calibration();

  delay(200);

  // Configure accelerometroThread
  accelerometroThread->onRun(accelerometerCallback);
  accelerometroThread->setInterval(SENSOR_INTERVAL);

  // Configure gyroscopeThread
  gyroscopeThread->onRun(gyroscopeCallback);
  gyroscopeThread->setInterval(SENSOR_INTERVAL);

  // Configurate magnetometerThread
  magnetometerThread->onRun(magnetometerCallback);
  magnetometerThread->setInterval(SENSOR_INTERVAL);

  // Configurate barometricPressureThread
  barometricPressureThread->onRun(barometricPressureCallback);
  barometricPressureThread->setInterval(SENSOR_INTERVAL);

  // Configure allSensorResultsThread
  // allSensorResultsThread->onRun(allSensorResultsCallback);
  // allSensorResultsThread->setInterval(SENSOR_INTERVAL);

  // Configurate Madgwick algorithm
  madgwickQuaternionThread->onRun(madgwickQuaternionCallback);
  madgwickQuaternionThread->setInterval(FILTER_INTERVAL);


  // Adds both threads to the sensor controllers of threads
  controll.add(accelerometroThread);
  controll.add(gyroscopeThread);
  controll.add(magnetometerThread);
  controll.add(barometricPressureThread);
  controll.add(madgwickQuaternionThread);
  //controll.add(allSensorResultsThread);
}
void loop()
{
    /* Runs the non RT controller */
    controller.run();

    // Serial.print(" Temperature milieu: ");
    // Serial.print(humidTempSensor.getTemp());
    // Serial.print(" Humidite milieu: ");
    // Serial.print(humidTempSensor.getHumidity());
    // Serial.print(" Temperature Bas: ");
    // Serial.println(lowSensor.getAverageValue());
    // Serial.println();

    // lcd.cursor();
    // lcd.clear();
    lcd.setCursor(7,0);
    digitalClockDisplay(now());
    lcd.setCursor(0,1);
    lcd.print("Up : ");
    lcd.print(highSensor.getTemp());
    lcd.print((char)223);
    lcd.print('C');
    // lcd.setCursor(0,2);
    // lcd.print("Mid : ");
    // lcd.print(humidTempSensor.getTemp());
    // lcd.print((char)223);
    // lcd.print('C');
    lcd.setCursor(0,2);
    lcd.print("Down : ");
    lcd.print(lowSensor.getTemp());
    lcd.print((char)223);
    lcd.print('C');

    // fogRelay.on();
    // lightRelay.on();
    // // heatRelay.on();
    // delay(5000);
    // fogRelay.off();
    // lightRelay.off();
    // // heatRelay.off();
    lcd.setCursor(0,1);
    Alarm.delay(1000);
}
void setup()
{

    tmElements_t currentTime;

    currentTime.Second = 0;
    currentTime.Minute = 35;
    currentTime.Hour =18;
    currentTime.Wday = 5;
    currentTime.Day = 17;
    currentTime.Month = 4;
    currentTime.Year = 44;

    // RTC.set(makeTime(currentTime));

    Serial.begin(9600);
    Serial.println("TerraMonitor starting ...");
    Wire.begin(); 

    // analogReference(INTERNAL);

    Serial.print("EEPROM HumidityController size occupied (in bytes) : ");
    Serial.println(EEPROM_HUMIDITY_CONTROLLER_SIZE);

    setSyncProvider(RTC.get);   // the function to get the time from the RTC    
    if(timeStatus()!= timeSet) 
        Serial.println("Unable to sync with the RTC");
    else
        Serial.println("RTC has set the system time");

    /* Controller Alarm init */
    humidityController.initAlarms();
    lightController.initAlarms();

    /* read the sensor every s */
    lowSensor.setInterval(5000);
    highSensor.setInterval(5000);
    /* read the sensor every 10s */
    humidTempSensor.setInterval(10000); 
    /* run temp controller every s */
    tempController.setInterval(1000);

    /* add the thread to the controller */
    controller.add(&lowSensor);
    controller.add(&highSensor);
    controller.add(&humidTempSensor);
    controller.add(&tempController);
    
    Timer1.initialize(100000);
    Timer1.attachInterrupt(timerCallback);

    // Alarms
    //Alarm.alarmRepeat(12,00,0, MorningAlarm);  // 8:30am every day
    // Alarm.alarmRepeat(14,48,0, MorningAlarm);  // 8:30am every day
    //Alarm.alarmRepeat(17,30,0, MorningAlarm);  // 8:30am every day
    //Alarm.alarmRepeat(22,15,0, MorningAlarm);  // 8:30am every day

    humidityController.disableAllAlarms();
    humidityController.setAlarm(0, 12, 0, 0, 30);
    humidityController.setAlarm(1, 14, 0, 0, 15);
    humidityController.setAlarm(2, 16, 0, 0, 15);
    humidityController.setAlarm(3, 20, 0, 0, 15);
    humidityController.setAlarm(4, 22, 15, 0, 20);

    // humidityController.setAlarm(4, 16, 10, 0, 10);

    lightController.setStartTime(11,30,0);
    lightController.setStopTime(23,30,0);

    tempController.setMaxUpperTemp(29);
    tempController.setMinDayLowerTemp(21);
    tempController.setMinNightLowerTemp(20);
    tempController.setMinNightUpperTemp(21);

    // extractFan.startFan();
    // sideFan.startFan();
    // frontFan.startFan();

    lcd.init();                      // initialize the lcd 
 
    // Print a message to the LCD.
    lcd.backlight();

}