CLOCK_USED()
uint32_t tag;


static  int gasflag=0;
static  int stromflag=0;
CONTROL_START

ECMD_GLOBAL(gas_h, 1000,uint16_t);
ECMD_GLOBAL(gas_l, 100,uint16_t);
ECMD_GLOBAL(strom_h, 1000,uint16_t);
ECMD_GLOBAL(strom_l, 900,uint16_t);
ECMD_GLOBAL(wert, 0, uint16_t);
ECMD_GLOBAL(stromwert, 0, uint16_t);
ECMD_GLOBAL(stromz, 0,uint16_t);
ECMD_GLOBAL(zaehler_5,0, uint16_t);
ECMD_GLOBAL(zaehler_15, 0,uint16_t);
ECMD_GLOBAL(zaehler_15_l, 0,uint16_t);
ECMD_GLOBAL(zaehler_h, 0,uint16_t);
ECMD_GLOBAL(zaehler_h_log, 0,uint16_t);
GLOBAL(temp,int16_t,0);
GLOBAL(temp_str[5],char);
ECMD_GLOBAL(flag, 0,uint8_t);
GLOBAL(strom_str[5],char);
ECMD_GLOBAL(stromzw, 0,uint16_t);
ECMD_GLOBAL(zaehler_tag, 0,uint16_t); 
ECMD_GLOBAL(zaehler_tagl, 0,uint16_t);
//GASvar
ECMD_GLOBAL(gasz, 0,uint16_t);
ECMD_GLOBAL(gas_zaehler_5,0, uint16_t);
ECMD_GLOBAL(gas_zaehler_15, 0,uint16_t);
ECMD_GLOBAL(gas_zaehler_15_l, 0,uint16_t);
ECMD_GLOBAL(gas_zaehler_h, 0,uint16_t);
ECMD_GLOBAL(gas_zaehler_h_log, 0,uint16_t);
ECMD_GLOBAL(gas_zaehler_tag, 0,uint16_t); 
ECMD_GLOBAL(gas_zaehler_tagl, 0,uint16_t);
ECMD_GLOBAL(zaehler_uptime, 0,uint32_t);
ECMD_GLOBAL(gas_zaehler_uptime, 0,uint32_t);
ECMD_GLOBAL(z_stand_strom, 0,uint32_t);
ECMD_GLOBAL(z_stand_strom_analog, 0,uint32_t);
ECMD_GLOBAL(z_stand_gas, 0,uint32_t);
ECMD_GLOBAL(n, 0,uint32_t);
ECMD_GLOBAL(m, 50000,uint32_t);
ECMD_GLOBAL(zstand1008,0,uint16_t);
//ende gasvar
PORTD |= (1<<PORTD3); dnl Pull-UpWiderstand zuschalten


