Beispiel #1
0
void	LowPowerClass::idle(period_t period, adc_t adc, 
							            timer1_t timer1, timer0_t timer0)
{
	if (adc == ADC_OFF)	
	{
		ADCSRA &= ~(1 << ADEN);
		power_adc_disable();
	}

	if (timer1 == TIMER1_OFF)	power_timer1_disable();	
	if (timer0 == TIMER0_OFF)	power_timer0_disable();	
	
	if (period != SLEEP_FOREVER)
	{
		wdt_enable(period);
		WDTCR |= (1 << WDIE);	
	}
	
	lowPowerBodOn(SLEEP_MODE_IDLE);
	
	if (adc == ADC_OFF)
	{
		power_adc_enable();
		ADCSRA |= (1 << ADEN);
	}

	if (timer1 == TIMER1_OFF)	power_timer1_enable();	
	if (timer0 == TIMER0_OFF)	power_timer0_enable();	
}
//---------------------------------------------------------------------
void loop() 
{
  // comming from wake-up?
  //pinMode(DALLAS_SENSOR_PIN, OUTPUT);
  LaCrosse.setTxPinMode(OUTPUT);
  
  power_adc_enable();
  delay(500); // ms, needed for settling DS18B20
  
  //--- [0] Betriebsspannung auslesen  
  lngVcc          = getVcc();   // as long
  controller_VCC  = (lngVcc/1000.0);  // as float in Volt, not millivolts (sent as HUM !
  
  //--- [2] read Dallas-Sensor
  float theta = ReadSingleOneWireSensor(dallas);

  #ifdef USE_SOFT_SERIAL_FOR_DEBUG
    //--- debug-output-block
    //softSerial.print("Vcc: ");
    //softSerial.print( (float) controller_VCC, 1);
    //softSerial.print("   Vcc_read: ");
    //softSerial.print((long) lngVcc);
    //softSerial.print("   ");  
    softSerial.print("Feuchte: ");
    softSerial.print( (float) bodenfeuchte, 1);
    softSerial.print("    ");
    softSerial.print("Temp: ");
    softSerial.println( (float) theta, 1);
  #endif 
  
  //--- transfer measured values to LaCrosse-instance
  LaCrosse.bSensorId = SENSOR_ID;
  LaCrosse.t = theta;    //--- alias temperature;  
  LaCrosse.sendTemperature();
  
  LaCrosse.sleep(1);        /* 1 second, no power-reduction! */

  #ifdef USE_SEPARATE_BATTERIE_ID 
    LaCrosse.bSensorId = SENSORID_BATTERIE;
  #endif 
  
  //LaCrosse.h = bodenfeuchte/1000; //   controller_VCC;    
  //LaCrosse.sendHumidity();
  //LaCrosse.sleep(1);        /* 1 second, no power-reduction! */


  //--- preserve more power during sleep phase 
  pinMode(DALLAS_SENSOR_PIN, INPUT);  
  LaCrosse.setTxPinMode(INPUT);

  //--- switch AD-converter off
  power_adc_disable(); 

  //--- fall to deep powersave-sleep, see notes in comments and 
  Narcoleptic.delay_minutes(DEEP_SLEEP_MINUTES);

  //--- deep sleep or test?
  //delay(10000); // 10 Sec 
}
Beispiel #3
0
void cube_start(uint8_t unused)
{
	// Set sleep mode to lighter
	mode = old_mode;

	power_adc_enable();
	power_spi_enable();
	power_timer0_enable();
	power_timer1_enable();

	// Enable BLANK timer interrupt (starts SPI)
	TIMSK0 |= (1 << OCIE0A);
}
Beispiel #4
0
// check battery voltage during sleep
void system_check_battery(void) {
    // enable analog to digital converter
    power_adc_enable();

    // select bandgap as analog to digital input
    //   REFS1:0 =   00:  VREF pin as voltage reference
    //   MUX3:0  = 1110:  1.1v bandgap as input
    ADMUX = _BV(MUX3) | _BV(MUX2) | _BV(MUX1);

    // configure analog to digital converter
    // ADEN    =   1:  enable analog to digital converter
    // ADSC    =   1:  start ADC conversion now
    // ADPS2:0 = 100:  system clock / 16  (4 MHz / 4 = 125 kHz)
    ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADPS2);

    // wait for conversion to complete
    while(ADCSRA & _BV(ADSC));

    uint16_t adc_curr = 0, adc_prev = 0;
    int16_t  adc_err  = 0;
    uint8_t  adc_good = 0;

    // the analog-to-digital converter may take a while to converge
    while(adc_good < 4) {
       ADCSRA |= _BV(ADSC);            // start adc conversion
       while(ADCSRA & _BV(ADSC));      // wait for result
       adc_curr = ADC;                 // save current value
       adc_err = adc_prev - adc_curr;  // calculate error--
                                       // difference from previous value

       // if negligable error, result is good
       if(-1 <= adc_err && adc_err <= 1) {
           ++adc_good;    // count consecutative good results
       } else {
           adc_good = 0;  // otherwise reset result counter
       }

       adc_prev = adc_curr;  // save current value as previous value
    }

    // disable analog to digital converter
    ADCSRA = 0;  // disable ADC before power_adc_disable()
    power_adc_disable();

    // set or clear low battery flag as required
    if(adc_curr > 1024UL * 1100 / LOW_BATTERY_VOLTAGE) {
       system.status |= SYSTEM_LOW_BATTERY;
    } else {
       system.status &= ~_BV(SYSTEM_LOW_BATTERY);
    }
}
Beispiel #5
0
/*********************************************************************************************************
** Function name:           wakeUp
** Descriptions:            wakeUp
*********************************************************************************************************/
void xadow::wakeUp()
{
#if defined(__AVR_ATmega32U4__)
    power_adc_enable();
    power_usart0_enable();
    power_spi_enable();
    power_twi_enable();
    power_timer1_enable();
    power_timer2_enable();
    power_timer3_enable();
    power_usart1_enable();
    power_usb_enable();
#endif
}
Beispiel #6
0
void setup() {
    fatal_set_blink(blink);
    DDRD |= LED_ALL | DEBUG_PIN | IOBIT_MASK;
    PORTD = LED_ALL;
    setup_timers(F_CPU);
    uart_setup(BAUDRATE);
    uart_send_all(3, online_packet);
    power_adc_enable();
    DDRB |= 0x3f;  //  direction, output
    DDRC |= 0x3f;  //  step, output
    PORTC &= ~0x3f;  //  Start low
    PORTB &= ~0x3f;  //  Start low
    MCUCR &= ~(1 << PUD);
    after(0, main_loop, 0);
    //  turn off red and yellow
    PORTD &= ~(LED_RED | LED_YELLOW);
}
Beispiel #7
0
void	Low_Power::idle(Period_t period, ADC_t adc,
                        Timer4_t timer4, Timer3_t timer3,
                        Timer1_t timer1, Timer0_t timer0,
                        SPI_t spi, USART1_t usart1,	TWI_t twi, usb_t usb)
{
    if (adc == ADC_OFF)
    {
        ADCSRA &= ~(1 << ADEN);
        power_adc_disable();
    }

    if (timer4 == TIMER4_OFF)	power_timer4_disable();
    if (timer3 == TIMER3_OFF)	power_timer3_disable();
    if (timer1 == TIMER1_OFF)	power_timer1_disable();
    if (timer0 == TIMER0_OFF)	power_timer0_disable();
    if (spi == SPI_OFF)				power_spi_disable();
    if (usart1 == USART1_OFF)	power_usart1_disable();
    if (twi == TWI_OFF)				power_twi_disable();
    if (usb == USB_OFF)				power_usb_disable();

    if (period != SLEEP_FOREVER)
    {
        wdt_enable(period);
        WDTCSR |= (1 << WDIE);
    }

    lowPowerBodOn(SLEEP_MODE_IDLE);

    if (adc == ADC_OFF)
    {
        power_adc_enable();
        ADCSRA |= (1 << ADEN);
    }

    if (timer4 == TIMER4_OFF)	power_timer4_enable();
    if (timer3 == TIMER3_OFF)	power_timer3_enable();
    if (timer1 == TIMER1_OFF)	power_timer1_enable();
    if (timer0 == TIMER0_OFF)	power_timer0_enable();
    if (spi == SPI_OFF)				power_spi_enable();
    if (usart1 == USART1_OFF)	power_usart1_enable();
    if (twi == TWI_OFF)				power_twi_enable();
    if (usb == USB_OFF)				power_usb_enable();
}
Beispiel #8
0
void adc_init() {
	#if DEBUG_L(1)
	fprintf_P(stderr,PSTR("\nadc: init"));
	#endif
	power_adc_enable();

	//Set Voltage to AVCC with external capacitor at AREF pin
	ADMUX|= (uint8_t)(1<<REFS0);
	ADMUX&=(uint8_t)~(1<<REFS1);
	//ADMUX&=~(1<<ADLAR); // Default disabled
	
	// Enable ADC, Inturupt, Trigger mode and set prescaler
	//ADCSRA=(((1<<ADEN)|(1<<ADIE)|(1<<ADATE))&0b11111000)|(ADC_PRESCALE);
	ADCSRA|= (uint8_t)(1<<ADEN)|(1<<ADIE)|(1<<ADATE);
	ADCSRA = (uint8_t)(ADCSRA & 0b11111000)|((uint8_t)ADC_PRESCALE);
	
	// Enable Free Running Mode 
	ADCSRB|= (1<<7); //reserved bit.
	ADCSRB&= (uint8_t)~(0b111); //(ADTS2:0)=0
	
	// Disable Digital reads from analog pins
	DIDR0 |= (uint8_t)((1<<ADC4D)|(1<<ADC5D)|(1<<ADC6D)|(1<<ADC7D));
	
	set_sleep_mode(SLEEP_MODE_ADC);
	#if DEBUG_L(3)
	fprintf_P(stderr,PSTR("\nadc: init: setup convertions"));
	#endif
	adc_set_channel(curr_ch);
	//Start the convertions
	ADCSRA|= (1<<ADSC);

	// Wait one adc clock cycle and change the channel, done by interupt later.
	_delay_loop_2(ADC_CYCLE_DELAY);
	adc_set_channel(++curr_ch);
	
	// Wait for one set of convertions to complete.
	//_delay_loop_2(ADC_CYCLE_DELAY*26);
	#if DEBUG_L(1)
	fprintf_P(stderr,PSTR("\t[done]"));
	#endif
}
Beispiel #9
0
// returns the battery voltage in 10mV units
// for instance: get_battery_voltage() returning 278 equals a voltage of 2.78V
uint16_t get_battery_voltage(void)
{
	power_adc_enable();

	ADMUX = _B0(REFS1) | _B1(REFS0)	// AVCC with external capacitor at AREF pin
#ifndef PREC_BATT_VOLTAGE
			| _BV(ADLAR)			// left adjust ADC - drops the two LSBs
#endif
			| 0b11110;				// measure 1.1v internal reference

	ADCSRA = _BV(ADEN)					// enable ADC
			| _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0)	// prescaler 128
			| _BV(ADSC);				// start conversion

	// wait for the conversion to finish
	loop_until_bit_is_set(ADCSRA, ADIF);

	// remember the result
