I'm trying to use the following code on an attiny85.
It works without any problems with an arduino uno but not with the attiny85.
I'm pretty sure the problem is the Wire.h library because the attiny can't use i2c as simple as the uno can. I tried using the tinyWireM.h instead which should help using i2c and even the
attiny core to compile the code and should have a build in transformation of wire.h for attiny. Both options didn't work, probably because the
Adafruit led backpack library, which I need to control the alphanum-display, isn't working with tinyWireM.h or something.
This is my code:
// include libraries
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include <ClickEncoder.h>
#include <TimerOne.h>
#include <avr/sleep.h>
#include <avr/power.h>
// Set up the LED display
Adafruit_AlphaNum4 alpha4 = Adafruit_AlphaNum4();
char displayBuffer[4];
uint8_t dimensionLetter='C';
// Set up the click encoder
ClickEncoder *encoder;
int16_t last, value;
#define encoderPinA A1
#define encoderPinB A0
#define encoderButtonPin A2
// Steps per notch can be 1, 4, or 8. If your encoder is counting
// to fast or too slow, change this!
#define stepsPerNotch 8
// Comment this line to make the encoder increment in the opposite direction
#define reverseEncoderWheel
// FX Board output delay (ms)
const int msDelay = 500;
// Set up the LED
#define LEDpin 9
// Set up interrupt pin for wake-up
#define NAV0_PIN A2
//Let us know if woke up from sleep
volatile bool justWokeUp;
void timerIsr() {
encoder->service();
}
void setup() {
enablePinInterupt(NAV0_PIN);
//Set up pin modes
pinMode(LEDpin, OUTPUT);
digitalWrite(LEDpin, HIGH);
encoderSetup();
alpha4.begin(0x70); // pass in the address for the LED display
justWokeUp = false;
}
void loop() {
// Woke up
if (justWokeUp) {
digitalWrite(LEDpin, HIGH);
justWokeUp = false;
}
ClickEncoder::Button b = encoder->getButton();
switch (b) {
case ClickEncoder::Held:
// Holding the button will put your trinket to sleep.
// The trinket will wake on the next button press
alpha4.clear();
alpha4.writeDigitAscii(0, 'R');
alpha4.writeDigitAscii(1, 'I');
alpha4.writeDigitAscii(2, 'C');
alpha4.writeDigitAscii(3, 'K');
digitalWrite(LEDpin, LOW);
alpha4.writeDisplay();
delay(5000);
alpha4.clear();
alpha4.writeDisplay();
delay(5000);
justWokeUp = true;
goToSleep();
break;
case ClickEncoder::Clicked:
// When the encoder wheel is single clicked
break;
case ClickEncoder::DoubleClicked:
//If you double click the button, it sets the dimension to C137
dimensionLetter = 'C';
value = 137;
break;
case ClickEncoder::Open:
// The dimension will increment from 0-999, then roll over to the next
// letter. (A999 -> B000)
updateDimension();
break;
}
}
void encoderSetup(){
// set up encoder
encoder = new ClickEncoder(encoderPinA, encoderPinB, encoderButtonPin, stepsPerNotch);
encoder->setAccelerationEnabled(true);
Timer1.initialize(1000);
Timer1.attachInterrupt(timerIsr);
last = -1;
value = 137;
}
void updateDimension(){
#ifdef reverseEncoderWheel
value -= encoder->getValue();
#endif
#ifndef reverseEncoderWheel
value += encoder->getValue();
#endif
if (value != last) {
if (value > 999){
value = 0;
if (dimensionLetter == 'Z') {
dimensionLetter = 'A';
} else {
dimensionLetter ++;
}
} else if ( value < 0 ) {
value = 999;
if (dimensionLetter == 'A') {
dimensionLetter = 'Z';
} else {
dimensionLetter --;
}
}
last = value;
}
sprintf(displayBuffer, "%03i", value);
alpha4.clear();
alpha4.writeDigitAscii(0, dimensionLetter);
alpha4.writeDigitAscii(1, displayBuffer[0]);
alpha4.writeDigitAscii(2, displayBuffer[1]);
alpha4.writeDigitAscii(3, displayBuffer[2]);
alpha4.writeDisplay();
}
/*
============== Sleep/Wake Methods ==================
====================================================
*/
// Most of this code comes from seanahrens on the adafruit forums
// http://forums.adafruit.com/viewtopic.php?f=25&t=59392#p329418
void enablePinInterupt(byte pin)
{
*digitalPinToPCMSK(pin) |= bit (digitalPinToPCMSKbit(pin)); // enable pin
PCIFR |= bit (digitalPinToPCICRbit(pin)); // clear any outstanding interrupt
PCICR |= bit (digitalPinToPCICRbit(pin)); // enable interrupt for the group
}
void goToSleep()
{
// The ATmega328 has five different sleep states.
// See the ATmega 328 datasheet for more information.
// SLEEP_MODE_IDLE -the least power savings
// SLEEP_MODE_ADC
// SLEEP_MODE_PWR_SAVE
// SLEEP_MODE_STANDBY
// SLEEP_MODE_PWR_DOWN -the most power savings
// I am using the deepest sleep mode from which a
// watchdog timer interrupt can wake the ATMega328
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Set sleep mode.
sleep_enable(); // Enable sleep mode.
sleep_mode(); // Enter sleep mode.
// After waking the code continues
// to execute from this point.
sleep_disable(); // Disable sleep mode after waking.
}
ISR (PCINT0_vect) // handle pin change interrupt for D8 to D13 here
{
// if I wired up D8-D13 then I'd need some code here
}
ISR (PCINT1_vect) // handle pin change interrupt for A0 to A5 here // NAV0
{
/* This will bring us back from sleep. */
/* We detach the interrupt to stop it from
* continuously firing while the interrupt pin
* is low.
*/
detachInterrupt(0);
}
ISR (PCINT2_vect) // handle pin change interrupt for D0 to D7 here // NAV1, NAV2
{
// Check it was NAV1 or NAV2 and nothing else
}
It reads a rotary encoder and writes something on the display accordingly and also has a sleep mode.
The error message I receive with the attiny ist the following:
C:\Users\Anton\Documents\Arduino\libraries\Adafruit_LED_Backpack-master\Adafruit_LEDBackpack.cpp:695:6: error: prototype for 'void Adafruit_7segment::writeDigitNum(uint8_t, uint8_t, boolean)' does not match any in class 'Adafruit_7segment'
void Adafruit_7segment::writeDigitNum(uint8_t d, uint8_t num, boolean dot) {
^~~~~~~~~~~~~~~~~
In file included from C:\Users\Anton\Documents\Arduino\libraries\Adafruit_LED_Backpack-master\Adafruit_LEDBackpack.cpp:38:0:
C:\Users\Anton\Documents\Arduino\libraries\Adafruit_LED_Backpack-master\Adafruit_LEDBackpack.h:376:8: error: candidate is: void Adafruit_7segment::writeDigitNum(uint8_t, uint8_t, bool)
void writeDigitNum(uint8_t x, uint8_t num, bool dot = false);
^~~~~~~~~~~~~
C:\Users\Anton\Documents\Arduino\libraries\Adafruit_LED_Backpack-master\Adafruit_LEDBackpack.cpp:726:6: error: prototype for 'void Adafruit_7segment::writeDigitAscii(uint8_t, uint8_t, boolean)' does not match any in class 'Adafruit_7segment'
void Adafruit_7segment::writeDigitAscii(uint8_t d, uint8_t c, boolean dot) {
^~~~~~~~~~~~~~~~~
In file included from C:\Users\Anton\Documents\Arduino\libraries\Adafruit_LED_Backpack-master\Adafruit_LEDBackpack.cpp:38:0:
C:\Users\Anton\Documents\Arduino\libraries\Adafruit_LED_Backpack-master\Adafruit_LEDBackpack.h:384:8: error: candidate is: void Adafruit_7segment::writeDigitAscii(uint8_t, uint8_t, bool)
void writeDigitAscii(uint8_t x, uint8_t c, bool dot = false);
^~~~~~~~~~~~~~~
exit status 1
Fehler beim Kompilieren für das Board ATtiny25/45/85 (No bootloader).
I'm happy about any help.
Thank you!