#include "U8glib.h"
#include <StepperMSeries.h>
// change this to the number of steps on your motor
#define STEPS 200
#define PULSE_PIN 23
#define DIRECTION_PIN 25
#define ENABLE 27
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0 | U8G_I2C_OPT_NO_ACK | U8G_I2C_OPT_FAST);
StepperMSeries stepper(STEPS, PULSE_PIN, DIRECTION_PIN);
int pot_val = 0;
int pot_speed = 10;
char buffer[20];
int string_width;
//float pixel_x = 0;
//float pixel_y = 0;
//float line_x = 0;
//float line_y = 0;
//float text_x = 0;
//float text_y = 0;
//int center_x = 64;
//int center_y = 108;
//int radius_pixel = 92;
//int radius_line = 87;
//int radius_text = 75;
//int angle;
//int tick_val;
int dig_sw = 0;
int drill_pos = 0;
//unsigned long millis_time;
//unsigned long millis_time_last;
//int fps;
void setup() {
// put your setup code here, to run once:
u8g.setFont(u8g_font_tpssb);
u8g.setColorIndex(1);
//Read digital switch
pinMode(4,INPUT);
// set the speed at 60 rpm:
stepper.setSpeed(150);
pinMode(ENABLE,OUTPUT);
digitalWrite(ENABLE,HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
u8g.firstPage();
do{
u8g.setColorIndex(1);
u8g.setFont(u8g_font_6x10r); //set tick mark font
//Read digital switch state
if(dig_sw == 0){
//print REVERSE
u8g.drawStr(0,30,"Bottom..");
if(pot_val < 1000){
pot_val = pot_val + pot_speed;
}
}
else{
//print FORWARD
u8g.drawStr(0,30,"Top..");
if(pot_val > 0){
pot_val = pot_val - pot_speed;
}
}
//Draw Drill Post
u8g.drawRBox(125,0,3,64, 0);
//Draw Drill
drill_pos = map(pot_val/10,0,100,0,36);
u8g.drawRBox(112,drill_pos,13,20, 2);
u8g.drawBox(116,drill_pos+20,6,4);
u8g.drawBox(118,drill_pos+24,2,6);
//Draw Percentage
u8g.setFont(u8g_font_8x13r);
dtostrf(pot_val/10.0,1,1,buffer);
sprintf(buffer,"%s%s",buffer,"%");
string_width = u8g.getStrWidth(buffer);
u8g.setColorIndex(1);
u8g.drawRBox(64-string_width/2,0,string_width+1, 11, 2);
u8g.drawTriangle(64-3,11,64+4,11,64,15);
u8g.setColorIndex(0);
u8g.drawStr(64-string_width/2,10, buffer);
//Draw Speed
u8g.setFont(u8g_font_5x7r);
u8g.setColorIndex(1);
itoa(pot_speed, buffer, 10);
u8g.drawStr(0,10,buffer);
}while (u8g.nextPage());
stepper.step(STEPS);
pot_speed = map(analogRead(A0),0,1023,0,20);
dig_sw = digitalRead(4);
}