Hello. I've got a code where i want to input a number into my keypad, have the code "hold" or record that number, then use it to tell my stepper how many steps to go. So im inputting say 2 for 2 inches, that gets converted to steps and makes the stepper run. I've got a VERY, VERY ugly code right now but i just simply put everything together (see below), so please dont judge too harshly. I just want to know how do i input a number through a keypad to use to move my stepper to that length/position.
Thanks so much in advance. I'm still very new but i'm trying not to ask too much! I'm using the Arduino Mega.
#include <Keypad.h>
#include <LiquidCrystal.h>
//----- Encoder ----
int rs = 22, en = 23, d4 = 24, d5 = 25, d6 = 26, d7 = 27;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
int pulses;
int encoderA = 2;
int encoderB = 3;
int pulsesChanged = 0;
//----- LCD ----
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { 31, 32, 33, 34 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { 28, 29, 30 };
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// ----- Stepper ----
// defines pins numbers
const int stepPin = 35;
const int dirPin = 36;
const int enPin = 37;
void setup(){
pinMode(encoderA, INPUT);
pinMode(encoderB, INPUT);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);
attachInterrupt(0, A_CHANGE, CHANGE);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("Test Circuit");
}//setup
void loop(){
char key = kpd.getKey();
lcd.setCursor(9, 1);
if (key){
lcd.setCursor(9,1);
lcd.print("key= ");
lcd.print(key);
}
if (pulsesChanged != 0) {
pulsesChanged = 0;
lcd.setCursor(0, 1);
lcd.print(pulses);
}
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 < 800; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000); // One second delay
digitalWrite(dirPin,LOW); //Changes the rotations direction
for(int x = 0; x < 800; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000);
}
void A_CHANGE(){
if( digitalRead(encoderB) == 0 ) {
if ( digitalRead(encoderA) == 0 ) {
// A fell, B is low
pulses--; // moving reverse
} else {
// A rose, B is low
pulses++; // moving forward
}
}
// tell the loop that the pulses have changed
pulsesChanged = 1;
}