Hello. I'm having trouble with programing my arduino and need some help.
I need my Attiny13a (using arduino UNO as ISP) to be sleeping (for power saving), wake every 1800 seconds, beeb a piezo buzzer and go back to sleep.
I need it to be doing that "forever".
I stop it only by disconecting the power source and thats fine.
I've been looking for some code and implementation, but i just cant get it right.
At the moment my code doesnt include the sleep and wdt and the power consumation is around 7mA that lasts my AA battery for 13 days running 24/7. As much as i've read i can get it to use micro amps with this method.
Thanks in advance.
At the moment my code ...
What code would that be?
Please use code tags.
Read this before posting a programming question
How to use this forum
Sorry, 1st post. 
Anyway my current code is
void setup()
{
pinMode(4, OUTPUT);
}
void loop() {
int i;
int a;
int j;
int b;
/* pisk za opozorilo */
digitalWrite(4, HIGH);
delay(20);
digitalWrite(4, LOW);
/* pisk za pit 30 min */
do
{
for(i=0;i<20000;i++)
{
for(j=0;j<20000;j++);
}
digitalWrite(4, HIGH);
delay(20);
digitalWrite(4, LOW);
}
while(1);
}
I did a project using an ATtiny85 a little while back: Gammon Forum : Electronics : Microprocessors : Simple project - torch-locator
That flashes an LED and sleeps in between.
Plus a simpler one which just flashes an LED without checking the light level. That one is still going 2 years later, running on a CR2032 button battery.

