/* Arduino Control Stepper with Keypad and LCD
Created by Yvan / https://Brainy-Bits.com
This code is in the public domain...
You can: copy it, use it, modify it, share it or just plain ignore it!
Thx!
*/
#include "AccelStepper.h" // AccelStepper Library
#include "U8glib.h" // U8glib for Nokia LCD
// Variables to hold entered number on Keypad
volatile int firstnumber=99; // used to tell how many numbers were entered on keypad
volatile int secondnumber=99;
volatile int thirdnumber=99;
volatile int run_time=5;
volatile int speed=50;
volatile int screen=0;
bool busy = false;
// Variables to hold Distance and CurrentPosition
int keyfullnumber=0; // used to store the final calculated distance value
String currentposition = ""; // Used for display on Nokia LCD
// U8glib Setup for I2C 12864 OLED
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);
//SDA = A4, SCL = A5
// AccelStepper Setup
AccelStepper stepper(1, A0, A1); // 1 = Easy Driver interface
// Arduino A0 connected to STEP pin of Easy Driver
// Arduino A1 connected to DIR pin of Easy Driver
#define EncCLK 2
#define EncDT 1
#define EncSW 3
void readEncoder() {
if (screen == 1){
if (digitalRead(EncDT) == false) {
run_time++;
Serial.print("run_time = ");
Serial.println(run_time);
}
if (digitalRead(EncDT) == true) {
run_time--;
Serial.print("run_time = ");
Serial.println(run_time);
if (run_time<=0) run_time = 1;
}
}
if (screen == 2){
if (digitalRead(EncDT) == false) {
speed++;
Serial.print("speed = ");
Serial.println(speed);
if (speed>=21) speed = 20;
}
if (digitalRead(EncDT) == true) {
speed--;
Serial.print("speed = ");
Serial.println(speed);
if (speed<=0) speed = 1;
}
}
}
void readSwitch() {
if (screen < 3) screen++;
if (screen == 3) {
//stepper.setMaxSpeed(speed*100);
screen++;
//busy = true;
}
if (screen == 4) {
screen--;
//busy = false;
}
//displayscreen();
Serial.println(screen);
}
void setup(void) {
// AccelStepper speed and acceleration setup
stepper.setMaxSpeed(1500); // Not to fast or you will have missed steps
stepper.setAcceleration(400); // Same here
pinMode (EncCLK,INPUT);
pinMode (EncDT,INPUT);
pinMode (EncSW,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(EncCLK), readEncoder, FALLING );
attachInterrupt(digitalPinToInterrupt(EncSW), readSwitch, FALLING );
Serial.begin(9600);
}
void loop(){
//Nothing
}
//movestepper(keyfullnumber);
/*
void movestepper(int z) { // Move the stepper
int calculatedmove=((z*1600)/80); // Calculate number of steps needed in mm
stepper.runToNewPosition(calculatedmove);
currentposition = String(z);
u8g.firstPage();
do {
u8g.drawHLine(0, 15, 84);
u8g.drawVLine(50, 16, 38);
u8g.drawHLine(0, 35, 84);
u8g.setFont(u8g_font_profont11);
u8g.drawStr(0, 10, "ENTER DISTANCE");
u8g.drawStr(62, 29, "MM");
u8g.drawStr(4, 46, "cur-pos");
u8g.setPrintPos(57,47);
u8g.print(currentposition);
}
while( u8g.nextPage() );
}
void resetnumbers() { // Reset numbers for next entry
firstnumber=99;
secondnumber=99;
thirdnumber=99;
}
void drawnokiascreen(String y) {
u8g.firstPage();
do {
u8g.drawHLine(0, 15, 84);
u8g.drawVLine(50, 16, 38);
u8g.drawHLine(0, 35, 84);
u8g.setFont(u8g_font_profont11);
u8g.drawStr(0, 10, "ENTER DISTANCE");
u8g.setPrintPos(0,29);
u8g.print(y); // Put entered number on Nokia lcd
u8g.drawStr(62, 29, "MM");
u8g.drawStr(4, 46, "cur-pos");
u8g.setPrintPos(57,47);
u8g.print(currentposition); // Display current position of stepper
}
while( u8g.nextPage() );
}*/