Can you access the EEPROM of an ATtiny with Arduino Code?
The Arduino EEPROM library is compatible with the ATTiny range of AVR microcontrollers as the library itself is built on the standard Atmel AVR avr/eeprom.h 'library' so it is compatible with all the AVR microcontrollers.
The EEPROM also doesn't take to being written or read to often as EEPROM can wear very quickly. Reading though does not cause much damage though.
Also note that using exit(0);
will stop the ATTiny from doing anything else after it is called so I hope your intention is to only run the loop once, if not this would account for either not seeing anything or it only ever running the blinking cycle once.
To answer your follow up question. Yes you can run one sketch to set you values in EEPROM and then use another sketch to read those. That us usually the point of EEPROM it is a memory type that "keeps its value while there is no power".
Also you need to make sure the ATTiny is set to preserve EEPROM during upload with the ISP, this is done with the fuse settings. You need to look for a tutorial on fuse calculators for the AVRs. To set the EESAVE
you need to set the High fuse to 0xD7
, you can change this in the boards.txt
file. Here is a fuse calculator.
If the code that is on your question at the moment is being used you won't be seeing anything as it needs the pins to be set with pinMode
. That's a note for others that see this.
Next what you can do is run a basic test code which blinks the LED without anything else going on.
Basically:
void setup(){
pinMode(0, OUTPUT);
}
void loop(){
digitalWrite(0, HIGH);
delay(1000);
digitalWrite(0, LOW);
delay(1000);
}
Below is the code you have written, it works I have it running in front of me. I have commented it and changed a few things to make it work as intended, although the intention is not clear.
#include <EEPROM.h>
byte CLK = 0; //This is a global variable with an initial value
void setup() {
// put your setup code here, to run once:
EEPROM.write(0, 1); //set the pin to D1
delay(5);
//read from EEPROM address 0
//this sets the value of the global variable CLK
CLK = EEPROM.read(0); //place this inside a function
//just to give an example...
byte PIN = CLK; //PIN variable is a local variable
pinMode(PIN ,OUTPUT); //using that local variable
}
void loop() {
// put your main code here, to run repeatedly:
// Note: the below if() will always be true as the value
// of CLK is never change and shouldn't be changed as
// it will change the pin number of the output
if (CLK == 1) {
for (int a = 0; a < 3; a++) {
digitalWrite(CLK, HIGH); //this uses the global variable
delay(1000);
digitalWrite(CLK, LOW);
delay(1000);
}
//putting exit(0); will actually stop the program
//remove it
// exit(0);
}
else {
for (int a = 0; a < 100; a++) {
digitalWrite(0, HIGH); //this has a predefined pin no.
delay(500);
digitalWrite(0, LOW); //this has a predefined pin no.
delay(500);
}
//putting exit(0); will actually stop the program
//remove it
// exit(0);
}
}