I am looking for help with ATtiny85 coding. The project I am working on is using an Arduino Nano and I want to switch to an ATtiny85.
The code below works with the Nano but not with the ATtiny85. I am relatively new to Arduinos and have done a few projects with success but this is my first time using an ATtiny.
I will try to explain. When the code is running on a Nano
- It is checking for a number between 1 and 32 in the EEPROM at address 0.
- If the number is outside either value it will generate a random number between 18 and 32.
- I then check to see if the value at address 0 in the EEPROM is less than or equal to 0, if not it will decrement that value and store the new value in address 0.
- Once the value is at 0 or less a separate value is generated for a time delay.
- Once that time delay has occurred pin 7 outputs power for 1 second and stops.
- Once that has occurred a new random number is generated and stored in address 0.
- To decrement the value the Nano is powered off and on again and the check is made.
I have tested this code with a Nano multiple times and with pin 7 connected to an led it turns on and off, resets and begins again. When I load this code onto an ATtiny85 and run the same test the led will not turn on, also I get an error with the Serial.begin(9600) and anything else starting with Serial, so I comment them out and it compiles and uploads just fine. I have the Spark Fun AVR programmer and was able to get the ATtiny to blink an led using a simple led blink sketch but that's as far as I got. I need some help.
#include <EEPROM.h> // Include the EEPROM library
void setup() {
Serial.begin(9600); // Initialize the serial communication
pinMode(7, OUTPUT); // Use digital pin 7 as output
randomSeed(analogRead(0)); // Seed the random number generator with a random value
int value = EEPROM.read(0); // Read the value stored in EEPROM address 0
if (value <= -1 || value > 32) { // If the value is not between 1 and 32
int new_Value = random(18, 32); // Generate a new random number between 18 and 32
EEPROM.write(0, new_Value); // Store the new value in EEPROM address 0
Serial.println("Generated a new random number: " + String(new_Value));
}
else if (value <= 0) { // Check if the random value is less than or equal to 0
value--; // If no then decrement the value
EEPROM.write(0, value); // Write the new value to EEPROM address 0
Serial.println("Random number from EEPROM: " + String(value));
}
else {
int time_delay = random(1, 25); // Generate random number for time delay
Serial.println("Generated Time Delay: " + String(time_delay));
delay(1000 * time_delay);
digitalWrite(7, HIGH);
delay(1000);
digitalWrite(7, LOW);
int new_Value = random(18, 32); // Generate new random day value
EEPROM.write(0, new_Value); // Write new value to EEPROM address 0
Serial.println("Generated a new random number: " + String(new_Value));
}
}
void loop() {
}
Sketch used for LED blink
void setup() {
pinMode(0, OUTPUT);
}
void loop() {
digitalWrite(0, HIGH);
delay(5000);
digitalWrite(0, LOW);
delay(5000);