#include <Keypad.h>
// #include <Stepper.h>
const uint8_t ROWS = 4;
const uint8_t COLS = 4;
const int stepPin = 12;
const int dirPin = 11;
const int red = 13;
const int yellow = 10;
const int green = 2;
int all_steps = 0;
// Stepper stepper = Stepper(1, stepPin, dirPin);
// step(100);
char keys[ROWS][COLS] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
};
uint8_t colPins[COLS] = { 5, 4, 3 }; // Pins connected to C1, C2, C3, C4
uint8_t rowPins[ROWS] = { 9, 8, 7, 6 }; // Pins connected to R1, R2, R3, R4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup(){
Serial.begin(9600);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
digitalWrite(red, LOW);
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);
}
String steps = "";
void loop(){
char key = keypad.getKey();
if (key != NO_KEY && key != '#') {
Serial.print(key);
steps= steps + key;
} else if (key != NO_KEY && key == '#'){
int stepsVal = steps.toInt();
all_steps += stepsVal;
Serial.println();
Serial.print("Steps: ");
Serial.println(all_steps);
if (all_steps > 1000) {
digitalWrite(red, HIGH);
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);
}
if (all_steps > 2000) {
digitalWrite(yellow, HIGH);
digitalWrite(red, LOW);
digitalWrite(green, LOW);
}
if (all_steps > 3000) {
digitalWrite(green, HIGH);
digitalWrite(red, LOW);
digitalWrite(yellow, LOW);
}
if (stepsVal == 0) {
stepsVal = 180;
}
for(int x = 0; x < stepsVal; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(100000);
digitalWrite(stepPin,LOW);
delayMicroseconds(100000);
}
steps = "";
}
}
/*digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 200; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000); // One second delay
digitalWrite(dirPin,LOW); //Changes the rotations direction
// Makes 400 pulses for making two full cycle rotation
for(int x = 0; x < 400; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);*/