I'm not at all familiar with the printf type of stuff. I saw it in the code @groundFungus posted that would put the text on LCD up in setup for the text that wasn't changed. Sounded like a good idea, and it works for me.
The code in the updating LCD portion that should update only if the rpm has changed still seems to be updating at the 500 milli pace. I was hoping with the LCD updating only when rpm changed the motor would run smooth except when rpm changed. As you can see both rpm and ips are calculated from the analog_value pin.
#include <Wire.h>
#include <AccelStepper.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
// Define the stepper and the pins it will use
AccelStepper stepper1(AccelStepper::DRIVER, 8, 9); //8=PUL, 9=DIR "AccelStepper::DRIVER" cam be changed to "1" ok
// Define our three input button pins
#define LEFT_PIN 5
#define STOP_PIN 4
#define RIGHT_PIN 3
#define ENA 7 //So the stepper will turn freely when STOP is pressed.
// Define our analog pot input pin
#define SPEED_PIN A0
// Define our maximum and minimum speed in steps per second (scale pot to these)
#define MAX_SPEED 5000 //this was set to 500, but I changed to higher number, might set higher later
#define MIN_SPEED 0.0 //This was set to 0.1, but I changed to 0 so rotation would stop at min setting
float current_speed = 0.0; // Holds current motor speed in steps/second
unsigned int analog_read_counter = 1000; // Counts down to 0 to fire analog read
char sign = 0; // Holds -1, 1 or 0 to turn the motor on/off and control direction
unsigned int analog_value = 0; // Holds raw analog value.
unsigned int rpm = 0;
float ips = 0.0;
unsigned int timer = 0;
unsigned int RPM = 0;
float IPS = 0.0;
unsigned int lastRPM = 0;
float lastIPS = 0;
void setup()
{
// The only AccelStepper value we have to set here is the max speeed, which is higher than we'll ever go
stepper1.setMaxSpeed(10000.0);
// initialize and home the LCD display
// lcd.begin(LCD_COLS, LCD_ROWS);
// Set up the three button inputs, with pullups
pinMode(LEFT_PIN, INPUT_PULLUP);
pinMode(STOP_PIN, INPUT_PULLUP);
pinMode(RIGHT_PIN, INPUT_PULLUP);
pinMode (ENA, OUTPUT);
Serial.begin(115200);
lcd.begin(LCD_COLS, LCD_ROWS);
// print permanent labels once, only
lcd.setCursor(2, 0);
lcd.print("RPM =");
lcd.setCursor(2, 1);
lcd.print("IPS =");
}
void loop()
{
// If a switch is pushed down (low), set the sign value appropriately
if (digitalRead(LEFT_PIN) == 0)
{
sign = 1; digitalWrite(ENA, HIGH);
}
else if (digitalRead(RIGHT_PIN) == 0)
{
sign = -1; digitalWrite(ENA, HIGH);
}
else if (digitalRead(STOP_PIN) == 0)
{
sign = 0; digitalWrite(ENA, LOW);
}
stepper1.runSpeed(); // This will run the stepper at a constant speed
// We only want to read the pot every so often (because it takes a long time we don't
// want to do it every time through the main loop).
if (analog_read_counter > 0)
{
analog_read_counter--;
}
else
{
analog_read_counter = 3000; //originally at 3000
// Now read the pot (from 0 to 1023)
analog_value = analogRead(SPEED_PIN);
// And scale the pot's value from min to max speeds
current_speed = sign * (((analog_value / 1023.0) * (MAX_SPEED - MIN_SPEED)) + MIN_SPEED);
}
// Update the stepper to run at this new speed
stepper1.setSpeed(current_speed);
rpm = (analog_value / 5.5297);
ips = (analog_value / 56.8333);
/*{
// generate test numbers with pots
RPM = analogRead(SPEED_PIN);
IPS = 800 - analogRead(pot2Pin) * 30.5;
updateLCD();
}*/
updateDisplay();
}
void updateDisplay()
{
/* static unsigned long timer = 0;
unsigned long interval = 500; // update display 2 times / second
if (millis() - timer >= interval)
{
timer = millis();*/
static unsigned long lcdTimer = 0;
unsigned long lcdInterval = 500; // update 2 times per second
if (millis() - lcdTimer >= lcdInterval)
{
lcdTimer = millis();
if(rpm != lastRPM) // only print to LCD if rpm changed
lcd.setCursor(6, 0);
lcd.print(" "); // overwrite old data
lcd.setCursor(8, 0); // reset cursor
lcd.print(rpm);
lcd.setCursor(6, 1);
lcd.print(" "); // overwrite old data
lcd.setCursor(8, 1); // reset cursor
lcd.print(ips, 1);
}
}