Hi,
I have a small project on the go ( see attached ), that will be used to interface to a stepper motor driver. It consists of a Sparkfun rotary encoder used to change speed numbers on a 7-segment display, with additional button for motor stop/start and toggle switch for motor direction, all powered by USB.
Everything works great but occasionally I get odd behaviour which occurs on power up. I did a test where I repeatedly disconnected/reconnected power by pulling out the USB cable and re-inserting. Most of the time it fires up fine and I am able to turn the encoder knob and the numbers change as they should. Three problems occur randomly in about 1 in 10 power ups, these are:
- The display reads '0' but turning the knob does not change the display ( when I press the start/stop button it starts to work).
- The knob changes the display but the numbers are out of step with the rotation of the encoder.
- The display reads '0' but almost immediately the Arduino resets itself ( sometimes when in the process of changing the display ).
The setup
-
The display is driven by Digital pins 4-11. Each LED segment is driven by one of these pins, each having a resistor to give the correct amount of sinking current. These pins are set to OUTPUT and HIGH and current is sunk through these pins on a LOW and is no more than 15mA sunk on each pin).
-
The stop/start button is attached to pin 3, set up as an INPUT with pullups on.
-
The encoder is hooked up to Analogue input pins 0 and 1 and these are set as INPUTS with pullups on. The encoder pulls these pins to ground.
-
The stop/start button has an integral, independent LED that is lit when the button is pressed. The power is provided by Analogue pin 2, set to OUTPUT. Current drawn is through a 200ohm resistor drawing around 10mA.
-
The toggle switch is attached to Analogue pin 3, set up as an INPUT with pullups on.
-
Analogue pins 4 and 5 are setup as OUTPUTS and are used to provide the direction and frequency inputs to the stepper driver.
The initialisation code:
...
#define BUTT_STEP 3 // Stepper start/stop switch interrupt pin
#define ESTOP 2 // Emergency stop interrupt.
// Definitions for Analog port pins
#define ENC_A 14 // Encoder input A
#define ENC_B 15 // Encoder input B
#define BUTT_LED 16 // Power for button LED
#define SWCH_DIR 17 // Switch input for direction
#define DIR 18 // Output for direction input to microstepping drive
#define PUL 19 // Output for frequency input to microstepping drive
#define ENC_PORT PINC // Analog port
void setup()
{
// Set display pins for sinking current.
for( byte n=4;n<=11;n++)
{
pinMode(n, OUTPUT);
digitalWrite(n,HIGH);
}
// Setup stepper start/stop button
pinMode(BUTT_STEP, INPUT);
digitalWrite(BUTT_STEP, HIGH);
attachInterrupt(1, StartStopPressed, FALLING);
// Setup emergency stop interrupt
pinMode(ESTOP, INPUT);
digitalWrite(ESTOP, HIGH);
attachInterrupt(0, EmergenyStop, FALLING);
// Setup encoder pins as inputs
pinMode(ENC_A, INPUT);
digitalWrite(ENC_A, HIGH);
pinMode(ENC_B, INPUT);
digitalWrite(ENC_B, HIGH);
//Setup button LED power pin
pinMode(BUTT_LED, OUTPUT);
digitalWrite(BUTT_LED, HIGH);
//Setup direction switch pin
pinMode(SWCH_DIR, INPUT);
digitalWrite(SWCH_DIR, HIGH);
//Setup direction output pin
pinMode(DIR, OUTPUT);
digitalWrite(DIR, LOW);
//Setup frequency output pin
pinMode(PUL, OUTPUT);
digitalWrite(PUL, LOW);
}
Anyone have any ideas as to what the problem is? Is it a power supply issue? The fact that the Arduino occasionally resets itself leads me to think yes. Or, am I doing something fundamentally wrong in using the Analogue pins in this way? Is there a fundamental flaw in the initialisation code, maybe a different sequence...?
Any pointers greatly appreciated... thanks!