Ejemplo n.º 1
0
void setup() {
#ifdef DEBUG
  Serial.begin(9600);
#endif

  pinMode(RELAY_SW_OUT, OUTPUT);
  pinMode(CMD_MAN_IN, INPUT_PULLUP);

  // Setup watchdog timeout to 8 s (mode 9)
  setupWatchdog(WD_TICK_TIME);

  // Watchdog interrupt count before reading light value
  // example: wd count * wd interval == 4 * 8 s = 32 s
  // Start with "enable" time tick and then check the current time
  wdMatch = (uint16_t) WD_WAIT_EN_TIME / WD_TICK_TIME;

  // Setup Pin change interrupt on PCINT1
  GIMSK |= _BV(PCIE);
  PCMSK |= _BV(PCINT1);

  // Set the internal registers to reduce power consumes
  ACSR   = (1 << ACD);                  // Shut down the analog comparator
  MCUCR |= (1 << BODS);                 // BOD disabled

  // I2C begin() is called BEFORE sensors library "begin" methods:
  // it is called just once for all the sensors.
  bus.begin();

  // Sensors initialization
  // Light sensor
  BH1750.powerOn();
  BH1750.setMode(BH1750_CONTINUOUS_HIGH_RES_MODE_2);
  BH1750.setMtreg(200);                 // Set measurement time register to high value
  delay(100);
  BH1750.sleep();                       // Send light sensor to sleep

  // Real time clock
  setSyncProvider(timeProvider);        // Pointer to function to get the time from the RTC
  setSyncInterval(WD_WAIT_DIS_TIME);    // Set system time at every sensor read

  // Update drift info from RTC
  di = RTC.read_DriftInfo();
  di.DriftDays = 1000;                  // RTC drift in seconds/day. 18 s per day is 18000/1000
  di.DriftSeconds = 27857;
  RTC.write_DriftInfo(di);
  setDriftInfo(di);
}
void System::init()
{
	wdt_reset();
	pinMode(INFO_LED_PIN, OUTPUT);
	lastRadioUpdate = 0;
	Serial.begin(9600);
	delay(20);

	SPI.begin();
	initializeRadio();
	accelerometer.init();
	compass.init();
	if(!barometer.begin()) {
		packet.setBarometerErrorFlag();
	}
	setupWatchdog();
	attachInterrupt(digitalPinToInterrupt(2), anemometerOnRisingEdge, RISING);
}
Ejemplo n.º 3
0
int main(void)
{
	//factory settings is to divide internal clock 8MHz by 8.
	//don't, and just run at 8 MHz (set the clock divider to 1 so no effect)
	CLKPR = (1<<CLKPCE);
	CLKPR = 0; // Divide by 1
	
	//PB1 out, PB5 & PB0 in
	DDRB = 0x02;
	//Pullup on PB0 % PB5
	PORTB = (1 << PORTB5) | (1 << PORTB0);
	//just make sure pullups are NOT disabled
	MCUCR |= (0 << PUD);
	
	setupWatchdog();
	
	setup7seg();
	
	int16_t vSpeed = 1000;
	while(1)
	{
		/*
		//1000 run makes it nearly 20 sec
		showWaitAnimation(vSpeed);
		
		vSpeed -= 20;
		if (vSpeed <= 0){
			vSpeed = 1000;
		}
		*/
		
		if (mRemainingOnDuration > ONE_SEC * 60){
			//more convert the seconds to minutes ('1+' to show remaining minutes rounded up)
			uint16_t vMinutes = 1+ (uint16_t)mRemainingOnDuration / (ONE_SEC * 60);
			//mul by 10 to shift for the display only (shows digits _XX_)
			vMinutes *= 10;
			showNumber(vMinutes, 1,2,false);
		}
		else {
			if (mRemainingOnDuration > ONE_SEC * 20){
				//until 20 sec is countdown : show seconds as is
				showNumber((uint16_t)mRemainingOnDuration, 1,2,false);
			}
			else {
				if (mRemainingOnDuration > ONE_SEC){
					//until last sec put the animation
					//1000 run makes it nearly 20 sec
					vSpeed = 1000;
					showWaitAnimation(vSpeed);
							
					vSpeed -= 20;
				}
				else {
					//zero!
					clearDisplay();
				}
			}
		}
		
		
		//check for button press : PB0 -> Add 3 minutes
		if ((~PINB & (1 << PINB0)) != 0){
			clearDisplay();
			//pushed button : turns on if not already anyway and add and extra 3 minutes
			mRemainingOnDuration +=ONE_SEC * 180;
			//max is 10 minutes
			if (mRemainingOnDuration > MAX_DURATION_SEC){
				mRemainingOnDuration = MAX_DURATION_SEC;
			}
			turnOn();
			
			//wait while pressed and wait again 1/2 sec (debouncing) 
			while ((~PINB & (1 << PINB0)) != 0);
			_delay_ms(300);
		}
		
		
		//check for button press : PB5 -> Stop now !
		if ((~PINB & (1 << PINB5)) != 0){
			clearDisplay();
			//pushed button : turns off
			mRemainingOnDuration = 0;
			turnOff();
					
			//wait while pressed and wait again 1/2 sec (debouncing)
			while ((~PINB & (1 << PINB5)) != 0);
			_delay_ms(300);
		}
	}
}