Go Down

Topic: Reset problem (Read 156 times) previous topic - next topic


May 25, 2019, 08:56 am Last Edit: May 25, 2019, 06:27 pm by Amrtgv
I'm very new to Arduino and Atmel products, I was trying to build a code for ATTiny44.
I've used PIN#0 and PIN#1 as output.
PIN#5 as INPUT - Interrupt.

Whenever there is a pin change(0 or 1) on PIN#5, LED1 must flashes (PIN#2). => This part is working, but not sure if coding is correct or if it's working by luck.

Now digitalread PIN#5, if it's 1 (just pullup resistor) then switch on LED2 (PIN#0) and/or if it's 0 (ground + pullup resistor) then switch off LED (PIN#0). => This part is also working if I put switch off code before sleepmode but NOT with else in the code.

But my problem is that, if PIN5 is 0 and I push reset button, LED2 stops working and won't switch on.

Here is the code:

Code: [Select]

#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
int pinLed = 0;
int pinLed1 = 2;
int val2 = 0;
int val3 = 0;

void setup(){
  val3 = digitalRead(5);

void Flash_before_sleep()
digitalWrite(pinLed1, HIGH);
digitalWrite(pinLed1, LOW);

void solid_run(){
  val3 = digitalRead(5);
  if (val3 == 1){                    
      digitalWrite(pinLed, HIGH);    

//  else {
//      digitalWrite(pinLed, LOW);    
//  }              

void blink_run(){
  while(val2 < 1){                            
                digitalWrite(pinLed1, HIGH);  
                digitalWrite(pinLed1, LOW);    

void loop()

void system_sleep() {
  digitalWrite(pinLed, LOW);  



  val2 = 0;

Thank you,


A couple of things...

Variables used inside an ISR need to be declared as 'volatile'
Code: [Select]

volatile int val2 = 0;

Inside an ISR, you call Flash_before_sleep() which calls delay().  This will not work since delay() relies on interrupts working in order to increment elapsed time, but interrupts are NOT enabled inside an ISR.

Because of this, the delay() call inside Flash_before_sleep() will never complete.

Go Up