#include <Keypad.h>
#include <TM1637.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
#define Ap A0
#define Am A1
#define Bp A2
#define Bm A3
const uint8_t SWpin = 2;
uint8_t colPins[COLS] = { 5, 4, 3, 2 };// Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 };// Pins connected to R1, R2, R3, R4
const int CLK = 12;
const int DIO = 13;
int showCounter = 0, runCounter = 0;
int PBupPin = 0 , PBdownPin = 0;
int SWstatus = 0 , PBdownLastStatus = 0;
int PBupLastStatus = 0;
int i = 0 ;
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
TM1637 tm(CLK , DIO);
void setup() {
pinMode(Ap, OUTPUT); pinMode(Am, OUTPUT);
pinMode(Bp, OUTPUT); pinMode(Bm, OUTPUT);
Serial.begin(9600);
}
void StepperCW() {
if (i++ < 5){
delay(5);
digitalWrite(Bp, LOW); digitalWrite(Am, HIGH);
delay(5);
digitalWrite(Am, LOW); digitalWrite(Bm, HIGH);
delay(5);
digitalWrite(Bm, LOW); digitalWrite(Ap, HIGH);
delay(5);
digitalWrite(Ap, LOW); digitalWrite(Bp, HIGH);
}
}
void StepperCCW() {
if (i++ < 5){
delay(5);
digitalWrite(Bm, LOW); digitalWrite(Ap, HIGH);
delay(5);
digitalWrite(Ap, LOW); digitalWrite(Bp, HIGH);
delay(5);
digitalWrite(Bp, LOW); digitalWrite(Am, HIGH);
delay(5);
digitalWrite(Am, LOW); digitalWrite(Bm, HIGH);
}
}
char curKey, lastKey = 0, last2Key = 0 ;
int j = 0 , PBupPressed , PBdownPressed;
void loop() {
PBupPressed = digitalRead(PBupPin);
PBdownPressed = digitalRead(PBdownPin);
tm.display(3 , curKey);
if( ( PBupPressed == HIGH) || ( PBdownPressed == HIGH) ) {
if(PBupPressed == HIGH) PBupLastStatus = HIGH;
if(PBdownPressed == HIGH) PBdownLastStatus = HIGH;
Serial.print("PBupLastStatus:");
Serial.println(PBupLastStatus);
}
if ( ( PBupLastStatus == HIGH ) || ( PBdownLastStatus == HIGH ) )
curKey = keypad.getKey();
if (curKey != NO_KEY){
switch (curKey){
case '1':
showCounter = 1;
break;
case '2':
showCounter = 2;
break;
case '3':
showCounter = 3;
break;
case '4':
showCounter = 4;
break;
case '5':
showCounter = 5;
break;
case '6':
showCounter = 6;
break;
case '7':
showCounter = 7;
break;
case '8':
showCounter = 8;
break;
case '9':
showCounter = 8;
break;
}
}
SWstatus = digitalRead(SWpin);
curKey = keypad.getKey();
if (( curKey != NO_KEY) && ( SWstatus == 1) ) {
Serial.println(curKey);
showCounter = curKey - 48;
runCounter = curKey - lastKey;
if ( runCounter >= 48 ) {
runCounter = runCounter - 48;
}
Serial.println(showCounter);
Serial.println(runCounter);
lastKey = curKey;
tm.display(3, showCounter);
if ( runCounter >= 0) {
for (j = 0; j <= runCounter; j++ ) {
StepperCW();
Serial.println("Motor clock runing...");
Serial.println(j);
}
}
}
delay(100);
}