# Counting revolution of stepper - big number

When I counting revolution of motor (counting impulse) using:

`````` count;
if (stepPinState == HIGH && runStepper >= 1)
{
count++;
}
``````

I get from serial monitor very big number like: 0, 7290, 17181, 27059, 36927, 46809, interval=1s.. I think it must be something like 0, 200, 400, 600. What's wrong?

``````long count = 0;

if (customKey == 'A')             // stepper - left direction
{
runStepper = 1;
digitalWrite(sleepPin, HIGH);
digitalWrite(dirPin, HIGH);
}

if (runStepper >= 1)
{
unsigned long current1 = micros();

if(current1 - previous1 > interval)
{
previous1 = current1;
if (stepPinState == LOW)
stepPinState = HIGH;
else
stepPinState = LOW;
digitalWrite(stepPin, stepPinState);
}
}

count;
if (stepPinState == HIGH && runStepper >= 1)
{
count++;
}

unsigned long current2 = millis();
if(current2 - previous2 > 1000)
{
Serial.println(count);
previous2 = current2;
}
``````

Where's the rest of your code?

``````int relay1Pin = 23;   // relay
int relay2Pin = 25;
int relay3Pin = 27;
int relay4Pin = 29;

int sleepPin = 47;   // stepper
const int stepPin = 49;
int dirPin = 51;

int stepPinState = LOW;
long previous1 = 0;
long previous2 = 0;
long interval = 1000;
static byte runStepper = 0;
static byte autoStepper = 0;
long count = 0;

const byte ROWS = 4;   // four rows
const byte COLS = 4;   // four columns
// define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{
'1','2','3','A'  }
,
{
'4','5','6','B'  }
,
{
'7','8','9','C'  }
,
{
'*','0','#','D'  }
};
byte rowPins[ROWS] = {
22, 24, 26, 28};   // connect to the row pinouts of the keypad
byte colPins[COLS] = {
30, 32, 34, 36};   // connect to the column pinouts of the keypad

void setup()
{
Serial.begin(9600);

pinMode(relay1Pin, OUTPUT);     // relay
pinMode(relay2Pin, OUTPUT);
pinMode(relay3Pin, OUTPUT);
pinMode(relay4Pin, OUTPUT);

pinMode(sleepPin, OUTPUT);     // stepper
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);

}

void loop()
{

if (customKey == '1')             // relay 1 on
{
digitalWrite(relay1Pin,LOW);
}

if (customKey == '4')             // relay 1 off
{
digitalWrite(relay1Pin,HIGH);
}

if (customKey == '2')             // relay 2 on
{
digitalWrite(relay2Pin,LOW);
}

if (customKey == '5')            // relay 2 off
{
digitalWrite(relay2Pin,HIGH);
}

if (customKey == '3')             // relay 3 on
{
digitalWrite(relay3Pin,LOW);
}

if (customKey == '6')            // relay 3 off
{
digitalWrite(relay3Pin,HIGH);
}

if (runStepper == 0)             // stepper - stand by off
{
digitalWrite(sleepPin, LOW);
}

if (customKey == 'A')             // stepper - right direction
{
runStepper = 1;
digitalWrite(sleepPin, HIGH);
digitalWrite(dirPin, LOW);
}

if (customKey == 'B')             // stepper - left direction
{
runStepper = 2;
digitalWrite(sleepPin, HIGH);
digitalWrite(dirPin, HIGH);
}

if (runStepper >= 1)
{
unsigned long current1 = micros();

if(current1 - previous1 > interval)
{
previous1 = current1;
if (stepPinState == LOW)
stepPinState = HIGH;
else
stepPinState = LOW;
digitalWrite(stepPin, stepPinState);
}
}

count;
if (stepPinState == HIGH && runStepper >= 1)
{
count++;
}

if (customKey == 'C')    //stepper off
{
runStepper = 0;
autoStepper = 0;
digitalWrite(sleepPin, LOW);
}

int sensorValue = analogRead(A0);  // read the input on analog pin 0:
int currentValue = sensorValue * (3.3 / 1024.0)*100;
unsigned long current2 = millis();
if(current2 - previous2 > 1000)
{
Serial.println(currentValue);  // print out the value you read:
Serial.println(count);
previous2 = current2;
}

if (customKey == 'D')
{
autoStepper=1;
}

if (currentValue >= 244 &&  autoStepper==1)
{
runStepper = 3;
digitalWrite(sleepPin, HIGH);
digitalWrite(dirPin, HIGH);
}

if (currentValue < 244 && autoStepper==1)
{
runStepper = 0;
digitalWrite(sleepPin, LOW);
}

}
``````

what is the count;

statement supposed to do. RIght now it does nothing.

RMLR: I get from serial monitor very big number like: 0, 7290, 17181, 27059, 36927, 46809, interval=1s.. I think it must be something like 0, 200, 400, 600. What's wrong?

You're not counting the pulses, you're incrementing count every iteration of the loop when your two conditions are true. It sounds like you should be looking for transitions, like when stepPinState transitions from LOW to HIGH.

Thank you Arrch and KeithRB. This is my code.

`````` if (stepPinState != lastStepPinState) // if the state has changed, increment the counter
{
if (stepPinState == HIGH)
{
countImpulse++;
}
}
lastStepPinState = stepPinState;
``````