back with a tiny84,
still struggling to get my code to work correctly,
my pot controls the flashing speed of the led correctly, but not the digipot (SPI?)
tap switch doesn't work, it should also change the flashing speed of the led and the digipot
I originally had this working on a 328p, the board I made was messy so I made another with some pin changes,
that didn't work, couldn't figure out why, so I etched another board with the original pin config, still didn't work, same problems as attempt 2, tried again, same problem again, so I ordered another 328p and some crystals, still waiting for them to arrive 4 weeks later, so I tried using the tiny85 (reset pin!)
and now the tiny84
if anyone can see anything wrong with any of this it would be greatly appreciated
#include <SPI.h>
#include <EEPROM.h>
#include "EEPROMAnything.h"
#define TOLERANCE 5
int oldVal = 0;
//Variables for LED
int redpin = 8; // pin 5 tiny84
int currentred = HIGH;
int led_lit_time = 30;
//Variables for the repeat rate of the delay
int digipot_value = 100;
const int min_delay = 100;
const int max_delay = 1000;
int current_ms;
//Variables for "determine pot value"
int value;
int pot_low;
int pot_high;
int pot_value_low;
int pot_value_high;
int interpolated_ms;
int potchangerequired = HIGH;
int startup = HIGH;
//Variablen for tap-Button
const int tapbuttonpin = 5; // TINY84 pin 8
int tapbuttonstate = LOW;
int readingtapbutton;
int lasttapbuttonstate = LOW;
//Variables for Tap-Tempo
long firsttapmillis = 0;
long secondtapmillis = 0;
long tappeddelta = 0;
int firsttap = LOW;
//Variables for LED-blink-freq.
int ledpin = 10; // TINY84 pin 2
int blinkstate = LOW;
long lastblinktoggle;
int blinkfreq;
const int slaveDelayPin = 7; // TINY84 pin 6
//Variables and constants for debounce (global)
long lastDebounceTime = 0;
long debounceDelay = 5;
unsigned long currentMillis;
void setup()
{
SPI.begin();
pinMode (slaveDelayPin, OUTPUT);
pinMode(ledpin, OUTPUT);
pinMode(redpin, OUTPUT); // Pin for Tap Tempo LED
pinMode(tapbuttonpin, INPUT_PULLUP);
lastblinktoggle = millis();
}
void loop()
{
//############### NORMAL POT (DELAY TIME) ######################
int potVal = analogRead(A1); // tiny84 pin 12
int potval_ms = map(potVal, 0, 1023, min_delay, max_delay);
int diff = abs(potVal - oldVal);
if (diff > TOLERANCE)
{
current_ms = potval_ms;
potchangerequired = HIGH;
oldVal = potVal;
}
if (startup = HIGH)
{
current_ms = potval_ms;
potchangerequired = HIGH;
oldVal = potVal;
startup = LOW;
}
//############### TAP-BUTTON ##########################
readingtapbutton = digitalRead(tapbuttonpin);
if (readingtapbutton != lasttapbuttonstate)
{
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
if (readingtapbutton != tapbuttonstate)
{
tapbuttonstate = readingtapbutton;
if (tapbuttonstate == LOW)
{
if (firsttap == HIGH)
{
lastblinktoggle = current_ms - blinkfreq;
firsttapmillis = millis();
firsttap = LOW;
}
else
{
secondtapmillis = millis();
tappeddelta = secondtapmillis - firsttapmillis;
if (tappeddelta <= 1500 && tappeddelta >= min_delay)
{
current_ms = tappeddelta;
tappeddelta = 0;
potchangerequired = HIGH;
}
firsttap = HIGH;
}
}
}
}
lasttapbuttonstate = readingtapbutton;
if (millis() - firsttapmillis > 2000)
{
firsttap = HIGH;
}
// ############### BLINK according to blinkfreq ########################
blinkfreq = current_ms;
currentMillis = millis();
digitalWrite(redpin, currentred);
if (currentMillis - blinkfreq > lastblinktoggle)
{
digitalWrite(ledpin, HIGH);
lastblinktoggle = currentMillis;
}
if (currentMillis - lastblinktoggle > led_lit_time)
{
digitalWrite(ledpin, LOW);
}
// ############### WRITE TO DELAY-MCP (POTCHANGEREQUIRED) ########################
if (potchangerequired == HIGH)
{
for (int i = 0; i < 256; i = i + 5)
// The loop tries to find two values from the EEPROM, that "surround" current_ms
{
pot_low = i;
pot_high = i + 5;
EEPROM_readAnything(i, pot_value_low);
EEPROM_readAnything(i + 5, pot_value_high);
if (current_ms >= pot_value_low && current_ms < pot_value_high)
{
digipot_value = pot_low + (((current_ms - pot_value_low) * 100) / (pot_value_high - pot_value_low) * (pot_high - pot_low)) / 100;
digitalPotWrite(slaveDelayPin, digipot_value);
potchangerequired = LOW;
}
}
}
} // END of LOOP()
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ digital pot write ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int digitalPotWrite(int pin, int value)
{
digitalWrite(pin, LOW); //When Slave is LOW, the MCP listens to the master
SPI.transfer(0x11);
SPI.transfer(value);
digitalWrite(pin, HIGH); //When Slave is HIGH, the MCP does not listen to the master
}
