First time using Attiny 45 and very amateur coder.
Basic test to blink a LED for 0.5 second every 2.5 second. Now I want to sleep the Attiny while the led is off.
I did installed the "tinysnore.h" but the little code now use 14% of the available program storage.
Is there a way to reduce memory usage or other sleep library.
The next goal will be to reduce power usage.
#include "tinysnore.h" // Include TinySnore Library
const int ledPin = 4; // LED connected to digital pin 0 (PB0)
const unsigned long onTime = 500; // 0.5 second in microseconds
const unsigned long offTime = 2000; // 2.5 seconds in microseconds
void setup() {
pinMode(ledPin, OUTPUT); // Initialize the digital pin as an output.
// Set clock to 1 MHz
CLKPR = (1 << CLKPCE); // Enable change of clock division
CLKPR = 0; // Set clock division to 1
}
void loop() {
digitalWrite(ledPin, HIGH); // Turn LED on
delay(onTime); // Wait for onTime microseconds
digitalWrite(ledPin, LOW); // Turn LED off
snore(offTime); // Wait for offTime microseconds
}
Every one starts as a beginner. Reading specs and manuals is a good way to learn what the device can do.
Often examples code are provided there, or by the IDE examples.
Is there any real need for that?
Please provide a description of the total project. Putting the controller asleep still havong current consuming sensors, devices, will likely not be what You want.
Without the "snore" your code compiles to 440 bytes.
With the snore to 584 bytes.
So in fact it adds 144 bytes to your code.
I looked into the source and it is using a list of "if" statements with different sleep lengths to support almost any amount of sleep lengths, by going to sleep repeatedly.
If you are not requiring an exact amount of sleeptime a lot of memory can be saved by sticking to the built in timeout periods for putting the attiny to sleep.
If one of these timeouts would serve your needs, you could code your own sleep instructions and be done with adding significant less bytes.
Further memory reduction can be done if you replace Arduino functions like digitalWrite by direct port manipulation. Like what you already did by changing the clock prescaler.
With a few modifications of your sketch this compiles to 546 bytes.
#include "tinysnore.h" // Include TinySnore Library
#define ledPin PB0 // LED connected to digital pin 0 (PB0)
const unsigned int onTime = 500; // 0.5 second in microseconds
const unsigned int offTime = 2000; // 2.5 seconds in microseconds
void setup() {
// pinMode(ledPin, OUTPUT); // Initialize the digital pin as an output.
DDRB |= _BV(ledPin); //set ledPin as output
// Set clock to 1 MHz
CLKPR = (1 << CLKPCE); // Enable change of clock division
CLKPR = 0; // Set clock division to 1
}
void loop() {
// digitalWrite(ledPin, HIGH); // Turn LED on
PORTB |= _BV(ledPin); // turn ledPin on
delay(onTime); // Wait for onTime microseconds
// digitalWrite(ledPin, LOW); // Turn LED off
PORTB &= ~_BV(ledPin); // turn ledPin off
snore(offTime); // Wait for offTime microseconds
}
I did try other library, but memory usage was worst. I want to install a little processor on an Radio Controlled 1 meter sailboat. The processor will run on a batterie. I did have on hand Attiny45 that I bought back in 2005, but never use it.
First task, display when power is on. This already use 14% of memory.
Then, i want to add some functionality like
A single push button (magnetic switch) to power on/off.
LiFePo4 2S batterie monitoring for onboard /charging, discharging
If there is space, I could use a GPS module to capture displacement, but I know that this is a futil dream knowing my lack of knowledge.