Hello , i need some help to add a feature in this sketch.
This sketch controls a stepper up and down by two buttons and counts the steps, another button reset the count.
I need to add another button to do, if the count is positive, brings the motor to 0 count ,one step at a time spaced by one minute.
Thanks.
const int dirPin = 3;
const int stepPin = 4;
const int enPin = 5;
const int LATCH = 2; // pin 12 on 74hc595
const int CLK = 7; // pin 11 on 74hc595
const int DATA = 6; // pin 14 on 74hc595
const int switchUp = 8;
const int switchDown = 9;
const int switchReset = 10;
byte displayDigit[11] = {
0x3f, // 0
0x06, // 1
0x5b, // 2
0x4f, // 3
0x66, // 4
0x6d, // 5
0x7d, // 6
0x07, // 7
0x7f, // 8
0x6f, // 9
0x40 // -
};
int count = 0;
int UpbuttonState = 0;
int lastUpbuttonState = 0;
int DownbuttonState = 0;
int lastDownbuttonState = 0;
int ResetbuttonState = 0;
int lastResetbuttonState = 0;
bool bPress = false;
bool isUp = false;
bool isDown = false;
void setup() {
Serial.begin(9600);
pinMode( switchUp, INPUT_PULLUP);
pinMode( switchDown, INPUT_PULLUP);
pinMode( switchReset, INPUT_PULLUP);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
pinMode(LATCH, OUTPUT);
pinMode(CLK, OUTPUT);
pinMode(DATA, OUTPUT);
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLK, MSBFIRST, 113);
shiftOut(DATA, CLK, MSBFIRST, 64);
shiftOut(DATA, CLK, MSBFIRST, 109);
digitalWrite(LATCH, HIGH);
}
void loop() {
isUp = false;
isDown = false;
UpbuttonState = digitalRead(switchUp);
DownbuttonState = digitalRead(switchDown);
ResetbuttonState = digitalRead(switchReset);
if (UpButtonPress()) {
digitalWrite(dirPin,HIGH);
delay(5);
}
if (DownButtonPress()) {
digitalWrite(dirPin,LOW);
delay(5);
}
if( isUp || isDown ){
for(int x = 0; x < 5; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin,LOW);
delayMicroseconds(2000);
}
}
if (ResetButtonPress()) {
count = 0;
delay(5);
}
}
bool UpButtonPress()
{
bool isPress = false;
if (UpbuttonState != lastUpbuttonState) {
if (UpbuttonState == LOW) {
bPress = true;
isPress = true;
Serial.println(++count);
if (count >= 0) {
digitalWrite(LATCH, LOW); // Pull latch LOW to start sending data
shiftOut(DATA, CLK, MSBFIRST, displayDigit[ count % 10]);
shiftOut(DATA, CLK, MSBFIRST, displayDigit[ count / 10]); // Send the data
shiftOut(DATA, CLK, MSBFIRST, 0);
digitalWrite(LATCH, HIGH); // Pull latch HIGH to stop sending data
}
else if (count < 0){
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLK, MSBFIRST,displayDigit[abs(count % -10)]);
shiftOut(DATA, CLK, MSBFIRST,displayDigit[abs(count / -10)]);
shiftOut(DATA, CLK, MSBFIRST, 64);
digitalWrite(LATCH, HIGH);
}
} else {
Serial.println("Idle");
isUp = true;
}
delay(50);
}
lastUpbuttonState = UpbuttonState;
return isPress;
}
bool DownButtonPress()
{
bool isPress = false;
if (DownbuttonState != lastDownbuttonState) {
if (DownbuttonState == LOW) {
bPress = true;
isPress = true;
Serial.println(--count);
if (count >= 0){
digitalWrite(LATCH, LOW); // Pull latch LOW to start sending data
shiftOut(DATA, CLK, MSBFIRST, displayDigit[ count % 10]);
shiftOut(DATA, CLK, MSBFIRST, displayDigit[ count / 10]); // Send the data
shiftOut(DATA, CLK, MSBFIRST, 0);
digitalWrite(LATCH, HIGH); // Pull latch HIGH to stop sending data
}
else if (count < 0){
digitalWrite(LATCH, LOW);
shiftOut(DATA, CLK, MSBFIRST,displayDigit[abs(count % -10)]);
shiftOut(DATA, CLK, MSBFIRST,displayDigit[abs(count / -10)]);
shiftOut(DATA, CLK, MSBFIRST, 64);
digitalWrite(LATCH, HIGH);
}
} else {
Serial.println("Idle");
isDown = true;
}
delay(50);
}
lastDownbuttonState = DownbuttonState;
return isPress;
}
bool ResetButtonPress()
{
bool isPress = false;
if (ResetbuttonState != lastResetbuttonState) {
if (ResetbuttonState == LOW) {
bPress = true;
isPress = true;
Serial.println(count = count = 0);
digitalWrite(LATCH, LOW); // Pull latch LOW to start sending data
shiftOut(DATA, CLK, MSBFIRST,displayDigit[count]); // Send the data
shiftOut(DATA, CLK, MSBFIRST, 0);
shiftOut(DATA, CLK, MSBFIRST, 0);
digitalWrite(LATCH, HIGH); // Pull latch HIGH to stop sending data
} else {
Serial.println("Idle");
}
delay(50);
}
lastResetbuttonState = ResetbuttonState;
return isPress;
}