Hi. In my project, I use two interrupts. One from timer1 (library TimerOne) for working with the infrared remote control, the other from timer2 (library MsTimer2) for reversing the DC motor. Individually, each interrupt works fine, but together there is only the Timer1 interrupt works. I am new, but I suspect that Timer1 prohibits interruption from Timer2.
I will be grateful for any hint.
Since you are not a new member, you should know we need to see ALL your code in code tags, any error logs or other serial output also in code tags, and a wiring diagram (a photo of hand drawn is fine)
I am fairly sure I know what the problem is but will wait until you post what is asked.
no, it runs independently
I suspect that is not the case at all.
But until you put in the effort to show your sketch, properly formatted with the <CODE/> tool, I'm not going to put any more effort into speculation.
This is a sketch at the moment. IR remote is working, LEDs are flashing. So Timer1 is fine. But the motor does not turn on, although the command is accepted from the remote control, I checked it. So Timer2 does not generate an interrupt.
//Controling RGB led & Motor
//Exploring TimerOne interrupt for On/Off lights and motor
//*******************************************
#define PIN_RECV 3//Pin 3/D3 Input IR DATA
#include <TimerOne.h>
#include <IRremote.hpp>
#include <MsTimer2.h>
//=============================================
volatile int enableLeds;//Global variable enable leds
volatile int enableMotor;
volatile int enableMusic;
volatile int enablePauseMusic;
int redPin= 5;//D5
int greenPin = 6;//D6
int bluePin = 11;//D11
int motorPin1=10;int valueMot1;
int motorPin2=12;int valueMot2;
int countPulse=0;
//======================================================
void setup()
{
pinMode(3,OUTPUT);
pinMode(7,OUTPUT);//pin NEXT MUSIC
pinMode(8,OUTPUT);//pin PREVIOS MUSIC
pinMode(4,OUTPUT);//pin PAUSE MUSIC
pinMode(motorPin1,OUTPUT);//motor pin
pinMode(motorPin2,OUTPUT);//motor pin
MsTimer2 :: set(1000,isrFunc); //set timer2
MsTimer2 :: start(); //start timer2
Serial.begin(9600);
IrReceiver.begin(PIN_RECV); // Initializes the IR
//=====================================
//Pins for RGB output LEDS:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
//For Interrupt Timer1
Timer1.initialize(200000);//Initialisation TimerOne
Timer1.attachInterrupt(callback);//TimerOne interrupt procedure
}
//=====================================
int callback() //ISR function for Timer1
{
if (IrReceiver.decode()) //IR Decoding initialization
{
if(IrReceiver.decodedIRData.command==69)//if got IR code 0x34
{
enableLeds=1;// start LEDs blinking
}
else if(IrReceiver.decodedIRData.command==71)// got IR code 0x44
{
enableLeds=0;// stop LEDs blinking
}
if(IrReceiver.decodedIRData.command==68)// got IR code 0x35
{
enableMotor=1;//start motor
}
else if(IrReceiver.decodedIRData.command==67)// got IR code 0x45
{
enableMotor=0;//stop motor
}
IrReceiver.resume(); // Enables NEXT code receiving
}
}
//====================================
int isrFunc()//ISR function for Timer2
{
if (enableMotor==1)
{
digitalWrite(motorPin1,HIGH);
digitalWrite(motorPin2,LOW);
}
// digitalWrite (motorPin1,!digitalRead(motorPin2));
// digitalWrite (motorPin1,!digitalRead(motorPin1));
// digitalWrite (motorPin2,!digitalRead(motorPin2));
}
//=====================================
int RGB_BLINK() // function RGB blinking
{
setColor(0, 255, 255); // set RGB red
delay(1000);
setColor(255, 0, 255); // set RGB GREEN
delay(1000);
setColor(255, 255, 0); // set RGB BLUE
delay(1000);
setColor(80, 80, 255); // set RGB PURPLE
delay(1000);
}
//=====================================================
//Function of color setting
void setColor(int redValue, int greenValue, int blueValue)
{
analogWrite(redPin, redValue);//PWM pulses for RED
analogWrite(greenPin, greenValue);//PWM pulses for GREEN
analogWrite(bluePin, blueValue);//PWM pulses for BLUE
}
//=====================================
void loop()
{
if(enableLeds==1)
{RGB_BLINK();}
else if(enableLeds==0){setColor(255,255,255);} //off leds}
//-------------------------------------------
}
//*******************END OF PROGRAMM********************************
How have you worked around the fact that this library already uses one of the timers that you are attempting to reuse?
I read that remote uses timer1
Really.
And did you alter the IRremote library to actually make that happen? Because when I look in the library, this is what I see.
// Macros for enabling timers for development
//#define SEND_PWM_BY_TIMER
//#define IR_USE_AVR_TIMER1
//#define IR_USE_AVR_TIMER2
.
.
.
// Arduino Duemilanove, Diecimila, LilyPad, Mini, Fio, Nano, etc
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328PB__) || defined(__AVR_ATmega168__) \
|| defined(__AVR_ATmega88P__) || defined(__AVR_ATmega88PB__)
# if !defined(IR_USE_AVR_TIMER1) && !defined(IR_USE_AVR_TIMER2)
//#define IR_USE_AVR_TIMER1 // send pin = pin 9
#define IR_USE_AVR_TIMER2 // send pin = pin 3
# endif
Or are you not using a 328P based board? That's something you didn't see fit to mention.
Thank you. I got it. So remote uses timer1 and timer2. I will think. Thanks again.
Sigh. No, that's not what I wrote. That's not what the code snippet above says. That's not what the library's README says. I give up.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.