<?xml version="1.0" encoding="UTF-8"?>        <rss version="2.0"
             xmlns:atom="http://www.w3.org/2005/Atom"
             xmlns:dc="http://purl.org/dc/elements/1.1/"
             xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
             xmlns:admin="http://webns.net/mvcb/"
             xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
             xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <channel>
            <title>
									Mystery: Arduino pulse count interrupts with FALLING and RISING edge - Arduino				            </title>
            <link>https://forum.dronebotworkshop.com/arduino/mystery-arduino-pulse-count-interrupts-with-falling-and-rising-edge/</link>
            <description>Discussion board for Robotics, Arduino, Raspberry Pi and other DIY electronics and modules. Join us today!</description>
            <language>en-US</language>
            <lastBuildDate>Wed, 12 Aug 2026 17:41:31 +0000</lastBuildDate>
            <generator>wpForo</generator>
            <ttl>60</ttl>
							                    <item>
                        <title>RE: Mystery: Arduino pulse count interrupts with FALLING and RISING edge</title>
                        <link>https://forum.dronebotworkshop.com/arduino/mystery-arduino-pulse-count-interrupts-with-falling-and-rising-edge/#post-10135</link>
                        <pubDate>Fri, 10 Apr 2020 07:25:04 +0000</pubDate>
                        <description><![CDATA[Good news, everyone!I figured it out myself :-)In my motor controller code I have a section in setup() where I set up my motor pins. It looked like this:  // Initialize the motor pins and sp...]]></description>
                        <content:encoded><![CDATA[<p>Good news, everyone!</p><p>I figured it out myself :-)</p><p>In my motor controller code I have a section in setup() where I set up my motor pins. It looked like this:</p><pre>  // Initialize the motor pins and speed:<br />  for(uint8_t idx = 0; idx &lt; MOTORS; idx++) {<br />    // Pulse reading pins<br />    pinMode(motor_pulseA_pins, INPUT_PULLUP);<br />    pinMode(motor_pulseB_pins, INPUT_PULLUP);<br />    pinMode(motor_ENA_pins, OUTPUT);<br />    pinMode(motor_IN1_pins, OUTPUT);<br />    pinMode(motor_IN2_pins, OUTPUT);<br />    analogWrite(motor_ENA_pins, motor_PWM);<br />    digitalWrite(motor_IN1_pins, LOW);<br />    digitalWrite(motor_IN2_pins, LOW);<br />  }</pre><p>MOTORS holds the number of motors on my robot, currently set to 2, so in the above I loop over arrays of setup parameters, and set up my motor pins.</p><p>Notice the line</p><pre>analogWrite(motor_ENA_pins, motor_PWM);</pre><p>It has no index on the motor_ENA_pins array. Adding the index fixed it:</p><pre>analogWrite(motor_ENA_pins, motor_PWM);</pre><p>So all in all, analogWrite to an array can ruin your pulse counting... At least I learned something today :-)</p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/arduino/">Arduino</category>                        <dc:creator>mbogelund</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/arduino/mystery-arduino-pulse-count-interrupts-with-falling-and-rising-edge/#post-10135</guid>
                    </item>
				                    <item>
                        <title>Mystery: Arduino pulse count interrupts with FALLING and RISING edge</title>
                        <link>https://forum.dronebotworkshop.com/arduino/mystery-arduino-pulse-count-interrupts-with-falling-and-rising-edge/#post-10133</link>
                        <pubDate>Fri, 10 Apr 2020 06:15:18 +0000</pubDate>
                        <description><![CDATA[My fellow Dronebotters :-)I&#039;m building a robot that has an Arduino Nano controlling 2 motors with rotary encoders. The Nano also reads sensor input from LDR&#039;s, and communicates with a Raspbe...]]></description>
                        <content:encoded><![CDATA[<p>My fellow Dronebotters :-)</p><p>I'm building a robot that has an Arduino Nano controlling 2 motors with rotary encoders. The Nano also reads sensor input from LDR's, and communicates with a Raspberry Pi over I2C (Nano as slave).</p><p>I have encountered something that is a mystery to me, and I hope that one of you can help me understand what is going on.</p><p>I'm having an issue with counting pulses from the 2 motors' rotary encoders, and I have the following working test code:</p><pre>const byte interruptPin1 = 2;<br />const byte interruptPin2 = 3;<br />volatile int pulseCountLeft = 0;<br />volatile int pulseCountRight = 0;<br /><br />void setup() {<br />  Serial.begin(9600);<br />  pinMode(interruptPin1, INPUT_PULLUP);<br />  pinMode(interruptPin2, INPUT_PULLUP);<br />  attachInterrupt(digitalPinToInterrupt(interruptPin1), LeftCount, RISING);<br />  attachInterrupt(digitalPinToInterrupt(interruptPin2), RightCount, RISING);<br />}<br /><br />void loop() {<br />  if(pulseCountLeft &gt; 100) {<br />    Serial.println((String)"Left pulses: " + pulseCountLeft);<br />    pulseCountLeft = 0;<br />  }<br />  if(pulseCountRight &gt; 100) {<br />    Serial.println((String)"Right pulses: " + pulseCountRight);<br />    pulseCountRight = 0;<br />  }<br />}<br /><br />void LeftCount() {<br />  pulseCountLeft++;<br />}<br /><br />void RightCount() {<br />  pulseCountRight++;<br />}<br /><br /><br /></pre><p>This works exactly as expected; I turn the wheels on my robot manually, and for every 100 pulses I get a message in the serial monitor for the left or right motor, respectively:</p><pre>Right pulses: 101<br />Left pulses: 101<br />Left pulses: 101<br />Left pulses: 101<br />...</pre><p>In my actual controller program, I have to do the following to make pulse counting work:</p><pre>  // Attach interrupts to motor pulse pins<br />  attachInterrupt(digitalPinToInterrupt(motor_pulseA_pins), RisingMot1A, RISING);<br />  attachInterrupt(digitalPinToInterrupt(motor_pulseA_pins), RisingMot2A, FALLING);</pre><p>Ie. the difference here is that I have to attach one interrupt as RISING and the other as FALLING; if both are RISING or both are FALLING, the first ISR (RisingMot1A which is on digital pin 2) isn't called, only the other one works (RisingMot2A on pin 3).</p><p>My question is: What is causing this? What could be causing this? I've concluded that my hardware setup works, since everything works fine in my test sketch and when I use one FALLING and one RISING ISR, so I guess it's something about my I2C communication or analog reading of LDR's that is messing with my pulse counting.</p><p>Any hints about what to look for in my code is appreciated - thank you :-)</p><p>vkr Martin</p><p> </p>]]></content:encoded>
						                            <category domain="https://forum.dronebotworkshop.com/arduino/">Arduino</category>                        <dc:creator>mbogelund</dc:creator>
                        <guid isPermaLink="true">https://forum.dronebotworkshop.com/arduino/mystery-arduino-pulse-count-interrupts-with-falling-and-rising-edge/#post-10133</guid>
                    </item>
							        </channel>
        </rss>
		