Code not work 38khz for IR and sleep

Hi Folks,

Hope you are doing fine !

I spend many days tring to do a simples code run but I fail in all ways

My code should to do ......

  1. Turn on micronctroller from mechanical switch

  2. Send IR signal for 6 sec.

  3. after 6 seconds go to sleeep for save battery

  4. keep on sleep until to mechanical switch should turn off microcontroller after 30sec more or less

I tried to use IF, Were as well case switch but i think problem is using interruption + library from sleep .

Below you can find my code

Anyone know a way to help me :slight_smile: ? I really apreciate it

int cont = 0;

#include "tinysnore.h"

void setup(){
  DDRB |= (1<<PB0); //Set pin PB0 as output
  TCNT0 = 0;
  TCCR0A=0;
  TCCR0B=0;

  TCCR0A |=(1<<COM0A0); //Timer0 in toggle mode Table 11-2
  TCCR0A |=(1<<WGM01); //Start timer 1 in CTC mode Table 11.5
  TCCR0B |= (1 << CS00);// Prescaler table 11.6
  OCR0A=104; //CTC Compare value 
}

void loop(){

  if (cont == 3) {

    snore(30000); 

  } else {

   TCCR0A &= ~(1<<COM0A0); //off
  delay(1000); 
  TCCR0A |=(1<<COM0A0); //on
  delay(1000);
  cont++;

  }

}
  • Please explain this more ?

  • This might be a job for a hardware/software power on circuit.


Hi Larry.

Hardware is working well ........ if a run my code without sleep, IR work perfect

My target is

I need just 6sec IR on and go to sleep mode until to mechanical sitch turnoff my cisrucit.

I made a simple code as below it's work perfect

void setup(){
  DDRB |= (1<<PB0); //Set pin PB0 as output
  TCNT0 = 0;
  TCCR0A=0;
  TCCR0B=0;

  TCCR0A |=(1<<COM0A0); //Timer0 in toggle mode Table 11-2
  TCCR0A |=(1<<WGM01); //Start timer 1 in CTC mode Table 11.5
  TCCR0B |= (1 << CS00);// Prescaler table 11.6
  OCR0A=104; //CTC Compare value 
}

void loop(){
  TCCR0A &= ~(1<<COM0A0); //off
  delay(1000); 
  TCCR0A |=(1<<COM0A0); //on
  delay(1000);
}

But it's keep runing around 40 seconds until to mechanical switch turnoff my circuit.

I would like to use IR for 6 seconds and save baterry puttin it on sleep after 6 seconds.

image

  • What causes the LED to turn ON ?

  • How long is the LED to remain ON ?

Edit

I had no toruble with circuit.

I made it simple as below

image

Here is step from code.

step 1 - Turn on micronctroller from mechanical switch ..... my last topic with esquematic

step 2 - Send IR signal for 6 sec.

step 3 - after 6 seconds go to sleeep for save battery

step 4 - keep on sleep until to mechanical switch turnoff microcontroller

My mistake is .... when microcontroller turnon don't follow my code using if.

int cont = 0;

#include "tinysnore.h"

void setup(){
  DDRB |= (1<<PB0); //Set pin PB0 as output
  TCNT0 = 0;
  TCCR0A=0;
  TCCR0B=0;

  TCCR0A |=(1<<COM0A0); //Timer0 in toggle mode Table 11-2
  TCCR0A |=(1<<WGM01); //Start timer 1 in CTC mode Table 11.5
  TCCR0B |= (1 << CS00);// Prescaler table 11.6
  OCR0A=104; //CTC Compare value 
}

void loop(){

  if (cont == 3) {

    snore(30000); 

  } else {

   TCCR0A &= ~(1<<COM0A0); //off
  delay(1000); 
  TCCR0A |=(1<<COM0A0); //on
  delay(1000);
  cont++;

  }

}
  • Sounds like the switch you are using is similar to an ON/OFF toggle switch.

  • Your software logic is reasonable but as long as the switch remains in the ON position, some power is still being taken from the battery (a small amount).

  • Highly recommend you use this circuit.

  • The switch (in the above circuit) is momentary, normally open (N.O.).
    When the switch is pressed, the controller powers up, software makes the output go HIGH, therefore keeping power ON via the NPN transistor being ON.

  • When 6 seconds expires your LED goes OFF, software makes the output pin go LOW, therefore turning OFF ALL power.

I'm using mechanical switch from machine end stroke like this

image

it's seen a software problem like keep pin on awake 6 second send 38khz after 6 seconds sleep.

This code work perfect changing 38khz script for blink script without interrupts just to made a test.

But if I use 38khz functions doesn't work ..... don't respect my if conditions

:confused: