Attiny85 and NRF24L01 remote controller not working properly

With the ATtiny85 (3Pin configuration) and NRF2401, I created a program for a single button remote controller.
When the button is pressed for more than 400 milliseconds, the transmitter circuit will transfer the payload, and after 3000 milliseconds, the attiny will go into sleep mode and close the transistor that controls the nrf24l01 module's power supply.

The issue I'm having is that the tx circuit only sends the payload after the third time button hit, this only occurred immediate after waking up from sleep mode. While wakeup condition tx circuit send payload on each press.

Here is my code ,Kindly let me know if anything wrong in code.




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


#include "RF24.h"
#define CE_PIN 3
#define CSN_PIN 3

#define adc_disable() (ADCSRA &= ~(1<<ADEN)) // disable ADC (before power-off)
#define adc_enable()  (ADCSRA |=  (1<<ADEN)) // re-enable ADC
#define INTERRUPT_PIN PCINT4  // This is PB1 per the schematic
#define INT_PIN PB4           // Interrupt pin of choice: PB1 (same as PCINT1) - Pin 3
#define PCINT_VECTOR PCINT0_vect      // This step is not necessary - it's a naming thing for clarity
#define LED_PIN PB3           // PB4 - Pin 2
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

byte joystick[2];  // 2 element array holding Joystick reading and 4 buttons
unsigned long TimeElapse = 400; // wait to turn on LED
unsigned long TimeElapse2 = 3000;
unsigned long LedChangeState; // Store the time after press the button
unsigned long LedChangeState2;
boolean ledState = false; //set the flag Only one time do the function
boolean ledState2 = false; //set the flag Only one time do the functio
byte LastState = LOW;

const int LadderSw = 4;
int pinLed = 3;
void setup() {

  radio.begin();
  radio.openWritingPipe(pipe);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  cli();                            // Disable interrupts during setup

  PCMSK |= (1 << INTERRUPT_PIN);    // Enable interrupt handler

  GIMSK |= (1 << PCIE);             // Enable PCINT interrupt in the general interrupt mask

  pinMode(LadderSw, INPUT);
  sei();//last line of setup - enable interrupts after setup
  adc_disable(); // ADC uses ~320uA
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
}
ISR(PCINT_VECTOR)
{
  sbi(ADCSRA, ADEN);
  sleep_disable();
}

void enterSleep (void)
{
  radio.powerDown(); //put nRF24L01 into power down mode
  cbi(ADCSRA, ADEN);
  sleep_enable();
  sleep_cpu();
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_mode();
}



void loop() {



  unsigned long currentMillis = millis();
  joystick[0] = digitalRead(LadderSw);

  if ( joystick[0] != LastState)
  {

    LedChangeState = currentMillis;
    ledState = false;
    digitalWrite(LED_PIN, LOW); ///power on nrf24 through transistor
    radio.begin();
    radio.powerUp(); //power up the nRF24
    radio.openWritingPipe(pipe);

  }

  if (joystick[0] == HIGH && (unsigned long)(currentMillis - LedChangeState) >= TimeElapse && ledState == false)//After the TimeElapse tx will send payload
  {
    radio.stopListening();
    radio.write( joystick, sizeof(joystick));
    LedChangeState2 = currentMillis;
    ledState = true;
    ledState2 = true;

  }
  if ((unsigned long)(currentMillis - LedChangeState2) >= TimeElapse2 && ledState2 == true)//After the TimeElapse2 tx will control will enter to sleep mode
  {
    digitalWrite(LED_PIN, HIGH); //power off the nrf24 through transistor
    ledState2 = false;
    enterSleep();

  }
  LastState = joystick[0];

}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.