I want to calculate the number of pulses coming from motor1 encoder pin2 (Int0) as input , and motor 2 encoder pin3 (Int1), I am working now for the first motor (just for the first motor ) so I set timer 2 to count 50 ms then throw flag in this moment I have to send the number of pulses to my screen, but not succeeded cause i see on my screen zeros and ascii caracters but not the number of pulses.:this my code .
volatile int pulses = 0;
int flag=0;
int setbit = 0;
int encoder_in = 2;
void count() {
pulses++;
}
void setup()
{
Serial.begin(115200);
pinMode(encoder_in, INPUT);
digitalWrite(2, HIGH); //Enable pullup
// initialize timer2
noInterrupts(); // disable all interrupts
TCCR2A = 0x00;
TCCR2B = 0x07;
TCNT2 = 0x64;
//EIMSK = 0x03; // Enable external interrupt INT0
//EICRA = 0x0f; // Trigger INT0 on falling edge
TIMSK2 |= 0x01; // enable timer compare interrupt
interrupts(); // enable all interrupts
attachInterrupt(0,count,RISING);
}
ISR(TIMER2_OVF_vect)
{
TCNT2 = 0x64; // preload timer
setbit=setbit+1;
if(setbit==5)
{
flag=1;
setbit=0;
}
}
//ISR(EXT_INT0_vect)
//{ pulses++; }
void loop()
{
if (flag==1)
{
Serial.println(pulses);
pulses=0;
}
}
2 Answers 2
Your technique looks convoluted to me. How about this?
volatile bool counting;
volatile unsigned long events;
unsigned long startTime;
const unsigned long INTERVAL = 50; // ms
void eventISR ()
{
if (counting)
events++;
} // end of eventISR
void setup ()
{
Serial.begin (115200);
Serial.println ();
pinMode (2, INPUT_PULLUP);
attachInterrupt (0, eventISR, RISING);
} // end of setup
void showResults ()
{
Serial.print ("I counted ");
Serial.println (events);
} // end of showResults
void loop ()
{
if (counting)
{
// is time up?
if (millis () - startTime < INTERVAL)
return;
counting = false;
showResults ();
} // end of if
noInterrupts ();
events = 0;
startTime = millis ();
EIFR = bit (INTF0); // clear flag for interrupt 0
counting = true;
interrupts ();
} // end of loop
Explanation
Using a timer to detect when 50 ms is up isn't particularly helpful in this case. You can't do printing inside an ISR, so you would have to set a flag that can be checked in the main loop.
If you are going to check a flag anyway, you may as well just check if the time is up, as my code does.
Inside the ISR, I stop adding to events
if the flag counting
is false. This is to stop events
being changed at the exact moment it is being printed.
-
that's work thank you Mr Nick, but I tried to calculate for both Motor but it not Works , I Tried many Times but not works, may you can see my code:omran– omran2015年08月20日 07:54:36 +00:00Commented Aug 20, 2015 at 7:54
-
whats the "Return" instruction in loop() exactly do. I am working to calculate for the second Motor but it not work.omran– omran2015年08月20日 09:06:31 +00:00Commented Aug 20, 2015 at 9:06
-
may you can see my code
- amend your question to add this new code so I can see it.2015年08月20日 09:23:01 +00:00Commented Aug 20, 2015 at 9:23 -
whats the "Return" instruction in loop() exactly do
- it returns from theloop()
function, which then gets called again.2015年08月20日 09:23:30 +00:00Commented Aug 20, 2015 at 9:23
Check the Baud Rate in Serial Monitor. If baud rate is not correct i.e. 9600 bps then it will show garbage.
Remove the Timer and Wire Libs
add this code in void setup()
attachInterrupt(0,void_counter,RISING);
the above is for the the external interrupt input.
and attach your sensor/incoming pulse wire on Pin 2 on arduino.
make a new function like this
void_counter(){
pulses++;
}
-
1I agree with this. But it is more appropriated as a comment.Pedro Quadros– Pedro Quadros2015年08月19日 12:40:06 +00:00Commented Aug 19, 2015 at 12:40
-
New to stack exchange.ammar.cma– ammar.cma2015年08月19日 12:43:45 +00:00Commented Aug 19, 2015 at 12:43
-
Also he already enabled the Interrupt, but by registersPedro Quadros– Pedro Quadros2015年08月19日 12:44:51 +00:00Commented Aug 19, 2015 at 12:44
-
i did this change before and now too i did what you told me, but I still see characters like {0¤0, A,P,. . }omran– omran2015年08月19日 13:16:55 +00:00Commented Aug 19, 2015 at 13:16
-
I see the pulses on oscilloscope but still I cant see the number of pulses on my monitromran– omran2015年08月19日 14:11:05 +00:00Commented Aug 19, 2015 at 14:11
Explore related questions
See similar questions with these tags.
volatile int pulses = 0;
tovolatile int pulses = 1;
and see if you now get1
s printed. Also which ascii characters do you see? Can you give us a sample output.