Created
February 4, 2020 11:19
-
-
Save TamojitSaha/ead5acdb12a6ea00baa58590823f015c to your computer and use it in GitHub Desktop.
The sketch implements sleep enabled NRF24 based sensor node interfacing with Attiny85 in 5 pins configuration. It can only be woken up by RESET.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @File: Attiny85_NRF24_Tx_5Pin_Sleep | |
* @author: Tamojit Saha <https://tamojitsaha.info> | |
* @Description: The sketch implements sleep enabled NRF24 based sensor node | |
* interfacing with Attiny85 in 5 pins configuration. | |
* It can only be woken up by RESET. | |
* | |
* @license: Creative Commons License (CC-BY-NC-SA 4.0) | |
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. | |
To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/ | |
or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. | |
*/ | |
#include <RF24.h> | |
#include <avr/sleep.h> // Sleep Modes | |
#include <avr/power.h> | |
#define CE_PIN 3 | |
#define CSN_PIN 4 | |
#define LED_PIN 3 //PB3 | |
RF24 radio(CE_PIN, CSN_PIN); | |
byte address[11] = "1Node"; | |
unsigned long payload = 500; | |
void setup() { | |
radio.begin(); // Start up the radio | |
while (!radio.isChipConnected()) { | |
digitalWrite(LED_PIN, HIGH); | |
delay(100); | |
digitalWrite(LED_PIN, LOW); | |
delay(100); | |
} | |
radio.setAutoAck(1); // Ensure autoACK is enabled | |
radio.setRetries(15, 15); // Max delay between retries & number of retries | |
radio.openWritingPipe(address); // Write to device address 'SimpleNode' | |
} | |
void loop() { | |
if (radio.isChipConnected()) { | |
process(); | |
} | |
if (!radio.isChipConnected()) { | |
for (byte i = 0; i < 10; i++) { | |
digitalWrite(LED_PIN, HIGH); | |
delay(100); | |
digitalWrite(LED_PIN, LOW); | |
delay(100); | |
} | |
} | |
goToSleep(); | |
} | |
bool isDelivered = false; | |
void process() { | |
radio.powerUp(); | |
radio.stopListening(); // First, stop listening so we can talk. | |
//isDelivered = radio.write( &payload, sizeof(payload)); | |
isDelivered = radio.write( &payload, sizeof(unsigned long)); | |
if (isDelivered) { | |
digitalWrite(LED_PIN, HIGH); | |
delay(250); | |
digitalWrite(LED_PIN, LOW); | |
delay(250); | |
//updateMessage(); | |
} | |
if (!isDelivered) { | |
for (int i = 0; i < 4; i++) { | |
digitalWrite(LED_PIN, HIGH); | |
delay(250); | |
digitalWrite(LED_PIN, LOW); | |
delay(250); | |
} | |
} | |
} | |
void goToSleep () | |
{ | |
radio.powerDown(); | |
set_sleep_mode(SLEEP_MODE_PWR_DOWN); | |
ADCSRA = 0; // turn off ADC | |
power_all_disable (); // power off ADC, Timer 0 and 1, serial interface | |
sleep_enable(); | |
sleep_bod_disable(); | |
sleep_cpu(); | |
sleep_disable(); | |
power_all_enable(); // power everything back on | |
} // end of goToSleep |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment