Interrupt is triggered unmotivated

Hello Community,
I have the following small test program written for Arduino Mega. I want to monitor using an encoder and a rail to rail circuit, the rotation speed.

//----------------------Declaration -------------------------------------------

#include <WProgram.h>

#define MOTORRIGHTPLUS 	  		24  	///< H-bridge leg 5       Input 1
#define MOTORRIGHTMINUS   		25  	///< H-bridge leg 7       Input 2
#define PWM_MOTORSPEEDRIGHT      3  	///< H-bridge leg 11      Enable B (PWM)

bool intro = false;

volatile int count = 0;

void counter();

//------------------ End of deklarations --------------------------------------
extern "C"
  {
    void __cxa_pure_virtual()
    {
      // put error handling here
    }
}
// -------------------------------- setup -------------------------------------
void setup()
{
	pinMode(MOTORRIGHTPLUS, OUTPUT);
	pinMode(MOTORRIGHTMINUS, OUTPUT);
	pinMode(PWM_MOTORSPEEDRIGHT, OUTPUT);

	attachInterrupt(4, counter, CHANGE);

	digitalWrite(MOTORRIGHTPLUS, HIGH);
	digitalWrite(MOTORRIGHTMINUS, LOW);
	analogWrite(PWM_MOTORSPEEDRIGHT, 50);

  	Serial.begin(19200);	// Output to Serialdebugging
  	Serial.println("**************************");
  	Serial.println("*  Encoder Test 1        *");
  	Serial.print  ("*  ");Serial.print(__DATE__);Serial.print(" ");Serial.print(__TIME__);Serial.println("  *");
  	Serial.println("**************************");
}
//------------------------------- Loop ----------------------------------------
void loop()
{
  	if(intro == false)	// Just to view the startscreen can be longer
  		delay (5000);
	  	intro = true;

	Serial.println(count);
}
//-----------------------------------------------------------------------------
void counter()
{
	count++;
}
//-------------------------------- main ---------------------------------------
  int main(void) {
    /* Must call init for arduino to work properly */
    init();
    setup();

    for (;;) {
  	  loop();
    } // end for
  } // end main
//------------------------- End of program ------------------------------------

Even when no encoder is connected, the variable “count” is incremented. :cold_sweat: How can help me?
Beautiful Easter Days.

Willi

I’m not an Interrupts maven.
Is that pin “floating”, unconnected, then?
If it is supposed to be ‘normally low’, I suggest a pull-down resistor (a resistor from that pin to Gnd.; some nominal value, 3K-10K?).
If it is supposed to be ‘normally high’, I suggest a pull-up resistor (a resistor from that pin to +5; some nominal value, 3K-10K?).

Wow, that was fast. The pin is not connected. I think it is a pull down resistor. I´ll try it.

Willi

Now there is peace :). The pull-down resistor (10k) was the solution. Count we no longer counted. I will join later times the encoder.
Best Regards Willio

Also, the encoder with rail-to-rail circuit works. What can be that the variable "count" indicates the same value several times. Up to 8 times :roll_eyes:.

Willi

The last question has done himself. :slight_smile:
Perhaps a stupid question. :~ The bug is not reproducible. With the small test program that I want to test the encoder. Times are recorded transitions correctly, both the left and right. Then, the transitions are reversed from left to right. Next time will be recorded only on the left, even though both wheels turn. I do not understand.

/*
 * _EncoderTest.cpp
 *
 *  Created on: 03.04.2012
 *      Author: Willi
 */
//----------------------Declaration -------------------------------------------

#include <WProgram.h>

#define MOTORLEFTMINUS	  		22  	///< H-bridge leg 12      Input 4
#define MOTORLEFTPLUS 	  		23  	///< H-bridge leg 10      Input 3
#define PWM_MOTORSPEEDLEFT	   	 2  	///< H-bridge leg 6       Enable A (PWM)

#define MOTORRIGHTPLUS 	  		24  	///< H-bridge leg 5       Input 1
#define MOTORRIGHTMINUS   		25  	///< H-bridge leg 7       Input 2
#define PWM_MOTORSPEEDRIGHT      3  	///< H-bridge leg 11      Enable B (PWM)

bool intro = false;
volatile int countRight = 0;
volatile int countLeft = 0;
void counterRight();
void counterLeft();

//------------------ End of deklarations --------------------------------------
extern "C"
  {
    void __cxa_pure_virtual()
    {
      // put error handling here
    }
}
// -------------------------------- setup -------------------------------------
void setup()
{
	pinMode(MOTORLEFTPLUS, OUTPUT);       			///< pin assignment for H-Bright
	pinMode(MOTORLEFTMINUS, OUTPUT);
	pinMode(PWM_MOTORSPEEDLEFT, OUTPUT);
	pinMode(MOTORRIGHTPLUS, OUTPUT);
	pinMode(MOTORRIGHTMINUS, OUTPUT);
	pinMode(PWM_MOTORSPEEDRIGHT, OUTPUT);

	attachInterrupt(4, counterRight, CHANGE);  			///< Pin 18 green - right
	attachInterrupt(5, counterLeft, CHANGE);			///< Pin 19 red - left

	digitalWrite(MOTORLEFTPLUS, HIGH);
	digitalWrite(MOTORLEFTMINUS, LOW);

	digitalWrite(MOTORRIGHTPLUS, HIGH);
	digitalWrite(MOTORRIGHTMINUS, LOW);

  	Serial.begin(19200);	// Output to Serialdebugging
  	Serial.println("**************************");
  	Serial.println("*  Encoder Test 1        *");
  	Serial.print  ("*  ");Serial.print(__DATE__);Serial.print(" ");Serial.print(__TIME__);Serial.println("  *");
  	Serial.println("**************************");
}
//------------------------------- Loop ----------------------------------------
void loop()
{
  	if(intro == false)	// Just to view the startscreen can be longer
  		delay (5000);
	  	intro = true;

		analogWrite(PWM_MOTORSPEEDLEFT, 100);
		analogWrite(PWM_MOTORSPEEDRIGHT, 250);

	  	Serial.print("Left - ");Serial.print(countLeft);Serial.print(" - Right - ");Serial.println(countRight);
}
//-----------------------------------------------------------------------------
void counterRight()
{
	countRight++;
}
//-----------------------------------------------------------------------------
void counterLeft()
{
	countLeft++;
}
//-------------------------------- main ---------------------------------------
  int main(void) {
    /* Must call init for arduino to work properly */
    init();
    setup();

    for (;;) {
  	  loop();
    } // end for
  } // end main
//------------------------- End of program ------------------------------------

I have the eagle-file for the encoder circuit attached. The 10k pulldown resistors connected directly to the board I have. Am grateful for any help.

Best regards Willi

Encoder_Main-OneSide.sch (202 KB)

Encoder_Main-OneSide.brd (36.7 KB)

Encoder_Main-OneSide.pro (982 Bytes)

Have the "error" found. I have interpreted the results incorrectly. In the small test program is all right. In the program itself, the results are wrong.
For example:
A wheel rotation should show 200 Encoder transitions. It will be displayed but only 68. Can someone tell me why is that?
I have the "volatile variable" declared globally in a structure.

Willi