0

I am pretty new to arduino programming. I want to use the library from the Robotdyn dimmer modul with a arduino uno and a encoder with lcd for a menü but if i attach the interrupt for the encoder (on pin 3 via attachInterrupt(digitalPinToInterrupt(3), encoder, LOW); ) i get this error:

WInterrupts.c.o (symbol from plugin): In function attachInterrupt': (.text+0x0): multiple definition of__vector_1' libraries\RBDDimmer-master\avr\RBDmcuAVR.cpp.o (symbol from plugin):(.text+0x0): first defined here collect2.exe: error: ld returned 1 exit status exit status 1 Fehler beim Kompilieren für das Board Arduino/Genuino Uno.

The dimmer uses pin 2 for zerocross detection so tried to search the library for the interrupt on pin 3 to remove it but i dont get what is going on in the library...

library:

    ifndef RBDDIMMER_H
#define RBDDIMMER_H

#include <stdlib.h>

#if   defined(ARDUINO_ARCH_AVR)
    #include "avr/RBDmcuAVR.h"
#elif defined(ARDUINO_ARCH_ESP32)
    #include "esp32/RBDmcuESP32.h"
#elif defined(ARDUINO_ARCH_ESP8266)
    #include "esp8266/RBDmcuESP8266.h"
#elif defined(ARDUINO_ARCH_SAMD)
    #include "samd/RBDmcuSAMD21.h"
#elif defined(ARDUINO_ARCH_SAM)
    #include "sam/RBDmcuSAM.h"
#elif defined(ARDUINO_ARCH_STM32F1)
    #include "stm32duino/STM32F1/RBDmcuSTM32F1.h"
#elif defined(ARDUINO_ARCH_STM32F4)
    #include "stm32duino/STM32F4/RBDmcuSTM32F4.h"
#else 
    #error "This library only supports boards with an AVR, ESP32, ESP8266, SAMD, SAM, STM32F1/F4 processor."
#endif

typedef enum
{
    NORMAL_MODE = 0,
    TOGGLE_MODE = 1
} DIMMER_MODE_typedef;

typedef enum
{
    OFF = false,
    ON = true
} ON_OFF_typedef;

#define ALL_DIMMERS 30

class dimmerLamp 
{         
    private:
        int current_num;
        int timer_num;
        bool toggle_state;
        int tog_current;

        void port_init(void);
        void timer_init(void);
        void ext_int_init(void);

    public:   
        uint16_t pulse_begin;
        int dimmer_pin;
        int tog_max;
        int tog_min;

#if !(defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_SAMD))
        int zc_pin;

        dimmerLamp(int user_dimmer_pin, int zc_dimmer_pin);
#else
        dimmerLamp(int user_dimmer_pin);
#endif
        void begin(DIMMER_MODE_typedef DIMMER_MODE, ON_OFF_typedef ON_OFF);
        void setPower(int power);
        int  getPower(void);
        void setState(ON_OFF_typedef ON_OFF);
        bool getState(void);
        void changeState(void);
        void setMode(DIMMER_MODE_typedef DIMMER_MODE);
        DIMMER_MODE_typedef getMode(void);
        void toggleSettings(int minValue, int maxValue);  
};

#endif

RBDmcuAVR.h :

#ifndef RBDMCUAVR_H
#define RBDMCUAVR_H

#include "Arduino.h"
#include "RBDdimmer.h"
#include <avr/io.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <util/atomic.h>

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define DIMMER_TIMER 4
#define INT_vect INT4_vect   
#define INTx INT4
#define EICRX EICRB
#define ISCx1 ISC41
#define ISCx0 ISC40
#define ALL_DIMMERS 50

#define TCCRxA_VALUE 0x00 // CTC mode
#define TCCRxB_VALUE 0x0A // 0b1011 // (1 << WGMx2)|(1 << CSx1)|(1 << CSx0)
#define OCRxAH_VALUE 0x00
#define OCRxAL_VALUE 0x0F

#define _OCRxAH(X) OCR ## X ## AH
#define OCRxAH(X) _OCRxAH(X)
#define _OCRxAL(X) OCR ## X ## AL
#define OCRxAL(X) _OCRxAL(X)

#elif defined(__AVR_ATmega32U4__)
#define DIMMER_TIMER 1
#define INT_vect INT6_vect
#define INTx INT6
#define EICRX EICRB
#define ISCx1 ISC61
#define ISCx0 ISC60
#define ALL_DIMMERS 30

#define TCCRxA_VALUE 0x00 // CTC mode
#define TCCRxB_VALUE 0x09// 0b1011 (1 << WGMx2)|(1 << CSx1)|(1 << CSx0)
#define OCRxAH_VALUE 0x00
#define OCRxAL_VALUE 0xBC

#define _OCRxAH(X) OCR ## X ## AH
#define OCRxAH(X) _OCRxAH(X)
#define _OCRxAL(X) OCR ## X ## AL
#define OCRxAL(X) _OCRxAL(X)

#else
#define DIMMER_TIMER 2
#define INT_vect INT0_vect
#define INTx INT0
#define EICRX EICRA
#define ISCx1 ISC01
#define ISCx0 ISC00
#define ALL_DIMMERS 13

