You will get more and faster answers if you follow the world-wide common indention-habits that were created by pressing ctrl-T. If your code gets indented more than two lines this can be adapted.
Anyway you should post your complete code from the very first line to the very last line.
It might be that you are missing a parenthesis or a curly brace
if other shall check this for you it is must to post your complete code
See. With the blocks lined up it was quick and easy for me to see the problem. The other way I would never have noticed it because it wasn't clear what part was nested inside what part without me having to count braces by hand.
This part of the code is inside the runSpeed10 bit. It's inside an if statement that only runs if (value3 != rotation3)
But that's where the values of rotation1, and rotation2 get updated. So if rotation3 is equal to value3, rotation1 and rotation2 never get updated and those sections continue to run over and over.
You should move each of those lines into the section they belong in so that rotation1 is updated in the section that checks against rotation1 and rotation2 gets updated in the section that checks against rotation2.
Also, question: are your encoders operating consistently and correctly? The code doesn't work well with the encoders I have available. Just say yes and I'll be quiet.
Yes alto777, the encoders are operating consistently.
Yes the position of the brace closing the Encoder#3 section was in the wrong place.
} // move here
runSpeed = runSpeed1000 + runSpeed100 + runSpeed10;
// move here Not here
myStepper.setSpeed(runSpeed);
myStepper.runSpeed();
Serial.print(runSpeed);
// move this brace up to end if
}
I moved the brace from line 166 to line 160.
This almost works. I am seeing the runSpeed but again in a endless loop.
2000
You should make a fundamental decision between
way A
and
way B
way A is:
staying on a pretty low level of understanding how writing C++-Code works and then depending on the grace of other users to modify your code again and again and again
way B:
taking some hours time to really learn the fundamentals and then beeing able to at least understand and modify your code yourself if something like
Life will become much easier if you use a library for the encoders and a different library for the stepper-motor
Here is a code-version that uses a encoder-library which uses a state-table. This has the advantage of automatic debouncing and really reliable up/down-counting
I have put the code that is polling the three encoder-states into a timer-interrupt.
This code-version uses the mobaTools for the stepper-motor.
This has the advantage that the step-pulses are created in the background which enables to execute code in parallel to the step-pulse-creation.
The code compiles but I don't have your hardware on the table. This means there can still be bugs in this code-version
/* Demo-Code that uses the Rotary-library from GitHub-User https://github.com/buxtronix
* using his library https://github.com/buxtronix/arduino/tree/master/libraries/Rotary
* in combination with a timer-interrupt executed 10000 times per second
* Copyright 2023 StefanL38. Licenced under the GNU GPL Version 3.
* A T T E N T I O N !
* this demo-code uses Timer2 which is used by other libraries too.
* This means using this code can interfere with other libraries
* causing malfunction of both
*
* As timer-interrupts are very hardware-sepcific
* this demo-code works with Arduino-Uno R3 based on the chip AtMega328P
* if you want to use it on other microcontrollers you will have to
* modify setting up the timer-interrupt according to the hardware
* of this other microcontroller
*
* The demo-code simply prints the value of variable myCounter
* each time the value of the variable changes
*/
#include <Rotary.h>
#include <MobaTools.h>
const byte dirPin = 4;
const byte stepPin = 5;
const int stepsPerRev = 200;
MoToStepper myStepper( stepsPerRev, STEPDIR ); // create a stepper instance
const byte CLK1 = 2;
const byte DT1 = 8;
const byte CLK2 = 3;
const byte DT2 = 11;
const byte CLK3 = 21;
const byte DT3 = 12;
Rotary rotary1 = Rotary(CLK1, DT1);
Rotary rotary2 = Rotary(CLK2, DT2);
Rotary rotary3 = Rotary(CLK3, DT3);
unsigned long myISR_TimerFrequency = 1000;
// Cnt1000, Cnt100, Cnt10 that will be incremented or decremented by rotation.
// as these variables are changed in an interrupt-service-routine
// this variable MUST !! be declared volatile to make sure
// that it works properly !
volatile unsigned char Cnt1000 = 0;
volatile unsigned char Cnt100 = 0;
volatile unsigned char Cnt10 = 0;
long runSpeed = 0;
long last_runSpeed = 0;
void setupTimerInterrupt(unsigned long ISR_call_frequency) {
long OCR2A_value;
const byte Prescaler___8 = (1 << CS21);
const byte Prescaler__32 = (1 << CS21) + (1 << CS20);
const byte Prescaler__64 = (1 << CS22);
const byte Prescaler_128 = (1 << CS22) + (1 << CS20);
const byte Prescaler_256 = (1 << CS22) + (1 << CS21);
const byte Prescaler1024 = (1 << CS22) + (1 << CS21) + (1 << CS20);
const unsigned long CPU_Clock = 16000000;
cli();//stop interrupts
TCCR2A = 0;// set entire TCCR2A register to 0
TCCR2B = 0;// same for TCCR2B
TCNT2 = 0;//initialize counter value to 0
TCCR2A |= (1 << WGM21); // turn on CTC mode
TIMSK2 |= (1 << OCIE2A); // enable timer compare interrupt
TCCR2B = Prescaler__32;
// OCR2A_value must be smaller than 256
// use a different prescaler if OCR2A_value is > 256
OCR2A_value = (CPU_Clock / ( 32 * ISR_call_frequency) ) - 1;
OCR2A = OCR2A_value; // set the value of OCR2A
sei();//allow interrupts
}
// timer-interrupt-service-function for AtMega328P
ISR(TIMER2_COMPA_vect) {
processEncoders();
}
// counts up/down with each encoder-click
void processEncoders() {
unsigned char result;
result = rotary1.process();
if (result == DIR_CW) {
Cnt1000++;
}
else if (result == DIR_CCW) {
Cnt1000--;
}
if (Cnt1000 < 0) {
Cnt1000 = 0;
}
result = rotary2.process();
if (result == DIR_CW) {
Cnt100++;
}
else if (result == DIR_CCW) {
Cnt100--;
}
if (Cnt100 < 0) {
Cnt100 = 0;
}
result = rotary3.process();
if (result == DIR_CW) {
Cnt10++;
}
else if (result == DIR_CCW) {
Cnt10--;
}
if (Cnt10 < 0) {
Cnt10 = 0;
}
}
void PrintFileNameDateTime() {
Serial.println( F("Code running comes from file ") );
Serial.println( F(__FILE__) );
Serial.print( F(" compiled ") );
Serial.print( F(__DATE__) );
Serial.print( F(" ") );
Serial.println( F(__TIME__) );
}
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long &startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}
unsigned long MyTestTimer = 0; // Timer-variables MUST be of type unsigned long
const byte OnBoard_LED = 13;
void BlinkHeartBeatLED(int IO_Pin, int BlinkPeriod) {
static unsigned long MyBlinkTimer;
pinMode(IO_Pin, OUTPUT);
if ( TimePeriodIsOver(MyBlinkTimer, BlinkPeriod) ) {
digitalWrite(IO_Pin, !digitalRead(IO_Pin) );
}
}
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
PrintFileNameDateTime();
setupTimerInterrupt(myISR_TimerFrequency);
myStepper.attach( stepPin, dirPin );
myStepper.setMaxSpeed(1000 * 10);
myStepper.setSpeed(50 * 10);
myStepper.setRampLen(0);
myStepper.rotate(1); // other rotation-direction myStepper.rotate(-1);
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED, 250);
runSpeed = Cnt1000 * 1000 + Cnt100 * 100 + Cnt10 * 10;
// check if value has changed
if (last_runSpeed != runSpeed) {
// only if value REALLY HAS changed
myStepper.setSpeed(runSpeed);
last_runSpeed = runSpeed; // update last_runSpeed
Serial.print("runSpeed=");
Serial.println(runSpeed);
}
}
Arduino IDE, cannot find Encoder.h.
I have the Encoder library installed I even copied the Encoder.h and Encoder.cpp to the skecth folder.
C:\Users\Ralph\Documents\Arduino\SpindleRun\SpindleRun-3\SpindleRun-4\SpindleRun-5\SpindleRun-6\SpindleRun-6.ino:16:10:
fatal error: Rotary.h: No such file or directory
#include <Rotary.h>
^~~~~~~~~~
compilation terminated.
exit status 1
Compilation error: Rotary.h: No such file or directory