Hi all,
I'm looking for some advice how to interface with a linear actuator,
Thanks for any help/guidance.
Required inputs are:
Travel distance in mm
Travel speed in mm/min
Move Up
Move Down
The code to control the stepper motor works, reading the analog input, and reading the state of the buttons work in individual scripts. I'm stuck on how to put it all together and read the buttons when they are pressed while the program takes care of other things.
I am using a Arduino UNO to control a stepper driver to motorize the bed of my drill press.
I have (1) 10K pot and (3) buttons to use as user inputs.
LCD 16x2 is used to display travel distance (mm) on row1 and travel speed (mm/min) on row 2.
I need 2 buttons for the UP/DOWN controls, and that only leaves me with the POT and one button.
I think this should be done using a interrupt routine, but I have never used one.
My thought was to use the single button to cycle through the three possible states
Update Travel value
Update Speed value
Enter/Exit input mode
the logic would look like this:
Initial button click
Highlight LCD Row-1
Update Row-1 with Pot value
Second button click
Keep current vale on row-1
Save current value in variable1
Remove highlight from Row-1
Highlight Row-2
Update Row-2 with Pot value
Third button click
Keep current value on Row2
Save current value in variable2
Remove highlight from Row-2
Return control to loop()
This is the code I have so far, but I don't know where to put the interrupt routine and can the interrupt routine update the global variable "SelectButtonIndex" so it can be used in the main body code?
/*
Based on https://dronebotworkshop.com/big-stepper-motors/
Status:
7/24/2024 > Test LCD display, completed
7/24/2014 > Read Potentiometer on pin A0, completed.
7/25/2024 > Display pot value on LCD, completed.
7/26/2024 > Display value of Select button in serial monitor, completed
TODO: Use select button to switch between 3 states (update row-1, update row-2, exit)
Pin Notes:
Potentiometer wiper pin 2 to analog A0
Potentiometer pin 3 to 5V
Potentiometer pin 1 to Gnd
Button "Select" to digital pin 4
Button "Up" to digital pin 8
Button "Down" to digital pin 9
Stepper driver Pul- on digital pin 7
Stepper driver Dir- on digital pin 6
LCD Display output
Row1:Colmun1 > active field symbol ">", Stored value symbol "#"
Row1:Colmun3 > Tavel distance value + "mm"
Row2:Colmun1 > active field symbol ">", Stored value symbol "#"
Row2:Colmun3 > Travel speed value + "mm/min"
*/
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneButton.h>
// Constant's
const int SelectPin = 4;
// Global variables
int PotValue = 0;
int SelectButtonState = 0;
int SelectButtonIndex = 0;
// Identify I2C devices
LiquidCrystal_I2C lcd(0x27,16,2);
?
? Interrupt
? cycle the SelectButtonIndex variable [0,1,2] when SelectButtonState is triggered
?
void setup() {
// Configure LCD
lcd.init();
lcd.backlight();
lcd.clear();
lcd.home();
// Initialize serial port
Serial.begin(9600);
// configure push button's
pinMode(SelectPin, INPUT);
}
void loop() {
// Get potentiomer raw value (0-1024)
PotValue = analogRead(A0);
// display values for testing
Serial.println(PotValue);
if (SelectButtonIndex = 1) {
// ? highlight row-1 ?
TravelDistance = PotValue;
lcd.setCursor(3,0);
lcd.print(TravelDistance);
Serial.print("Distance: ");
Serial.println(TravelDistance);
// ? remove highlight row-1 ?
}
if (SelectButtonIndex = 2) {
// ? highlight row-2 ?
TravelSpeed = PotValue;
lcd.setCursor(3,0);
lcd.print(TravelSpeed);
Serial.print("Speed: ");
Serial.println(TravelDistance);
// ? remove highlight row-2 ?
}
delay(500);
}