Go Down

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

Amrtgv

May 25, 2019, 08:56 am Last Edit: May 25, 2019, 06:27 pm by Amrtgv
Hi,
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))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
int pinLed = 0;
int pinLed1 = 2;
int val2 = 0;
int val3 = 0;


void setup(){
  pinMode(pinLed,OUTPUT);
  pinMode(pinLed1,OUTPUT);
  pinMode(5,INPUT);
  val3 = digitalRead(5);
  ADCSRA &= ~_BV(ADEN);
  sbi(GIMSK,PCIE0);
  sbi(PCMSK0,PCINT5);
  
}



void Flash_before_sleep()
{
digitalWrite(pinLed1, HIGH);
delay(50);
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);  
                delay(2);                    
                digitalWrite(pinLed1, LOW);    
                delay(80);                    
                val2++;                        
  }
  
                
}


void loop()
{
  blink_run();
  solid_run();
  system_sleep();
}


void system_sleep() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sei();
  sleep_cpu();
  digitalWrite(pinLed, LOW);  
  sleep_mode();
  sleep_disable();

}

 ISR(PCINT0_vect)
 {

  val2 = 0;
  Flash_before_sleep();
  }



Thank you,


blh64

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