#ifdef PREC_BATT_VOLTAGE
	uint16_t adc_result = ADC;
#else
	uint8_t adc_result = ADCH;
#endif

	// clear the ADIF bit by writing one
	SetBit(ADCSRA, ADIF);

	ADCSRA = 0;				// disable ADC

	power_adc_disable();	// ADC power off

#ifdef PREC_BATT_VOLTAGE
	return 112640 / adc_result;
#else
	return 28050 / adc_result;
#endif
}
Beispiel #10
0
void	Low_Power::idle(Period_t period, ADC_t adc, Timer5_t timer5,
                        Timer4_t timer4, Timer3_t timer3, Timer2_t timer2,
                        Timer1_t timer1, Timer0_t timer0, SPI_t spi,
                        USART3_t usart3, USART2_t usart2, USART1_t usart1,
                        USART0_t usart0, TWI_t twi)
{
    // Temporary clock source variable
    unsigned char clockSource = 0;

    if (timer2 == TIMER2_OFF)
    {
        if (TCCR2B & CS22) clockSource |= (1 << CS22);
        if (TCCR2B & CS21) clockSource |= (1 << CS21);
        if (TCCR2B & CS20) clockSource |= (1 << CS20);

        // Remove the clock source to shutdown Timer2
        TCCR2B &= ~(1 << CS22);
        TCCR2B &= ~(1 << CS21);
        TCCR2B &= ~(1 << CS20);

        power_timer2_disable();
    }

    if (adc == ADC_OFF)
    {
        ADCSRA &= ~(1 << ADEN);
        power_adc_disable();
    }

    if (timer5 == TIMER5_OFF)	power_timer5_disable();
    if (timer4 == TIMER4_OFF)	power_timer4_disable();
    if (timer3 == TIMER3_OFF)	power_timer3_disable();
    if (timer1 == TIMER1_OFF)	power_timer1_disable();
    if (timer0 == TIMER0_OFF)	power_timer0_disable();
    if (spi == SPI_OFF)			  power_spi_disable();
    if (usart3 == USART3_OFF)	power_usart3_disable();
    if (usart2 == USART2_OFF)	power_usart2_disable();
    if (usart1 == USART1_OFF)	power_usart1_disable();
    if (usart0 == USART0_OFF)	power_usart0_disable();
    if (twi == TWI_OFF)			  power_twi_disable();

    if (period != SLEEP_FOREVER)
    {
        wdt_enable(period);
        WDTCSR |= (1 << WDIE);
    }

    lowPowerBodOn(SLEEP_MODE_IDLE);

    if (adc == ADC_OFF)
    {
        power_adc_enable();
        ADCSRA |= (1 << ADEN);
    }

    if (timer2 == TIMER2_OFF)
    {
        if (clockSource & CS22) TCCR2B |= (1 << CS22);
        if (clockSource & CS21) TCCR2B |= (1 << CS21);
        if (clockSource & CS20) TCCR2B |= (1 << CS20);

        power_timer2_enable();
    }

    if (timer5 == TIMER5_OFF)	power_timer5_enable();
    if (timer4 == TIMER4_OFF)	power_timer4_enable();
    if (timer3 == TIMER3_OFF)	power_timer3_enable();
    if (timer1 == TIMER1_OFF)	power_timer1_enable();
    if (timer0 == TIMER0_OFF)	power_timer0_enable();
    if (spi == SPI_OFF)			  power_spi_enable();
    if (usart3 == USART3_OFF)	power_usart3_enable();
    if (usart2 == USART2_OFF)	power_usart2_enable();
    if (usart1 == USART1_OFF)	power_usart1_enable();
    if (usart0 == USART0_OFF)	power_usart0_enable();
    if (twi == TWI_OFF)			  power_twi_enable();
}
Beispiel #11
0
// seed the random number generator with entropy from the temperature,
// voltage reading, and microseconds since boot.
// this method is still most effective when called semi-randomly such
// as after a user hits a button to start a game or other semi-random
// events
void Arduboy::initRandomSeed()
{
  power_adc_enable(); // ADC on
  randomSeed(~rawADC(ADC_TEMP) * ~rawADC(ADC_VOLTAGE) * ~micros() + micros());
  power_adc_disable(); // ADC off
}
Beispiel #12
0
/*******************************************************************************
* Name: idle
* Description: Putting microcontroller into idle state. Please make sure you 
*			   understand the implication and result of disabling module.
*
* Argument  	Description
* =========  	===========
* 1. period     Duration of low power mode. Use SLEEP_FOREVER to use other wake
*				up resource:
*				(a) SLEEP_15MS - 15 ms sleep
*				(b) SLEEP_30MS - 30 ms sleep
*				(c) SLEEP_60MS - 60 ms sleep
*				(d) SLEEP_120MS - 120 ms sleep
*				(e) SLEEP_250MS - 250 ms sleep
*				(f) SLEEP_500MS - 500 ms sleep
*				(g) SLEEP_1S - 1 s sleep
*				(h) SLEEP_2S - 2 s sleep
*				(i) SLEEP_4S - 4 s sleep
*				(j) SLEEP_8S - 8 s sleep
*				(k) SLEEP_FOREVER - Sleep without waking up through WDT
*
* 2. adc		ADC module disable control:
*				(a) ADC_OFF - Turn off ADC module
*				(b) ADC_ON - Leave ADC module in its default state
*
* 3. timer2		Timer 2 module disable control:
*				(a) TIMER2_OFF - Turn off Timer 2 module
*				(b) TIMER2_ON - Leave Timer 2 module in its default state
*
* 4. timer1		Timer 1 module disable control:
*				(a) TIMER1_OFF - Turn off Timer 1 module
*				(b) TIMER1_ON - Leave Timer 1 module in its default state
*
* 5. timer0		Timer 0 module disable control:
*				(a) TIMER0_OFF - Turn off Timer 0 module
*				(b) TIMER0_ON - Leave Timer 0 module in its default state
*
* 6. spi		SPI module disable control:
*				(a) ADC_OFF - Turn off ADC module
*				(b) ADC_ON - Leave ADC module in its default state
*
* 7. usart0		USART0 module disable control:
*				(a) USART0_OFF - Turn off USART0  module
*				(b) USART0_ON - Leave USART0 module in its default state
*
* 8. twi		TWI module disable control:
*				(a) TWI_OFF - Turn off TWI module
*				(b) TWI_ON - Leave TWI module in its default state
*
*******************************************************************************/
void	LowPowerClass::idle(period_t period, adc_t adc, timer2_t timer2, 
							timer1_t timer1, timer0_t timer0,
							spi_t spi, usart0_t usart0,	twi_t twi)
{
	// Temporary clock source variable 
	unsigned char clockSource = 0;
	
	if (timer2 == TIMER2_OFF)
	{
		if (TCCR2B & CS22) clockSource |= (1 << CS22);
		if (TCCR2B & CS21) clockSource |= (1 << CS21);
		if (TCCR2B & CS20) clockSource |= (1 << CS20);
	
		// Remove the clock source to shutdown Timer2
		TCCR2B &= ~(1 << CS22);
		TCCR2B &= ~(1 << CS21);
		TCCR2B &= ~(1 << CS20);
		
		power_timer2_disable();
	}
	
	if (adc == ADC_OFF)	
	{
		ADCSRA &= ~(1 << ADEN);
		power_adc_disable();
	}
	
	if (timer1 == TIMER1_OFF)	power_timer1_disable();	
	if (timer0 == TIMER0_OFF)	power_timer0_disable();	
	if (spi == SPI_OFF)			power_spi_disable();
	if (usart0 == USART0_OFF)	power_usart0_disable();
	if (twi == TWI_OFF)			power_twi_disable();
	
	if (period != SLEEP_FOREVER)
	{
		wdt_enable(period);
		WDTCSR |= (1 << WDIE);	
	}
	
	lowPowerBodOn(SLEEP_MODE_IDLE);
	
	if (adc == ADC_OFF)
	{
		power_adc_enable();
		ADCSRA |= (1 << ADEN);
	}
	
	if (timer2 == TIMER2_OFF)
	{
		if (clockSource & CS22) TCCR2B |= (1 << CS22);
		if (clockSource & CS21) TCCR2B |= (1 << CS21);
		if (clockSource & CS20) TCCR2B |= (1 << CS20);
		
		power_timer2_enable();
	}
	
	if (timer1 == TIMER1_OFF)	power_timer1_enable();	
	if (timer0 == TIMER0_OFF)	power_timer0_enable();	
	if (spi == SPI_OFF)			power_spi_enable();
	if (usart0 == USART0_OFF)	power_usart0_enable();
	if (twi == TWI_OFF)			power_twi_enable();
}
int main()
{



	if (_is_docked()){
		for(int j=0;(j<10);j++)
			for(int i=0;(i<200);i++)
				_delay_ms(5);
	}
				
	scounter=0;
	//Initialized data buffer
	dataIndex=0;
	dataSubindex=0;
	// Blink green for 5 seconds	

	_wocket_initialize();
		

	AC_NUMS=_SAMPLING_RATE *60;
	power_adc_disable();
  	power_spi_disable();
  	power_timer0_disable();
  	power_timer1_disable();
  	power_twi_disable();


	while(1){
			

			

		//Sample only in the main loop because of p
		if(sampleFlag){			
			power_adc_enable();
			_atmega_adc_turn_on();
			sampleFlag=0;
#ifdef _VERSION ==3




			x=_atmega_a2dConvert10bit(ADC0);
		
			y=_atmega_a2dConvert10bit(ADC1);

			z=_atmega_a2dConvert10bit(ADC2);
			//x=y=z=cc++;
			//if (cc>=1024)
			//	cc=0;

				vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2);
			

				if (_wPC>40){	//Skip the first samples						
					if (summary_count==0)
					{
						vmag=vmag/24;
						if (vmag>65535)
							acount[ci]=65535;
						else
							acount[ci]=(unsigned short) vmag;
				 		vmag=0;
						++ci;
						if (ci==AC_BUFFER_SIZE)
							ci=0;
						cseq++;
		
						if (ci==si)
						{
							si++;
							if (si==AC_BUFFER_SIZE)
								si=0;
							sseq++;
						}
						acount[ci]=0;
						summary_count=AC_NUMS;
					}else
						summary_count--;
				}
				else if (_wPC==40)
					vmag=0;

			
#else
			//x=_atmega_a2dConvert10bit(ADC3);
			//y=_atmega_a2dConvert10bit(ADC2);
			//z=_atmega_a2dConvert10bit(ADC1);		
#endif
		

		
			 m_SET_X(data[dataIndex],x,dataSubindex);
			 m_SET_Y(data[dataIndex],y,dataSubindex);
			 m_SET_Z(data[dataIndex],z,dataSubindex);

			 dataSubindex++;
			 if (dataSubindex>=4)
			 	dataSubindex=0;
		

			 
			 //Most of the time the data buffer with 750 will not overflow
			 //and will be enough to transmit the data, data will go from 0 up to a specific
			 //value

			if (_wTM==_TM_Continuous)
			{
									
				switch(dataSubindex){
				case 1:
						m_GET_X(x,data[dataIndex].byte1,data[dataIndex].byte2,0);
						m_GET_Y(y,data[dataIndex].byte2,data[dataIndex].byte3,0);
						m_GET_Z(z,data[dataIndex].byte3,data[dataIndex].byte4,0);
						break;
				case 2:
						m_GET_X(x,data[dataIndex].byte4,data[dataIndex].byte5,1);
						m_GET_Y(y,data[dataIndex].byte6,data[dataIndex].byte7,1);
						m_GET_Z(z,data[dataIndex].byte7,data[dataIndex].byte8,1);
						break;
				case 3:
						m_GET_X(x,data[dataIndex].byte8,data[dataIndex].byte9,2);
						m_GET_Y(y,data[dataIndex].byte9,data[dataIndex].byte10,2);
						m_GET_Z(z,data[dataIndex].byte11,data[dataIndex].byte12,2);
						break;
				case 0:
						m_GET_X(x,data[dataIndex].byte12,data[dataIndex].byte13,3);
						m_GET_Y(y,data[dataIndex].byte13,data[dataIndex].byte14,3);
						m_GET_Z(z,data[dataIndex].byte14,data[dataIndex].byte15,3);
						break;
				}
											
		
				if (justconnected==1)
				{
					_send_tm();
					justconnected=2;
				}		
				//_send_pdu(x,y,z);
				_send_uncompressed_pdu(x, y, z);

				
					//Send summary activity count
				/*	for (int i=0;(i<summaryindex);i++){
						_send_summary_count(acount[i]);
						acount[i]=0;
					}
					if (summaryindex<AC_BUFFER_SIZE){
						acount[0]=acount[summaryindex];
						summaryindex=0;
					}*/

			}
			else 
			{
				if ((dataSubindex==0) && (batch_counter<750))
					batch_counter++;
				if (connected){
					_greenled_turn_on();
						
					gotack=1;
					tester=0;

					if (_wTM==_TM_Continuous)
						continue;
						
					for (int ixz=0;(ixz<100);ixz++) {                                                                                      
       						_bluetooth_transmit_uart0_byte(0xff); 

							
						if (sampleFlag)
							{
								sampleFlag=0;

								x=_atmega_a2dConvert10bit(ADC0);
		
								y=_atmega_a2dConvert10bit(ADC1);

								z=_atmega_a2dConvert10bit(ADC2);


								vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2);
			

								if (_wPC>40){	//Skip the first samples						
									if (summary_count==0)
									{
										vmag=vmag/24;
										if (vmag>65535)
											acount[ci]=65535;
										else
											acount[ci]=(unsigned short) vmag;
			 							vmag=0;
										++ci;
										if (ci==AC_BUFFER_SIZE)
											ci=0;
										cseq++;
	
										if (ci==si)
										{
											si++;
											if (si==AC_BUFFER_SIZE)
												si=0;
											sseq++;
										}
										acount[ci]=0;
										summary_count=AC_NUMS;
									}else
										summary_count--;
								}
								else if (_wPC==40)
									vmag=0;

			

		
			 					m_SET_X(data[dataIndex],x,dataSubindex);
			 					m_SET_Y(data[dataIndex],y,dataSubindex);
			 					m_SET_Z(data[dataIndex],z,dataSubindex);

			 					dataSubindex++;
			 					if (dataSubindex>=4)
			 					dataSubindex=0;
							}						
					}

					_send_fv();
					_send_sr();					 
					_send_tm();
					_send_batch_count((batch_counter-1)*4);																	
					_send_acs();
					//Send summary activity count
					/*for (int i=0;(i<summaryindex);i++){
						_send_summary_count(acount[i]);
						acount[i]=0;
					}
					if (summaryindex<AC_BUFFER_SIZE){
						acount[0]=acount[summaryindex];
						summaryindex=0;
					}*/

					
					if ((batch_counter>0) && (batch_counter<750)) // Go from 0 up to batch_counter
					{						
						for (int i=0;(i<(batch_counter-1));i++)
						{
							m_GET_X(x,data[i].byte1,data[i].byte2,0);
							m_GET_Y(y,data[i].byte2,data[i].byte3,0);
							m_GET_Z(z,data[i].byte3,data[i].byte4,0);							
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[i].byte4,data[i].byte5,1);
							m_GET_Y(y,data[i].byte6,data[i].byte7,1);
							m_GET_Z(z,data[i].byte7,data[i].byte8,1);							
							//_send_uncompressed_pdu(x,y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[i].byte8,data[i].byte9,2);
							m_GET_Y(y,data[i].byte9,data[i].byte10,2);
							m_GET_Z(z,data[i].byte11,data[i].byte12,2);
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[i].byte12,data[i].byte13,3);
							m_GET_Y(y,data[i].byte13,data[i].byte14,3);
							m_GET_Z(z,data[i].byte14,data[i].byte15,3);							
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							_receive_data();


							if (sampleFlag)
							{
								sampleFlag=0;

								x=_atmega_a2dConvert10bit(ADC0);
		
								y=_atmega_a2dConvert10bit(ADC1);

								z=_atmega_a2dConvert10bit(ADC2);


								vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2);
			

								if (_wPC>40){	//Skip the first samples						
									if (summary_count==0)
									{
										vmag=vmag/24;
										if (vmag>65535)
											acount[ci]=65535;
										else
											acount[ci]=(unsigned short) vmag;
			 							vmag=0;
										++ci;
										if (ci==AC_BUFFER_SIZE)
											ci=0;
										cseq++;
	
										if (ci==si)
										{
											si++;
											if (si==AC_BUFFER_SIZE)
												si=0;
											sseq++;
										}
										acount[ci]=0;
										summary_count=AC_NUMS;
									}else
										summary_count--;
								}
								else if (_wPC==40)
									vmag=0;

			

		
			 					m_SET_X(data[dataIndex],x,dataSubindex);
			 					m_SET_Y(data[dataIndex],y,dataSubindex);
			 					m_SET_Z(data[dataIndex],z,dataSubindex);

			 					dataSubindex++;
			 					if (dataSubindex>=4)
			 					dataSubindex=0;
							}
						}

						
						if (batch_counter>0){
						//copy end item into start
						data[0].byte1=data[batch_counter].byte1;
						data[0].byte2=data[batch_counter].byte2;
						data[0].byte3=data[batch_counter].byte3;
						data[0].byte4=data[batch_counter].byte4;
						data[0].byte5=data[batch_counter].byte5;
						data[0].byte6=data[batch_counter].byte6;
						data[0].byte7=data[batch_counter].byte7;
						data[0].byte8=data[batch_counter].byte8;
						data[0].byte9=data[batch_counter].byte9;
						data[0].byte10=data[batch_counter].byte10;
						data[0].byte11=data[batch_counter].byte11;
						data[0].byte12=data[batch_counter].byte12;
						data[0].byte13=data[batch_counter].byte13;
						data[0].byte14=data[batch_counter].byte14;
						data[0].byte15=data[batch_counter].byte15;
						}


					}else{

						int current=dataIndex+1;
						int end =dataIndex;
						if (current>=750)
							current=0;
						while(current!=end)
						{
							m_GET_X(x,data[current].byte1,data[current].byte2,0);
							m_GET_Y(y,data[current].byte2,data[current].byte3,0);
							m_GET_Z(z,data[current].byte3,data[current].byte4,0);							
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[current].byte4,data[current].byte5,1);
							m_GET_Y(y,data[current].byte6,data[current].byte7,1);
							m_GET_Z(z,data[current].byte7,data[current].byte8,1);							
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[current].byte8,data[current].byte9,2);
							m_GET_Y(y,data[current].byte9,data[current].byte10,2);
							m_GET_Z(z,data[current].byte11,data[current].byte12,2);
							//_send_uncompressed_pdu(x, y, z);
							
							_send_pdu(x,y,z);

							m_GET_X(x,data[current].byte12,data[current].byte13,3);
							m_GET_Y(y,data[current].byte13,data[current].byte14,3);
							m_GET_Z(z,data[current].byte14,data[current].byte15,3);							
							//_send_uncompressed_pdu(x,y, z);
							
							_send_pdu(x,y,z);

							current++;
							if (current==750)
								current=0;

							_receive_data();


							if (sampleFlag)
							{
								sampleFlag=0;

								x=_atmega_a2dConvert10bit(ADC0);
		
								y=_atmega_a2dConvert10bit(ADC1);

								z=_atmega_a2dConvert10bit(ADC2);


								vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2);
			

								if (_wPC>40){	//Skip the first samples						
									if (summary_count==0)
									{
										vmag=vmag/24;
										if (vmag>65535)
											acount[ci]=65535;
										else
											acount[ci]=(unsigned short) vmag;
			 							vmag=0;
										++ci;
										if (ci==AC_BUFFER_SIZE)
											ci=0;
										cseq++;
	
										if (ci==si)
										{
											si++;
											if (si==AC_BUFFER_SIZE)
												si=0;
											sseq++;
										}
										acount[ci]=0;
										summary_count=AC_NUMS;
									}else
										summary_count--;
								}
								else if (_wPC==40)
									vmag=0;

			

		
			 					m_SET_X(data[dataIndex],x,dataSubindex);
			 					m_SET_Y(data[dataIndex],y,dataSubindex);
			 					m_SET_Z(data[dataIndex],z,dataSubindex);

			 					dataSubindex++;
			 					if (dataSubindex>=4)
			 					dataSubindex=0;
							}
							
						}

						//copy end item into start
						data[0].byte1=data[end].byte1;
						data[0].byte2=data[end].byte2;
						data[0].byte3=data[end].byte3;
						data[0].byte4=data[end].byte4;
						data[0].byte5=data[end].byte5;
						data[0].byte6=data[end].byte6;
						data[0].byte7=data[end].byte7;
						data[0].byte8=data[end].byte8;
						data[0].byte9=data[end].byte9;
						data[0].byte10=data[end].byte10;
						data[0].byte11=data[end].byte11;
						data[0].byte12=data[end].byte12;
						data[0].byte13=data[end].byte13;
						data[0].byte14=data[end].byte14;
						data[0].byte15=data[end].byte15;
					}


			

					
					batch_counter=0;
					dataIndex=0;
					seconds_passed=0;
					while (seconds_passed<400)
					{
						_delay_ms(5);
						seconds_passed++;
						_receive_data();

						if (sampleFlag)
							{
								sampleFlag=0;

								x=_atmega_a2dConvert10bit(ADC0);
		
								y=_atmega_a2dConvert10bit(ADC1);

								z=_atmega_a2dConvert10bit(ADC2);


								vmag+=Filter(x,0)+Filter(y,1)+Filter(z,2);
			

								if (_wPC>40){	//Skip the first samples						
									if (summary_count==0)
									{
										vmag=vmag/24;
										if (vmag>65535)
											acount[ci]=65535;
										else
											acount[ci]=(unsigned short) vmag;
			 							vmag=0;
										++ci;
										if (ci==AC_BUFFER_SIZE)
											ci=0;
										cseq++;
	
										if (ci==si)
										{
											si++;
											if (si==AC_BUFFER_SIZE)
												si=0;
											sseq++;
										}
										acount[ci]=0;
										summary_count=AC_NUMS;
									}else
										summary_count--;
								}
								else if (_wPC==40)
									vmag=0;

			

		
			 					m_SET_X(data[dataIndex],x,dataSubindex);
			 					m_SET_Y(data[dataIndex],y,dataSubindex);
			 					m_SET_Z(data[dataIndex],z,dataSubindex);

			 					dataSubindex++;
			 					if (dataSubindex>=4)
			 					dataSubindex=0;
							}						
					}						
					//connected=0;

					//Don't turn off the radio if a request to switch mode has been received
					if (_wTM==_TM_Continuous)
						_bluetooth_turn_on();	
					else
						_bluetooth_turn_off();		
					
					command_counter=0;
					seconds_disconnected=0;
					_greenled_turn_off();
				
				}
			}
			_atmega_adc_turn_off();
			power_adc_disable();

			if ((dataSubindex==0) && (!connected))
				dataIndex++;			
			if (dataIndex==750)
				dataIndex=0;
			connected=0;			
			
		}	
		
			cli();
			set_sleep_mode(SLEEP_MODE_IDLE);
			//set_sleep_mode(SLEEP_MODE_PWR_SAVE);
    		sleep_enable();
    		sleep_bod_disable(); 	
    		sei();
    		sleep_cpu();
    		sleep_disable();


	}

	return 0;
}