Ah ok ,so if i increment my storage by 2 each time that will fix the storage of the values greater than 255?
how do read back the values? currently i suppose I'm just reading back the one byte , do i need to read back for each int 2 byte locations and recombine them?
cheers i really appreciate all the support and help provided.
[Using rotary encoder and serial monitor - #34 by J-M-L]
Experiment.
thanks everyone think I've got somewhere and have a working save / recover now i have one other thing i didn't realize was an issue but is,
when i put the rotary encoder into operation it always returns the stepper motor to position 0 before then moving the position turned by the encoder, is there a way to get the encoder to move in relation to where the stepper motor is not 0? it looks like the rotary encoder is always startign from 0 not from the current position of the stepper motor,
thanks
int Position0 = 0; //variable for Pos 0
int Position1 = 0; //variable for Pos 1
int Position2 = 0; //variable for Pos 2
int Position3 = 0; //variable for Pos 3
int Position4 = 0; //variable for Pos 4
int position5 = 0; //variable for Pos 5
int position6 = 0; //variable for Pos 6
int position7 = 0; //variable for Pos 7
int position8 = 0; //variable for Pos 8
int position9 = 0; //variable for Pos 9
This and the other numbered variables is just crying out for you to use arrays
Use arrays and store the whole array at once with a single call to EEPROM.put()
Hi can i ask for some further advice please, if i move my stepper motor to say position 800 (half way rotation) and then engage the rotary controller, as soon as i move the rotary controller it moves the stepper back to position 0 before then moving to the position of the encoder, is it possible to get the encoder to start its movement from the current position of the stepper motor (in this case 800) and not from 0 all the time?
cheer.
Hello please could I ask for some help with getting the decoder to start from where the stepper motor is currently instead of 0 please? every time I engage the rotary encoder it takes the stepper motor back to 0 before moving to where I have turned it, I need it to start from where the motor is currently.
thanks for any help.
cheers
Post your latest code
Hi here is my current code, I've switched to a different stepper driver - DRV899 which is listed to run with 1.5A max 2Amp and my motor is 1.7A so that works out at a vref of 0.75 and whilst its a little better I'm still getting missed steps randomly, and the heatsink on the driver is very hot when running and the motor is quite noisy, if I turn the vref voltage down to say 25 it quietens it down and doesn't get so hot but then misses more steps. ???
but if we can fix the rotary starting position that will be one step closer.
#include <Encoder.h> // https://www.pjrc.com/teensy/td_libs_Encoder.html
#include <easyRun.h> // https://github.com/bricoleau/easyRun
#include <AccelStepper.h>
#include <Keypad.h>
#include <EEPROM.h>
void writeIntIntoEEPROM(int address, int number)
{
EEPROM.write(address, number >> 8);
EEPROM.write(address + 1, number & 0xFF);
}
int readIntFromEEPROM(int address)
{
return (EEPROM.read(address) << 8) + EEPROM.read(address + 1);
}
//setup keybaord map
const byte ROWS = 4;
const byte COLS = 3;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {11, 10, 9, 8};
byte colPins[COLS] = {7, 6, 5};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
volatile boolean keypress = 1;
const byte encoderDTPin = 2; // encoder encoder DT
const byte encoderCLKPin = 3; // encoder encoder CLK
Encoder encoder(encoderDTPin, encoderCLKPin);
const byte encoderSWPin = 4; // encoder encoder SW
button encoderSwitch(encoderSWPin);
// A4988 Driver connections
#define motorInterfaceType 1 //DRV8825 stepper driver
#define dirPin 16
#define stepPin 17
#define MS1 A4 // Pin 10 connected to MS1 pin
#define MS2 A5 // Pin 11 connected to MS2 pin
// Hall Sensor connections
#define HALL_SENSOR 15
#define LED 12
#define ENABLE 14
int Currentpos = 0; //variable for current position value when saving to eeprom
int Position0 = 0; //variable for Pos 0
int Position1 = 0; //variable for Pos 1
int Position2 = 0; //variable for Pos 2
int Position3 = 0; //variable for Pos 3
int Position4 = 0; //variable for Pos 4
int position5 = 0; //variable for Pos 5
int position6 = 0; //variable for Pos 6
int position7 = 0; //variable for Pos 7
int position8 = 0; //variable for Pos 8
int position9 = 0; //variable for Pos 9
int a1 = 1;
int value1; //variable for Pos 1 to 9
int a2 = 3;
int value2; //variable for Pos 1 to 9
int a3 = 5;
int value3; //variable for Pos 1 to 9
int a4 = 7;
int value4; //variable for Pos 1 to 9
int a5 = 9;
int value5; //variable for Pos 1 to 9
int a6 = 11;
int value6; //variable for Pos 1 to 9
int a7 = 13;
int value7; //variable for Pos 1 to 9
int a8 = 15;
int value8; //variable for Pos 1 to 9
int a9 = 17;
int value9; //variable for Pos 1 to 9
// Stepper Home & Travel Variables
long TravelX; // Used to store the X value entered in the Serial Monitor
int move_finished = 1; // Used to check if move is completed
long initial_homing = -1; // Used to Home Stepper at startup
// Create a new instance of the AccelStepper class:
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
long currentPosition = 0;
volatile boolean rswitch = 1; // Variable to store rotary encoder switch state
volatile boolean rswitch2 = 1; // Variable to store program # switch state
bool encoderChanged() {
long newPosition = encoder.read() >> 2;// my encoder generates 2 ticks per click so I divide by 2
if (newPosition != currentPosition) {
currentPosition = newPosition;
Serial.println(currentPosition);
return true;
}
return false;
}
bool switchPressed() {
easyRun();
if (encoderSwitch) {
Serial.println("click");
return true;
}
return false;
delay(1000);
}
void setup() {
Serial.begin(115200); Serial.println();
pinMode(HALL_SENSOR, INPUT);
delay(5);
encoder.write(currentPosition << 2); // my encoder generates 2 ticks per click so I multiply by 2
Serial.println(F("Ready"));
// Set the maximum speed and acceleration:
stepper.setMaxSpeed(60);
stepper.setAcceleration(100);
//initialise pins as inputs
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(ENABLE, OUTPUT);
digitalWrite(ENABLE, LOW);
digitalWrite(MS1, HIGH);
digitalWrite(MS2, HIGH);
pinMode(LED, OUTPUT);
// Read value of EEprom and assign to button variables
value1 = readIntFromEEPROM(a1);
value2 = readIntFromEEPROM(a2);
value3 = readIntFromEEPROM(a3);
value4 = readIntFromEEPROM(a4);
value5 = readIntFromEEPROM(a5);
value6 = readIntFromEEPROM(a6);
value7 = readIntFromEEPROM(a7);
value8 = readIntFromEEPROM(a8);
value9 = readIntFromEEPROM(a9);
// Start Homing procedure of Stepper Motor at startup
Serial.print("Stepper is Homing . . . . . . . . . . . ");
while (digitalRead(HALL_SENSOR)) { // Make the Stepper move CCW until the switch is activated
stepper.moveTo(initial_homing); // Set the position to move to
initial_homing--; // Decrease by 1 for next move if needed
stepper.run(); // Start moving the stepper
delay(5);
}
stepper.setCurrentPosition(0); // Set the current position as zero for now
stepper.setMaxSpeed(60.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(100.0); // Set Acceleration of Stepper
initial_homing = 1;
while (!digitalRead(HALL_SENSOR)) { // Make the Stepper move CW until the switch is deactivated
stepper.moveTo(initial_homing);
stepper.run();
initial_homing++;
delay(5);
}
stepper.setCurrentPosition(0);
Serial.println("Homing Completed");
Serial.println("");
Serial.print (stepper.currentPosition());
Serial.println ();
//stepper.setCurrentPosition(0); // Set the current position as currentPosition
Serial.println ();
delay (2000);
digitalWrite(ENABLE, HIGH); //disables motor driver startup homing completed
}
void loop() {
{ char customKey = customKeypad.getKey();
if (switchPressed()) { // Do if Rotary Encoder switch is pressed
delay(250); // debounce switch
if (rswitch == 0) {
rswitch = 1;
digitalWrite(LED, LOW);
}
else {
rswitch = 0;
digitalWrite(LED, HIGH);
Serial.println(F("System is in free move mode"));
}
}
if (customKey == '#') { // do if # key is pressed
if (rswitch2 == 0) {
rswitch2 = 1;
digitalWrite(ENABLE, HIGH);
digitalWrite(LED, LOW);
Serial.println(F("Programming Complete..."));
}
else {
rswitch2 = 0;
digitalWrite(ENABLE, LOW);
digitalWrite(LED, HIGH);
Serial.println(F("Ready to Program......."));
}
}
if (rswitch2 == 0 && rswitch == 0) { //this is the state if both swithes are preseed
Currentpos = stepper.currentPosition(); // inserted to view end position of stepper motor
Serial.print (Currentpos);
Serial.println ();
if (customKey == '3') {
digitalWrite(LED, HIGH);
Serial.println(customKey);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
writeIntIntoEEPROM(a3, (Currentpos)); //used to store the position to button 3 to memory 3.
Reset() ;
}
if (customKey == '1') {
digitalWrite(LED, HIGH);
Serial.println(customKey);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
writeIntIntoEEPROM(a1, (Currentpos)); //used to store the position to button 3 to memory 3.
Reset() ;
}
}
else if (rswitch2 == 0 && rswitch == 1)
{
digitalWrite(ENABLE, LOW); //enables motor driver
digitalWrite(MS1, HIGH);
digitalWrite(MS2, HIGH);
stepper.setMaxSpeed(60.0); // Set Max Speed of Stepper (Slower to get better accuracy)
stepper.setAcceleration(100.0); // Set Acceleration of Stepper
stepper.runSpeedToPosition();
if (encoderChanged()) {
stepper.moveTo(currentPosition);
Serial.println ();
//delay (500);
digitalWrite(ENABLE,HIGH); //disables motor driver during down time
}
}
// char customKey = customKeypad.getKey(); moved further up to allow other options
if (customKey == '1') {
digitalWrite(LED, HIGH);
Serial.println(customKey);
delay (100);
digitalWrite(LED, LOW);
delay (100);
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
delay (100);
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
delay (100);
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
delay (100);
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
delay (100);
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
delay (100);
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
delay (100);
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
movepos1();
}
else if (customKey == '3') {
digitalWrite(LED, HIGH);
Serial.println(customKey);
delay (100);
digitalWrite(LED, LOW);
delay (100);
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
delay (100);
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
delay (100);
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
delay (100);
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
delay (100);
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
delay (100);
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
delay (100);
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
movepos3();
}
else
if (customKey == '*') {
digitalWrite(LED, HIGH);
Serial.println(customKey);
delay (80);
digitalWrite(LED, LOW);
delay (80);
digitalWrite(LED, HIGH);
delay (80);
digitalWrite(LED, LOW);
delay (80);
digitalWrite(LED, HIGH);
delay (80);
digitalWrite(LED, LOW);
delay (80);
digitalWrite(LED, HIGH);
delay (80);
digitalWrite(LED, LOW);
delay (80);
digitalWrite(LED, HIGH);
delay (80);
digitalWrite(LED, LOW);
delay (80);
digitalWrite(LED, HIGH);
delay (80);
digitalWrite(LED, LOW);
delay (80);
digitalWrite(LED, HIGH);
delay (80);
digitalWrite(LED, LOW);
delay (80);
digitalWrite(LED, HIGH);
delay (80);
digitalWrite(LED, LOW);
halfSwing();
}
else
if (customKey == '0') {
digitalWrite(LED, HIGH);
Serial.println(customKey);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
delay (20);
digitalWrite(LED, HIGH);
delay (20);
digitalWrite(LED, LOW);
returnHome();
}
}
}
void halfSwing()
{
{
// Set the maximum speed and acceleration:
digitalWrite(ENABLE, LOW); //enables stepper driver
stepper.setMaxSpeed(60);
stepper.setAcceleration(100);
stepper.moveTo(stepper.currentPosition() + 800); //180 Spin
stepper.runToPosition();
delay (1000);
digitalWrite(ENABLE,HIGH); //disables stepper driver
}
Position1 = stepper.currentPosition(); // inserted to view end position of stepper motor
Serial.print (Position1);
Serial.println ();
{
// EEPROM.write(0, Position1/8); //currently not used but will be used to store the position of the stepper motor to a memory location.
}
}
void returnHome()
{
{
// Set the maximum speed and acceleration:
digitalWrite(ENABLE, LOW); //enables stepper driver
stepper.setMaxSpeed(60);
stepper.setAcceleration(100);
stepper.moveTo(0);
stepper.runToPosition();
//stepper.setCurrentPosition(0);
delay (1000);
digitalWrite(ENABLE, HIGH); //disables stepper driver
}
Position0 = stepper.currentPosition(); // inserted to view end position of stepper motor
Serial.print (Position0);
Serial.println ();
{
// EEPROM.write(0, Position1/8); //currently not used but will be used to store the position of the stepper motor to a memory location.
}
}
void movepos1()
{
{
digitalWrite(ENABLE,LOW); //enables stepper driver
// Set the maximum speed and acceleration:
stepper.setMaxSpeed(60);
stepper.setAcceleration(100);
if (stepper.currentPosition() != 0)
{
stepper.moveTo(value1); //moves to eeprom value for position 3
stepper.runToPosition();
}
else if (stepper.currentPosition() == 0)
{
stepper.moveTo(value1); //moves to eeprom value for position 3+2
stepper.runToPosition();
}
//Position2 = stepper.currentPosition(); // inserted to view end position of stepper motor
Serial.print("Position1: ");
Serial.print (stepper.currentPosition());
Serial.println ();
delay (1000);
digitalWrite(ENABLE,HIGH); //disables stepper driver
}
}
void movepos3()
{
{
digitalWrite(ENABLE,LOW); //enables stepper driver
// Set the maximum speed and acceleration:
stepper.setMaxSpeed(60);
stepper.setAcceleration(100);
if (stepper.currentPosition() != 0)
{
stepper.moveTo(value3); //moves to eeprom value for position 3
stepper.runToPosition();
}
else if (stepper.currentPosition() == 0)
{
stepper.moveTo(value3); //moves to eeprom value for position 3+2
stepper.runToPosition();
}
//Position2 = stepper.currentPosition(); // inserted to view end position of stepper motor
Serial.print("Position3: ");
Serial.print (stepper.currentPosition());
Serial.println ();
delay (1000);
digitalWrite(ENABLE,HIGH); //disables stepper driver
}
}
void(* resetFunc) (void) = 0;
void Reset()
{
Serial.println("A");
delay(1000);
Serial.println("B");
delay(1000);
Serial.println("Now we are Resetting Arduino Programmatically");
Serial.println();
delay(1000);
resetFunc();
Serial.println("Arrduino will never reach there.");
}
to set the value of the encoder's value, you do
encoder.write(currentPosition << 2); // my encoder generates 2 ticks per click so I multiply by 2
I do that in the setup() for example.
You need to know the current stepper position of course for this to work.
Hmm thought that was what was in the code originally, is it possible I'm confusing the position with my location of the currentPosition variable? is stepper.currentPosition different to currentPosition? cheers.
I’m reading this on my iPhone, so hard to read all the code
if you don’t maintain the currentPosition variable up to date then they might be different
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.