#define TCCRxA_VALUE 0x02
#define TCCRxB_VALUE 0x09 // 0b1010 // (1 << WGMx2)|(1 << CSx1)
#define OCRxAH_VALUE 0x00
#define OCRxAL_VALUE 0xFF

#define _OCRxAH(X) OCR ## X ## A
#define OCRxAH(X) _OCRxAH(X)
#define _OCRxAL(X) OCR ## X ## A
#define OCRxAL(X) _OCRxAL(X)

#endif

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__)
static const uint16_t powerBuf[] = {
     610,   604, 598,  592,  586,  580,  574,  568,  562,   556,
     550,   544, 538,  532,  526,  520,  514,  508,  502,   496,
     490,   484, 478,  472,  466,  460,  454,  448,  442,   436,
     430,   424, 418,  412,  406,  400,  394,  388,  382,   376,
     370,   364, 358,  352,  346,  340,  334,  328,  322,   316,
     310,   304, 298,  292,  286,  280,  274,  268,  262,   256,
     250,   244, 238,  232,  226,  220,  214,  208,  202,   196,
     180,   174, 168,  162,  156,  150,  144,  138,  132,   126,
     120,   114, 108,  102,  96,   90,   84,   78,   72,     66,
      60,    54,  48,   42,  36,   30,   24,   18,   12,      6
};
#else
static const uint16_t powerBuf[] = {
     600,   600, 598,  592,  586,  580,  574,  568,  562,   556,
     550,   544, 538,  532,  526,  520,  514,  508,  502,   496,
     490,   484, 478,  472,  466,  460,  454,  448,  442,   436,
     430,   424, 418,  412,  406,  400,  394,  388,  382,   376,
     370,   364, 358,  352,  346,  340,  334,  328,  322,   316,
     310,   304, 298,  292,  286,  280,  274,  268,  262,   256,
     250,   244, 238,  232,  226,  220,  214,  208,  202,   196,
     180,   174, 168,  162,  156,  150,  144,  138,  132,   126,
     120,   114, 108,  102,  96,   90,   84,   78,   72,     66,
      60,    54,  48,   42,  36,   30,   24,   18,   12,      8
};
#endif

#endif

RBDmcuAVR.cpp :

#if defined(ARDUINO_ARCH_AVR)

#include "RBDmcuAVR.h"

#define _TCNT(X) TCNT ## X
#define TCNT(X) _TCNT(X)
#define _TCCRxA(X) TCCR ## X ## A
#define TCCRxA(X) _TCCRxA(X)
#define _TCCRxB(X) TCCR ## X ## B
#define TCCRxB(X) _TCCRxB(X)
#define _TIMSKx(X) TIMSK ## X
#define TIMSKx(X) _TIMSKx(X)
#define _TIFRx(X) TIFR ## X
#define TIFRx(X) _TIFRx(X)
#define _OCFxA(X) OCF ## X ## A
#define OCFxA(X) _OCFxA(X)
#define _OCFxB(X) OCF ## X ## B
#define OCFxB(X) _OCFxB(X)
#define _TOIEx(X) TOIE ## X
#define TOIEx(X) _TOIEx(X)
#define _OCIExA(X) OCIE ## X ## A
#define OCIExA(X) _OCIExA(X)
#define _TIMER_COMPA_VECTOR(X) TIMER ## X ## _COMPA_vect
#define TIMER_COMPA_VECTOR(X) _TIMER_COMPA_VECTOR(X)

int pulseWidth = 1;
volatile int current_dim = 0;
int all_dim = 3;
int rise_fall = true;
char user_zero_cross = '0';

static int toggleCounter = 0;
static int toggleReload = 25;

static dimmerLamp* dimmer[ALL_DIMMERS];
volatile uint16_t dimPower[ALL_DIMMERS];
volatile uint16_t dimOutPin[ALL_DIMMERS];
volatile uint16_t zeroCross[ALL_DIMMERS];
volatile DIMMER_MODE_typedef dimMode[ALL_DIMMERS];
volatile ON_OFF_typedef dimState[ALL_DIMMERS];
volatile uint16_t dimCounter[ALL_DIMMERS];
static uint16_t dimPulseBegin[ALL_DIMMERS];
volatile uint16_t togMax[ALL_DIMMERS];
volatile uint16_t togMin[ALL_DIMMERS];
volatile bool togDir[ALL_DIMMERS];

dimmerLamp::dimmerLamp(int user_dimmer_pin):
    dimmer_pin(user_dimmer_pin)
{
    current_dim++;
    dimmer[current_dim-1] = this;
    current_num = current_dim-1;
    timer_num = DIMMER_TIMER;
    toggle_state = false;

    dimPulseBegin[current_dim-1] = 1;
    dimOutPin[current_dim-1] = user_dimmer_pin;
    dimCounter[current_dim-1] = 0;
    zeroCross[current_dim-1] = 0;
    dimMode[current_dim-1] = NORMAL_MODE;
    togMin[current_dim-1] = 0;
    togMax[current_dim-1] = 1;
    pinMode(user_dimmer_pin, OUTPUT);
}