THREAD(disp)
TTY_CLEAR()
TTY_GOTO(0,0)
TTY_WRITE_TIME()
TTY_GOTO(0,10)
dnl TTY_WRITE(`"%5sC"', temp_str)
TTY_GOTO(1,0)
TTY_WRITE("Stromzaehler:%3d",stromz)
TTY_GOTO(2,0)
TTY_WRITE("Strom 5min=%d",zaehler_5)
stromzw=zaehler_5*100/15;
 itoa_fixedpoint(stromzw, 0, strom_str);
TTY_GOTO(3,0)
TTY_WRITE("Watth 5min=%s",strom_str)
THREAD_END(disp)

THREAD(dispr)
TTY_CLEAR()
TTY_GOTO(0,0)
TTY_WRITE("%04d-%02d-%02d", CLOCK_YEAR(), CLOCK_MONTH(), CLOCK_DAY())
TTY_GOTO(1,0)
TTY_WRITE_TIME()
TTY_GOTO(1,10)


TTY_GOTO(2,0)
 
TTY_WRITE("Strom 5min=%d",zaehler_5)
 TTY_GOTO(3,0)
 
TTY_WRITE("Strom Stunde=%d",zaehler_h)
THREAD_END(dispr)
THREAD(disptag)
TTY_CLEAR()
TTY_GOTO(0,0)
TTY_WRITE("%04d-%02d-%02d", CLOCK_YEAR(), CLOCK_MONTH(), CLOCK_DAY())
TTY_GOTO(0,11)
TTY_WRITE_TIME()
TTY_GOTO(1,0)
TTY_WRITE("Strom Tag=%5d",zaehler_tag)

TTY_GOTO(2,0)
 
TTY_WRITE("Strom Stunde=%d",zaehler_h)

THREAD_END(disptag)
THREAD(adc0)
  wert = ADC_GET(0);
  if ((wert > gas_h) && (gasflag==0)) {gasz++;gas_zaehler_15 ++;gas_zaehler_h ++;
	gas_zaehler_tag ++;z_stand_gas++;gas_zaehler_uptime++;gasflag=1;}
else
  if ((wert<gas_l) && (gasflag==1)) {gasflag=0;}
  THREAD_END(adc0)
THREAD(strom)
       
        
 
                // Pullup-Widerstaende aktivieren
                PORTD |= _BV(PORTD2); // activate pullup on D2
                DDRD &= ~_BV(DDD2);    // set D2 to input
 		DDRD &= ~_BV(DDD3);    // set D3 to input
                _EIMSK |= _BV(INT0);  // external interrupt request 0 enable
                _EICRA |= (1<<ISC01) | (0<<ISC00);// interrupt request the rising edge of INT0 generates an interrupt request.
		
THREAD_END(strom)


ON STARTUP DO
dnl THREAD_START(temploop)
THREAD_START(strom)
THREAD_RESTART(adc0)
        END
 ON (PIN_RISING(KEY)) DO
flag=flag+1;
END
 ON  (PIN_RISING(KEY) && flag==1) DO
 PIN_SET(p4);
 END
 ON ((PIN_RISING(KEY)) && flag==2) DO
 PIN_CLEAR(p4);
 END
 ON ((PIN_RISING(KEY)) && flag==3) DO
 PIN_CLEAR(p4);
flag=0;
 END

ON ((PIN_HIGH(p4)) && flag==1) DO
 THREAD_STOP(disptag)
THREAD_STOP(disp)
THREAD_START(dispr) END
 ON ((PIN_LOW(p4)) && flag==2) DO
 THREAD_STOP(disptag)
THREAD_STOP(dispr)
THREAD_START(disp) END
 ON ((PIN_LOW(p4)) && (flag==3) || flag==0) DO
 THREAD_START(disptag)
THREAD_STOP(dispr)
THREAD_STOP(disp) END

//GAS Strom alle 5 min


//GAS Strom alle 5 min


ON ONCE (CLOCK_MIN == 0) DO
 if (CLOCK_HOUR == 0) {
zaehler_tagl=zaehler_tag;
zaehler_tag = 0;
gas_zaehler_tagl=gas_zaehler_tag;
gas_zaehler_tag = 0;
} 
zaehler_5 = stromz;
stromz = 0;
zaehler_15_l=zaehler_15;
zaehler_15=0;
zaehler_h_log=zaehler_h;
zaehler_h=0;
gas_zaehler_5 = gasz;
gasz = 0;
gas_zaehler_15_l=gas_zaehler_15;
gas_zaehler_15=0;
gas_zaehler_h_log=gas_zaehler_h;
gas_zaehler_h = 0;

END
ON ONCE (CLOCK_MIN == 5) DO 
zaehler_5 = stromz;
stromz = 0; 
gas_zaehler_5 = gasz;
gasz = 0;

 

END
ON ONCE (CLOCK_MIN == 10) DO
zaehler_5 = stromz;
stromz = 0;
gas_zaehler_5 = gasz;
gasz = 0; 
END
ON ONCE (CLOCK_MIN == 15) DO
zaehler_5 = stromz;
stromz = 0;
zaehler_15_l=zaehler_15;
zaehler_15=0;
gas_zaehler_5 = gasz;
gasz = 0;
gas_zaehler_15_l=gas_zaehler_15;
gas_zaehler_15=0;

 

END
ON ONCE (CLOCK_MIN == 20) DO 
zaehler_5 = stromz;
stromz = 0; 
gas_zaehler_5 = gasz;
gasz = 0;

 

END
  ON ONCE (CLOCK_MIN == 25) DO
zaehler_5 = stromz;
stromz = 0; 
gas_zaehler_5 = gasz;
gasz = 0;

 

END
ON ONCE (CLOCK_MIN == 30) DO 
zaehler_5 = stromz;
stromz = 0;
zaehler_15_l=zaehler_15;
zaehler_15=0; 
gas_zaehler_5 = gasz;
gasz = 0;
gas_zaehler_15_l=gas_zaehler_15;
gas_zaehler_15=0;



END
ON ONCE (CLOCK_MIN == 35) DO
zaehler_5 = stromz;
stromz = 0; 
gas_zaehler_5 = gasz;
gasz = 0;



END
ON ONCE (CLOCK_MIN == 40) DO 
zaehler_5 = stromz;
stromz = 0; 
gas_zaehler_5 = gasz;
gasz = 0;

 

END
ON ONCE (CLOCK_MIN == 45) DO
zaehler_5 = stromz;
stromz = 0;
zaehler_15_l=zaehler_15;
zaehler_15=0; 
gas_zaehler_5 = gasz;
gasz = 0;
gas_zaehler_15_l=gas_zaehler_15;
gas_zaehler_15=0;

 

END
ON ONCE (CLOCK_MIN == 50) DO
zaehler_5 = stromz;
stromz = 0; 
gas_zaehler_5 = gasz;
gasz = 0;

 

END
ON ONCE (CLOCK_MIN == 55) DO
zaehler_5 = stromz;
stromz = 0;
gas_zaehler_5 = gasz;
gasz = 0;


END
 
CONTROL_END

ISR(INT0_vect)
{
        stromz++;
	zaehler_15++;
	zaehler_h++;
	zaehler_uptime++;
	zaehler_tag++;
	z_stand_strom++;
z_stand_strom_analog++;
zstand1008++;
if (zstand1008 == 252) {
z_stand_strom=z_stand_strom-2;
z_stand_strom_analog=z_stand_strom_analog-2;
zstand1008 = 0;
} 
while (n <= m) 
{
    
    n++;
}
n=0;
}

