Go Down

Topic: Interrupt is triggered unmotivated (Read 889 times) previous topic - next topic

KUCKY

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.

Code: [Select]

//----------------------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. :smiley-roll-sweat: How can help me?
Beautiful Easter Days.

Willi

runaway_pancake

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?).
"Who is like unto the beast? who is able to make war with him?"
When all else fails, check your wiring!

KUCKY

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

Willi

KUCKY

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

KUCKY

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 :smiley-roll:.

Willi

KUCKY

The last question has done himself.  :)
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.
Code: [Select]

/*
* _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     ///< 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

KUCKY

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

Go Up