void dimmerLamp::timer_init(void)
{
    TCCRxA(DIMMER_TIMER) &= ~(0xFF); // clean TCCRxA register
    TCCRxB(DIMMER_TIMER) &= ~(0xFF); // clean TCCRxB register

    TIMSKx(DIMMER_TIMER) |= (1 << OCIExA(DIMMER_TIMER)); //уÑтанавливаем бит Ñ€Ð°Ð·Ñ€ÐµÑˆÐµÐ½Ð¸Ñ Ð¿Ñ€ÐµÑ€Ñ‹Ð²Ð°Ð½Ð¸Ñ 1ого Ñчетчика по Ñовпадению Ñ OCR1A(H и L)

    TCCRxB(DIMMER_TIMER) = TCCRxB_VALUE;
    if (DIMMER_TIMER != 2) OCRxAH(DIMMER_TIMER) = OCRxAH_VALUE;
    OCRxAL(DIMMER_TIMER) = OCRxAL_VALUE;

    TIMSKx(DIMMER_TIMER) |= (1 << TOIEx(DIMMER_TIMER)); //timer interrupt enable
}

void dimmerLamp::ext_int_init(void) 
{ 
    EICRX &= ~0xFF; 

    EIMSK |= (1 << INTx); 
    EICRX |= (1 << ISCx1)|(1 << ISCx0);//0b00001100 
}


void dimmerLamp::begin(DIMMER_MODE_typedef DIMMER_MODE, ON_OFF_typedef ON_OFF)
{
    dimMode[this->current_num] = DIMMER_MODE;
    dimState[this->current_num] = ON_OFF;
    timer_init();
    ext_int_init(); 
}

void dimmerLamp::setPower(int power)
{   
    if (power >= 99) 
    {
        power = 99;
    }
    dimPower[this->current_num] = power;
    dimPulseBegin[this->current_num] = powerBuf[power];

    delay(1);
}

int dimmerLamp::getPower(void)
{
    if (dimState[this->current_num] == ON)
        return dimPower[this->current_num];
    else return 0;
}

void dimmerLamp::setState(ON_OFF_typedef ON_OFF)
{
    dimState[this->current_num] = ON_OFF;
}

bool dimmerLamp::getState(void)
{
    bool ret;
    if (dimState[this->current_num] == ON) ret = true;
    else ret = false;
    return ret;
}

void dimmerLamp::changeState(void)
{
    if (dimState[this->current_num] == ON) dimState[this->current_num] = OFF;
    else 
        dimState[this->current_num] = ON;
}

DIMMER_MODE_typedef dimmerLamp::getMode(void)
{
    return dimMode[this->current_num];
}

void dimmerLamp::setMode(DIMMER_MODE_typedef DIMMER_MODE)
{
    dimMode[this->current_num] = DIMMER_MODE;
}

void dimmerLamp::toggleSettings(int minValue, int maxValue)
{
    if (maxValue > 99) 
    {
        maxValue = 99;
    }
    if (minValue < 1) 
    {
        minValue = 1;
    }
    dimMode[this->current_num] = TOGGLE_MODE;
    togMin[this->current_num] = minValue;
    togMax[this->current_num] = maxValue;

    toggleReload = 50;
}

ISR(INT_vect)
{
    for (int i = 0; i < current_dim; i++ ) 
        if (dimState[i] == ON) 
        {
            zeroCross[i] = 1;
        }
}

static int k;
ISR (TIMER_COMPA_VECTOR(DIMMER_TIMER))
{   
    toggleCounter++;
    for (k = 0; k < current_dim; k++)
    {
        if (zeroCross[k] == 1 )
        {
            dimCounter[k]++;

            if (dimMode[k] == TOGGLE_MODE)
            {
            /*****
             * TOGGLE DIMMING MODE
             *****/
            if (dimPulseBegin[k] >= togMax[k])  
            {
                // if reach max dimming value 
                togDir[k] = false;  // downcount                
            }
            if (dimPulseBegin[k] <= togMin[k])
            {
                // if reach min dimming value 
                togDir[k] = true;   // upcount
            }
            if (toggleCounter == toggleReload) 
            {
                if (togDir[k] == true) dimPulseBegin[k]++;
                else dimPulseBegin[k]--;
            }
            }

            /*****
             * DEFAULT DIMMING MODE (NOT TOGGLE)
             *****/
            if (dimCounter[k] >= dimPulseBegin[k] )
            {
                digitalWrite(dimOutPin[k], HIGH);   
            }

            if (dimCounter[k] >=  (dimPulseBegin[k] + pulseWidth) )
            {
                digitalWrite(dimOutPin[k], LOW);
                zeroCross[k] = 0;
                dimCounter[k] = 0;
            }
        }
    }

    if (toggleCounter >= toggleReload) toggleCounter = 1;
    TIFRx(DIMMER_TIMER) |= ((1<<OCFxB(DIMMER_TIMER))|(1<<OCFxA(DIMMER_TIMER))); 
}

#endif

Here is the github link to the library: https://github.com/RobotDynOfficial/RBDDimmer

Can someone help me to use the library and my encoder ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.