#include <Wire.h>
#include <U8glib.h> // Library for OlED display https://github.com/olikraus/u8glib/
//#include "logo.h"
#include "AccelStepper.h"
uint8_t slp = 4; // sleep enable//HIGH=slp on LOW=slp off
#define step_pin 3 // Pin connected to Step pin
#define dir_pin 2 // Pin connected to Direction pin
#define MS1 5 // Pin connected to MS1 pin
#define MS2 6 // Pin connected to MS2 pin
#define MS3 7 // Pin connected to MS3 pin
byte buttonCWpin = 11; // green button
#define change_switch 10 // red button select stepping type
#define Slider_Pin A0 // Arduino A0 analog out of the Slider
#define U8G_I2C_OPT_NONE 0
const char* ver = "Ver: nano_SpdCtrl_v2.1x"; // 03.11.2024
#define logo_data_width 127
#define logo_data_height 27
// 'logo127x27', 127x27px reverse
const unsigned char logo_data [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0xc6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
0xc6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0xce, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x80, 0x70, 0xee, 0x1c, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x80, 0xf0, 0xec, 0x1e, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
0xe0, 0xff, 0x0f, 0x30, 0x80, 0x01, 0xc3, 0xc1, 0xe0,
0xbf, 0x07, 0x03, 0x07, 0xfe, 0x33, 0x98, 0xc0, 0xff,
0x07, 0x38, 0x80, 0x83, 0xe3, 0xe1, 0xf1, 0x9f, 0x07,
0x87, 0x0f, 0xff, 0x77, 0x9c, 0x80, 0xc7, 0xf3, 0x38,
0x80, 0x83, 0xe3, 0xe1, 0x71, 0x80, 0x0f, 0x87, 0x0f,
0x07, 0x77, 0x9c, 0x00, 0xbb, 0xff, 0x38, 0x80, 0x83,
0xe3, 0xf3, 0x71, 0x80, 0x1f, 0x87, 0x0f, 0x07, 0xe7,
0x8e, 0x00, 0x7c, 0x7f, 0x38, 0x80, 0x83, 0xe3, 0xf3,
0x71, 0x80, 0x3f, 0xc7, 0x1d, 0x07, 0xe7, 0x87, 0xfe,
0x7d, 0x03, 0x38, 0x80, 0x83, 0xe3, 0xf3, 0xf1, 0x9f,
0x7f, 0xc7, 0x1d, 0x87, 0xc7, 0x87, 0xfe, 0x7d, 0x0f,
0x38, 0x80, 0x83, 0x73, 0xbf, 0xf1, 0x8f, 0xf3, 0xc7,
0x18, 0xff, 0x83, 0x83, 0x00, 0xba, 0xff, 0x38, 0x80,
0x83, 0x73, 0xbf, 0x73, 0x80, 0xe3, 0xe7, 0x38, 0xf7,
0x80, 0x83, 0x00, 0xc7, 0xff, 0x38, 0x80, 0x83, 0x73,
0x9f, 0x73, 0x80, 0xc3, 0xe7, 0x38, 0xc7, 0x81, 0x83,
0x80, 0xff, 0x03, 0x38, 0x80, 0xc3, 0x73, 0x9e, 0x73,
0x80, 0x83, 0x67, 0x38, 0x87, 0x83, 0x83, 0xe0, 0xff,
0x0f, 0xf8, 0x9f, 0xff, 0x33, 0x1e, 0xf3, 0xbf, 0x83,
0x77, 0x70, 0x07, 0x87, 0x83, 0xe0, 0xee, 0x1e, 0xf0,
0x1f, 0xff, 0x31, 0x0c, 0xe3, 0x3f, 0x83, 0x73, 0x70,
0x06, 0x86, 0x83, 0xf8, 0xee, 0x1c, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x38, 0xee, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00,
0xce, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0xc6, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
};
const int maxSpeedLimit = 1000.0; // set this to the maximum speed you want to use.
int motorSpeed = map(Slider_Pin, 0, 1023, 5, maxSpeedLimit);
AccelStepper stepper(1, step_pin, dir_pin);
U8GLIB_SH1106_128X64 Oled_Screen(U8G_I2C_OPT_NONE);
/* Configure type of Steps :
// MS1 MS2 MS3
// LOW LOW LOW = Full Step //
// HIGH LOW LOW = Half Step //
// LOW HIGH LOW = A quarter of Step //
// HIGH HIGH LOW = An eighth of Step //
// HIGH HIGH HIGH = A sixteenth of Step //
*/
//byte ledPin = 13;
boolean buttonCWpressed = false;
int stepcount = 0;
unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 25; //was 25 milliseconds
int change_switch_state = 0; // used to debounce the switch
int current_stepping = 1; // Used to store the current stepping type (0-5)
int Slider_Value; // Used to save the current Analog Slider Value
int Stepper_Speed; // Used to set the travel speed of the stepper motor
int Stepper_Direction;
int speed_v = 0;
const char* what[8] = {};
int z = 20;
int y = 62;
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
float power_mW = 0;
int xmax = 64; // max length x-axis
int ymax = 32; // max length y-axis
int xcenter = 94; // center of x-axis
//int xcenter = xmax / 2; // center of x-axis
int ycenter = 30; // center of y-axis
//int ycenter = ymax; // center of y-axis
int arc = ymax / 2;
int angle = 0;
//char* label[] = { "SPEED"}; // some custom gauge labels
//int labelXpos[] = { 0, 83 }; // predefined x-position of a gauge label
int p, w, m;
u8g_uint_t xx = 0;
void drawLogo(void) {
Oled_Screen.firstPage();
do {
Oled_Screen.setContrast(100); // Set oled contrast at startup
Oled_Screen.setFont(u8g_font_8x13Br);
Oled_Screen.drawStr(3, 12, " Press Red ");
Oled_Screen.drawStr(3, 26, "Button To Start");
Oled_Screen.drawLine(0, 29, 132, 29);
Oled_Screen.drawXBMP(0, 28, 126, 27, logo_data);
Oled_Screen.drawLine(0, 55, 132, 55);
Oled_Screen.setFont(u8g_font_5x7);
// Oled_Screen.setPrintPos(labelXpos[0], 63);
Oled_Screen.print(ver);
} while (Oled_Screen.nextPage());
}
void setup() {
uint32_t currentFrequency;
stepper.setSpeed(analogRead(A0)); // Slider_Pin
// Oled_Screen.setRot180();
// drawLogo();
Serial.begin(115200);
while (!Serial) {
delay(1);
}
Serial.println("Starting Stepper Demo with millis()");
stepper.setMaxSpeed(maxSpeedLimit);
stepper.setSpeed(maxSpeedLimit / 5.0); // initial speed target
Stepper_Direction = stepper.currentPosition(); // Set the starting position of the stepper which is equal to zero at startup
pinMode(buttonCWpin, INPUT_PULLUP);
pinMode(change_switch, INPUT_PULLUP);
pinMode(slp, OUTPUT); // HIGH=slp on LOW=off
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(MS3, OUTPUT);
pinMode(dir_pin, OUTPUT);
pinMode(step_pin, OUTPUT);
// set stepping type to FULL Steps
digitalWrite(MS1, LOW);
digitalWrite(MS2, LOW);
digitalWrite(MS3, LOW);
Oled_Screen.setContrast(100); // Set oled contrast at startup
// Put stuff on OLED at startup
Oled_Screen.firstPage();
do {
digitalWrite(slp, HIGH);
drawLogo();
} while (Oled_Screen.nextPage());
}
void loop() {
curMillis = millis();
readButtons();
actOnButtons();
}
void loop2() { // red button
// Change Switch - Debounce using a delay() command
if (digitalRead(change_switch) == LOW && change_switch_state == 0) {
delay(20); // orig was 5 The higher the Delay the less chance of bouncing
change_switch_state = 0;
current_stepping++; // go thru cases
Serial.println("Change step -red- pressed - ");
switch (current_stepping) {
case 1:
digitalWrite(slp, HIGH);
Oled_Screen.firstPage();
do {
what[4] = "Stop";
Oled_Screen.drawStr(z, y, "Stopped");
drawStats();
} while (Oled_Screen.nextPage());
break;
case 2:
digitalWrite(slp, LOW);
digitalWrite(MS1, HIGH);
digitalWrite(MS2, LOW);
digitalWrite(MS3, LOW);
Oled_Screen.firstPage();
do {
what[4] = "Half";
Oled_Screen.drawStr(z, y, "Step = HALF");
drawStats();
} while (Oled_Screen.nextPage());
break;
case 3:
current_stepping = 0;
digitalWrite(slp, LOW);
digitalWrite(MS1, LOW);
digitalWrite(MS2, HIGH);
digitalWrite(MS3, LOW);
Oled_Screen.firstPage();
do {
what[4] = " 1/4";
Oled_Screen.drawStr(z, y, "Step = 1/4");
drawStats();
} while (Oled_Screen.nextPage());
break;
}
}
} // end loop2
void loop3() {
// read the sensor value:
int sensorReading = analogRead(A0);
digitalWrite(slp, LOW);
digitalWrite(step_pin, LOW);
digitalWrite(dir_pin, HIGH);
// map it to a the maximum speed range
// int motorSpeed = map(sensorReading, 0, 1023, 5, maxSpeedLimit);
// set the motor speed:
if (motorSpeed > 0) {
stepper.setSpeed((float)motorSpeed);
}
stepper.runSpeed();
}
void readSlider(void) {
p = analogRead(Slider_Pin); // get value from potmeter
w = map(p, 0, 1023, 0, 200); // map it between 0 and 100
stepper.setSpeed(Stepper_Speed);
stepper.setMaxSpeed(1000);
// Do this until the stepper as reached the new destination
// while (stepper.distanceToGo() != 0) { // if stepper hasn't reached new position
drawStats();
stepper.runSpeedToPosition(); // move the stepper until new position reached
//}
//Serial.println("Run -green- pressed - slider ");
}
void drawStats(void) {
speed_v = (Slider_Value);
char buf1[16];
char buf2[16];
char buf3[16];
// char buf3[16];
char dir;
Oled_Screen.setFont(u8g_font_8x13);
sprintf(buf1, "STATS");
sprintf(buf2, "p= %d", p); // % of slider 0-1023
sprintf(buf3, "mspd= %d", w);
Oled_Screen.drawStr(0, 10, buf1);
Oled_Screen.drawStr(0, 25, buf2);
Oled_Screen.drawStr(0, 45, buf3);
Oled_Screen.drawLine(0, 50, 132, 50);
// Serial.println("Run -green- pressed - stats end");
}
void readButtons() {
buttonCWpressed = 0;
if (digitalRead(buttonCWpin) == LOW) { // green button
buttonCWpressed = true; // start/stop motor
}
change_switch_state = 0;
if (digitalRead(change_switch) == 0) { // red button
loop2(); // steps - full to 1/16
}
}
void actOnButtons() {
if (buttonCWpressed == true) { // green button
//digitalWrite(dir_pin, HIGH);
int sensorReading = analogRead(A0);
digitalWrite(slp, LOW);
digitalWrite(step_pin, HIGH);
digitalWrite(dir_pin, LOW);
// map it to a the maximum speed range
int motorSpeed = map(sensorReading, 0, 1023, 45, 80);
// set the motor speed:
if (motorSpeed > 0) {
stepper.setSpeed((float)motorSpeed);
}
stepper.runSpeed();
singleStep();
// stepper.setSpeed(motorSpeed);
// digitalWrite(step_pin, HIGH);
// digitalWrite(step_pin, LOW);
char buf4[16];
Oled_Screen.firstPage();
do {
readSlider();
drawStats();
// digitalWrite(slp, LOW);
sprintf(buf4, "Running %c", what[4]);
Oled_Screen.drawStr(30, y, what[4]);
Oled_Screen.drawStr(60, y, " step");
Serial.println(what[4]);
} while (Oled_Screen.nextPage());
}
}
void singleStep() {
// read the sensor value:
int sensorReading = analogRead(A0);
digitalWrite(slp, LOW);
digitalWrite(step_pin, HIGH);
digitalWrite(dir_pin, HIGH);
// map it to a the maximum speed range
int motorSpeed = map(sensorReading, 0, 1023, 5, maxSpeedLimit);
// set the motor speed:
if (motorSpeed > 0) {
stepper.setSpeed((float)Stepper_Speed);
}
stepper.runSpeed();
//}
}
void whatsUp(int line, String info) {
if (line == 1) {
Oled_Screen.setCursorPos(0, 0);
Oled_Screen.println(info);
}
if (line == 2) {
Oled_Screen.setCursorPos(0, 16);
Oled_Screen.println(" ");
Oled_Screen.setCursorPos(0, 16);
Oled_Screen.println(info);
Oled_Screen.drawXBMP(0, 33, 127, 27, logo_data); // Oled_Screen.display();
}
}