I want to generate random numbers in my sketch for a project with an Attiny85 but I've used up all of my pins and don't have one to sample for noise. The project also doesn't have any user inputs that I could use to generate a seed. I've spent a bit of time looking this up but the best I've found are topics about entropy that are frankly far over my head.
The easiest solution I've come up with is to simply use an Attiny84 instead but I already have a handful of 85s and using the smaller chip would save me space on the PCB.
Is there an easy solution for the, preferably for a novice programmer who is not smart with the maths?
Edit: Added code and picture of schematic. I'm testing the code on an Arduino Uno R3 right now before I try putting it on the Attiny85, that is why the attiny pins are commented out and Uno pins are used instead.
The project is a motion activated music box that uses an ultrasonic sensor to detect movement and a motor attached to a hand crank music box mechanism. In addition having the music box being triggered by motion I want to also have a "haunted" mode that plays music at random intervals. This code does include an attempt I made at copy/pasting some code in to generate random numbers in the form of "getRandom()" at the bottom but I do not understand exactly what it is doing and the random number pattern ended up being the same every time I restarted the sketch.
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
//#define TRIGGER_PIN 3 // Attiny85 Arduino pin tied to trigger pin on the ultrasonic sensor.
//#define ECHO_PIN 4 // Attiny85 Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
// Pin assignments
const int led = 2; // LED is attached to pin 2
const int motor = 5; // Motor is attached to pin 1
const int hauntedSelect = 3;
const int hauntedInterval = 5; // How often the haunted function triggers in seconds
const int hauntedDuration = 10; // How long the haunted music plays when triggered
//const int led = 0; //Attiny85
//const int motor = 1; //Attiny85
//const in hauntedSelect = 2; //Attiny85
bool object_detected = false;
bool max_range = false;
int sensorSamples = 0;
unsigned long previousMillis = 0;
unsigned long m_w = 1; //variable for random number generation
unsigned long m_z = 2; //variable for random number generation
void setup() {
pinMode(led, OUTPUT);
pinMode(motor, OUTPUT);
pinMode(hauntedSelect, INPUT);
//Take range samples to determine range to nearest object in front of sensor
int i;
for (i = 0; i < 10; i++) {
digitalWrite(led, !digitalRead(led)); //toggles LED on and off while calibrating range
sensorSamples = sensorSamples + sonar.ping_cm();
delay(200);
}
digitalWrite(led, LOW);
//Find the average of all samples.
//10 is subracted to give a buffer to compensate for minor reading variations
sensorSamples = (sensorSamples / 10) - 10;
if(sensorSamples <= 0){ //the sensor will read 0 if it does not detect a surface
max_range = true;
}
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
if(max_range){
if(sonar.ping_cm() > 10){
playMusic(2);
}
}
else if(sonar.ping_cm() < sensorSamples){
playMusic(2);
}
if(digitalRead(hauntedSelect)){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > (hauntedInterval * 1000)){ // 600000 miliseconds is ten minutes
int randNumber;
randNumber = getRandom();
if(randNumber % 3 == 0){
playMusic(hauntedDuration);
}
previousMillis = currentMillis;
}
}
}
void playMusic(int x){ // turns on the music box motor for x seconds
digitalWrite(motor, HIGH);
for (int i = 0; i < x; i++) { //loop and toggle LED every 1 second
digitalWrite(led, !digitalRead(led));
delay(1000);
}
while(sonar.ping_cm() < sensorSamples){
digitalWrite(led, !digitalRead(led));
delay(1000);
}
digitalWrite(motor, LOW);
digitalWrite(led, LOW);
}
unsigned long getRandom()
{
m_z = 36969L * (m_z & 65535L) + (m_z >> 16);
m_w = 18000L * (m_w & 65535L) + (m_w >> 16);
return (m_z << 16) + m_w;
}