It's not the same processor, but that should give you some ideas.
Thank you for your help.
I've tried to upload a code i found on a page you provided
#include <avr/sleep.h>
#include <avr/wdt.h>
const byte LED = 9;
void flash ()
{
pinMode (LED, OUTPUT);
for (byte i = 0; i < 10; i++)
{
digitalWrite (LED, HIGH);
delay (50);
digitalWrite (LED, LOW);
delay (50);
}
pinMode (LED, INPUT);
} // end of flash
// watchdog interrupt
ISR (WDT_vect)
{
wdt_disable(); // disable watchdog
} // end of WDT_vect
void setup () { }
void loop ()
{
flash ();
// disable ADC
ADCSRA = 0;
// clear various "reset" flags
MCUSR = 0;
// allow changes, disable reset
WDTCSR = bit (WDCE) | bit (WDE);
// set interrupt mode and an interval
WDTCSR = bit (WDIE) | bit (WDP3) | bit (WDP0); // set WDIE, and 8 seconds delay
wdt_reset(); // pat the dog
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
noInterrupts (); // timed sequence follows
sleep_enable();
// turn off brown-out enable in software
MCUCR = bit (BODS) | bit (BODSE);
MCUCR = bit (BODS);
interrupts (); // guarantees next instruction executed
sleep_cpu ();
// cancel sleep as a precaution
sleep_disable();
} // end of loop
And it errored saying MCUSR and others were not declared in that segment. Is is the chip's fault or is there a library that aint included? I'm using attiny13a with arduino uno as isp.
Thank you.
The chip's fault? I think not. As I said, it isn't the same processor. You will have to look at the datasheets and adapt slightly.
I still have problems, since i'm a begginer. So i tried uploading your code.
// ATtiny85 sleep mode, wake on pin change interrupt or watchdog timer
// Author: Nick Gammon
// Date: 12 October 2013
// ATMEL ATTINY 25/45/85 / ARDUINO
//
// +-\/-+
// Ain0 (D 5) PB5 1| |8 Vcc
// Ain3 (D 3) PB3 2| |7 PB2 (D 2) Ain1
// Ain2 (D 4) PB4 3| |6 PB1 (D 1) pwm1
// GND 4| |5 PB0 (D 0) pwm0
// +----+
#include <avr/sleep.h> // Sleep Modes
#include <avr/power.h> // Power management
#include <avr/wdt.h> // Watchdog timer
const byte LED = 3; // pin 2
// watchdog interrupt
ISR (WDT_vect)
{
wdt_disable(); // disable watchdog
} // end of WDT_vect
void resetWatchdog ()
{
// clear various "reset" flags
MCUSR = 0;
// allow changes, disable reset, clear existing interrupt
WDTCR = bit (WDCE) | bit (WDE) | bit (WDIF);
// set interrupt mode and an interval (WDE must be changed from 1 to 0 here)
[b]WDTCR = bit (WDIE) | bit (WDP2) | bit (WDP1) | bit (WDP0); // set WDIE, and 2 seconds delay[/b]
// pat the dog
wdt_reset();
} // end of resetWatchdog
void setup ()
{
resetWatchdog (); // do this first in case WDT fires
pinMode (LED, OUTPUT);
} // end of setup
void loop ()
{
digitalWrite (LED, HIGH);
delay (1);
digitalWrite (LED, LOW);
goToSleep ();
} // end of loop
void goToSleep ()
{
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
ADCSRA = 0; // turn off ADC
[b]power_all_disable ();[/b] // power off ADC, Timer 0 and 1, serial interface
noInterrupts (); // timed sequence coming up
resetWatchdog (); // get watchdog ready
sleep_enable (); // ready to sleep
interrupts (); // interrupts are required now
sleep_cpu (); // sleep
sleep_disable (); // precaution
[b]power_all_enable ();[/b] // power everything back on
} // end of goToSleep
Obviously it didnt work, so i deleted the code that errored when compiling. That code is marked on the original. I understand that the code doesnt work becouse i altered the wake time of wdt.
I've looked thru attiny13a data sheet in watchdog timer section and i hasnt able to figure out how to get the proper code for it to get the wdt waking processor on the longest time period of 8s.
The errors were basicly that the lines are not specified in the segment.
Thanks for your interest and help.
Where did you get the "core" files for the Attiny13a from?
i'm currently using
i've tried replacing the ATTINY13 folder and now the only line that doesnt pass compiling is
Arduino: 1.6.5 (Windows 8.1), Board: "Attiny 13A standalone 9.6Mhz"
Build options changed, rebuilding all
WDT.ino.ino: In function 'void goToSleep()':
WDT.ino:43: error: 'power_all_disable' was not declared in this scope
WDT.ino:50: error: 'power_all_enable' was not declared in this scope
'power_all_disable' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
After commenting the problematic lines, i tried your original code again and got this:
Arduino: 1.6.5 (Windows 8.1), Board: "Attiny 13A standalone 9.6Mhz"
Build options changed, rebuilding all
In file included from C:\Users\Matja§\Documents\Arduino\hardware\attiny13\avr\cores\core13/WProgram.h:24:0,
from C:\Users\Matja§\Documents\Arduino\hardware\attiny13\avr\cores\core13/Arduino.h:11,
from WDT.ino.ino:6:
WDT.ino.ino: In function 'void resetWatchdog()':
WDT.ino:16: error: 'WDIF' was not declared in this scope
C:\Users\Matja§\Documents\Arduino\hardware\attiny13\avr\cores\core13/wiring.h:70:25: note: in definition of macro 'bit'
#define bit(b) (1UL << (b))
^
WDT.ino:18: error: 'WDIE' was not declared in this scope
C:\Users\Matja§\Documents\Arduino\hardware\attiny13\avr\cores\core13/wiring.h:70:25: note: in definition of macro 'bit'
#define bit(b) (1UL << (b))
^
'WDIF' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
You need to compare the datasheets. My code was written for the Atmega328P. It looks like WDIF is named WDTIF in the Atmega13A. And WDIE becomes WDTIE. Compare the relevant pages in the datasheet, and if something looks like it is describing the same thing, but has a slightly different name, that is probably the name to go with.
Thank you very much, works perfectly. As i've said i'm just a newbie trying to make stuff happen without full understanding it. But your examples and advice is much appreciated and helped me greatly.
One more problem. 
#include <avr/sleep.h> // Sleep Modes
#include <avr/power.h> // Power management
#include <avr/wdt.h> // Watchdog timer
// watchdog interrupt
ISR (WDT_vect)
{
wdt_disable(); // disable watchdog
} // end of WDT_vect
void resetWatchdog ()
{
// clear various "reset" flags
MCUSR = 0;
// allow changes, disable reset, clear existing interrupt
WDTCR = bit (WDCE) | bit (WDE) | bit (WDTIF);
// set interrupt mode and an interval (WDE must be changed from 1 to 0 here)
WDTCR = bit (WDTIE) | bit (WDP3) | bit (WDP0); // set WDIE, and 8 seconds delay
// pat the dog
wdt_reset();
} // end of resetWatchdog
void setup ()
{
resetWatchdog (); // do this first in case WDT fires
pinMode(4, OUTPUT);
} // end of setup
void loop ()
{
digitalWrite (4, HIGH);
delay(20);
digitalWrite (4, LOW);
goToSleep ();
} // end of loop
void goToSleep ()
{
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
ADCSRA = 0; // turn off ADC
//power_all_disable (); // power off ADC, Timer 0 and 1, serial interface
noInterrupts (); // timed sequence coming up
resetWatchdog (); // get watchdog ready
sleep_enable (); // ready to sleep
interrupts (); // interrupts are required now
sleep_cpu (); // sleep
sleep_disable (); // precaution
// power_all_enable (); // power everything back on
} // end of goToSleep
So the code works and all, basicly my piezo beeper beeps every 8 seconds, but power consumption hasent changed. When running my circuit still pulls 8mA at all times.
So i'm confused, the only thing i changed were:
wtd wake up to 8 seconds and power_all_enable and power_all_disable were commented as in code.
Thank you.
Perhaps if you posted your schematic?
Sorry, ive been afk.
Well i got the code to work, and i have another question. I want my buzzer to go off every 30 minutes.
I've checked the forum and got some ideas, but nothing works like i would want it to.
Anyway thats my code now, beeps every 8 seconds when wtd runs out.
#include <avr/sleep.h> // Sleep Modes
#include <avr/power.h> // Power management
#include <avr/wdt.h> // Watchdog timer
// watchdog interrupt
ISR (WDT_vect)
{
wdt_disable(); // disable watchdog
} // end of WDT_vect
void resetWatchdog ()
{
// clear various "reset" flags
MCUSR = 0;
// allow changes, disable reset, clear existing interrupt
WDTCR = bit (WDCE) | bit (WDE) | bit (WDTIF);
// set interrupt mode and an interval (WDE must be changed from 1 to 0 here)
WDTCR = bit (WDTIE) |bit (WDP3) | bit (WDP0); // set WDIE, and 8 seconds delay
// pat the dog
wdt_reset();
} // end of resetWatchdog
void setup ()
{
resetWatchdog (); // do this first in case WDT fires
pinMode(4, OUTPUT);
} // end of setup
void loop ()
{
digitalWrite (4, HIGH);
delay(160);
digitalWrite (4, LOW);
goToSleep ();
} // end of loop
void goToSleep ()
{
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
ADCSRA = 0; // turn off ADC
//power_all_disable (); // power off ADC, Timer 0 and 1, serial interface
noInterrupts (); // timed sequence coming up
resetWatchdog (); // get watchdog ready
sleep_enable (); // ready to sleep
interrupts (); // interrupts are required now
sleep_cpu (); // sleep
sleep_disable (); // precaution
// power_all_enable (); // power everything back on
} // end of goToSleep
I mostly tried looping wtd using FOR, and i cant get it right.. Basicly i'm confused what to loop.
Advice needed. 
Thank you.
Every 30 minutes, eh? 1800 seconds?
So you loop 1800/8 (225) times (going to sleep each time) and then sound the buzzer.
I've done a similar thing with my temperature sensor - to record the temperature every 15 minutes you loop, sleeping and waking, until the time is up.
1800 seconds, yes.
The problem i have is figuring out how to get it to sound the buzzer when i switch it ON and then buzz every 1800 seconds.
I manage to do one but not both like i would want.
I'm using a basic circuit.. 3V 2032 battery, buzzer, attiny13a and a ON/OFF switch.
#include <avr/sleep.h> // Sleep Modes
#include <avr/power.h> // Power management
#include <avr/wdt.h> // Watchdog timer
// watchdog interrupt
ISR (WDT_vect)
{
wdt_disable(); // disable watchdog
} // end of WDT_vect
void resetWatchdog ()
{
// clear various "reset" flags
MCUSR = 0;
// allow changes, disable reset, clear existing interrupt
WDTCR = bit (WDCE) | bit (WDE) | bit (WDTIF);
// set interrupt mode and an interval (WDE must be changed from 1 to 0 here)
WDTCR = bit (WDTIE) |bit (WDP3) | bit (WDP0); // set WDIE, and 8 seconds delay
// pat the dog
wdt_reset();
} // end of resetWatchdog
void setup ()
{
resetWatchdog (); // do this first in case WDT fires
pinMode(4, OUTPUT);
} // end of setup
void loop ()
{
int static flag=0;
if(flag>1)
{
digitalWrite (4, HIGH);
delay(160);
digitalWrite (4, LOW);
flag==0;
}
else
{
flag++;
}
goToSleep();
}
// end of loop
void goToSleep ()
{
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
ADCSRA = 0; // turn off ADC
//power_all_disable (); // power off ADC, Timer 0 and 1, serial interface
noInterrupts (); // timed sequence coming up
resetWatchdog (); // get watchdog ready
sleep_enable (); // ready to sleep
interrupts (); // interrupts are required now
sleep_cpu (); // sleep
sleep_disable (); // precaution
// power_all_enable (); // power everything back on
} // end of goToSleep
So i managed to get it going, but i dont understand why the first buzz comes after 16seconds and others come after 8 seconds repeating every 8 again.
flag==0;
That doesn't do anything useful. You want:
flag=0;