Hi All. I am working on an electronic lead screw for my lathe driven by a closed loop stepper.
For cutting threads I need to be able to first set the pitch of the tread in mm or TPI ( threads per inch ) . Currently I have a ky-40 encoder to set the numeric value of the thread and use the button to change between MM & TPI. This numeric value from the screen is set in a float and will then used for the stepper motor control. At present the code is focused on the input select, and once I get that right, I will move on
A typical metric thread would be 1.25mm and a typical imperial thread would be 20TPI. These are fine threads
It currently works ( basically ) but does not function as I thought it would.
When I first start I get "select" on the screen I have press the button, advance it 1 click then press the button again and get 1 TPI which increments when the encoder is turned.
for mm , from start I get "select" I increment the encoder by 1, then press button and get 0.01mm
If I want to switch between the 2 I need to decrease the count until select appears, then double click, advance 1 click and it will change.
What I want to happen is that I just want to press the switch once to change between modes.
I have tried different methods using interrupts etc, but none seem to work ( I am using a Teensy 4.0, but the code is exactly the same
#include <RotaryEncoder.h> //Rotary_Encoder_KY-040_Fixed-main/RotaryEncoder.h https://github.com/ownprox/Rotary_Encoder_KY-040_Fixed.....NOTE. this installs as RotaryEncoder.h, so make sure to remove any existing library
#include <Bounce.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSansBold12pt7b.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
#define BUTTON 6 // was pin 6
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int Counter = 0, LastCount = 0; //uneeded just for test
float Pitchmm ; // used for metric leadscrew calculation and stepper config
float Pitchtpi ; // used for imperial leadscrew calculation and stepper config
void RotaryChanged(); //we need to declare the func above so Rotary goes to the one below
RotaryEncoder Rotary(&RotaryChanged, 4, 5, 6 ); // Pins 4 (DT), 5 (CLK), 6 (SW)
void RotaryChanged()
{
const unsigned int state = Rotary.GetState();
if (state & DIR_CW)
Counter++;
if (state & DIR_CCW)
Counter--;
}
int buttonValue = LOW;
Bounce bouncer = Bounce( BUTTON, 5 );
void setup()
{
pinMode(BUTTON,INPUT_PULLUP);
Serial.begin(9600);
Serial.print("T.I.S.M"); // Death to art tour, featuring songs from their hit album "Great Truckin' Songs of the Renaissance "
delay(500);
display.begin();
display.clearDisplay();
display.setFont(&FreeSansBold12pt7b);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 30); //set the coordinates to start writing text
display.print("T.I.S.M" ); //print the characters at location x,y
display.display();
delay(500);
}
void loop()
{
display.clearDisplay();
display.setFont(&FreeSansBold12pt7b);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 30); //set the coordinates to start writing text
display.print("Select" ); //print the characters at location x,y
display.display();
if ( bouncer.update() ) {
if ( bouncer.read() == HIGH) {
if ( buttonValue == LOW ) {
buttonValue = HIGH; //starts mm()
Serial.println("high");
mm();
} else {
buttonValue = LOW; // starts tpi()
Serial.println("low");
tpi();
}
}
}
}
void mm()
{
Serial.println("void mm");
if (LastCount != Counter)
{
Serial.println(Counter);
Pitchmm = Counter / 100.00 ; //Metric threads have dec point values. i.e 1.25mm
display.clearDisplay();
display.setFont(&FreeSansBold12pt7b);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 30); //set the coordinates to start writing text
display.print(Pitchmm ); //print the characters at location x,y
display.setCursor(70, 30);
display.print(" mm");
display.display();
mm();
}
}
void tpi()
{
Serial.println("void tpi");
if (LastCount != Counter)
{
Serial.println(Counter);
Pitchtpi = Counter;
display.clearDisplay();
display.setFont(&FreeSansBold12pt7b);
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 30); //set the coordinates to start writing text
display.print(Pitchtpi,0 ); //print the characters at location x,y
display.setCursor(70, 30);
display.print(" tpi");
display.display();
tpi();
}
}