motorencoder.ino (1,1 KB)
Please insert the code using the code tags
Why?
Because you have a null immediately after your input.
Please do not use the links for code.
Read the forum guidelines how to insert code properly.
You can analyse what is going on by printing to the serial monitor.
Below is a code-version that does print almost everything to the serial monitor
in a special way.
The code uses so called macros that
print name and value of a variable but print only in case the value has CHANGED
or
print only once every 500 milliseconds or once every 10 seconds
Despite to function delay(1000); this enables that loop() can iterate very very fast
still the serial printing is slowed down. But only the serial printing is slowed down
The function delay() should better have the name
freeze_microcontroller_completely_stop_code_execution_do_not_read_in_any_sensors_until_freezingtime_is_over(1000);
delay() is blocking. You read in your encoder only once per second because of the delay(1000);
I am pretty sure you added the delay(1000); to be able to read the values. But as delay() is blocking reading in your encoder happends only once every second.
Adjust baudrate to 115200
and then upload this code-version and watch what gets printed to the serial monitor
// MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START * MACRO-START *
// a detailed explanation how these macros work is given in this tutorial
// https://forum.arduino.cc/t/comfortable-serial-debug-output-short-to-write-fixed-text-name-and-content-of-any-variable-code-example/888298
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
#define dbgi(myFixedText, variableName,timeInterval) \
{ \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
}
#define dbgc(myFixedText, variableName) \
{ \
static long lastState; \
if ( lastState != variableName ){ \
Serial.print( F(#myFixedText " " #variableName" changed from ") ); \
Serial.print(lastState); \
Serial.print( F(" to ") ); \
Serial.println(variableName); \
lastState = variableName; \
} \
}
#define dbgcf(myFixedText, variableName) \
{ \
static float lastState; \
if ( lastState != variableName ){ \
Serial.print( F(#myFixedText " " #variableName" changed from ") ); \
Serial.print(lastState); \
Serial.print( F(" to ") ); \
Serial.println(variableName); \
lastState = variableName; \
} \
}
// MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END * MACRO-END *
unsigned long MyTestTimer = 0; // Timer-variables MUST be of type unsigned long
const byte OnBoard_LED = 2; // onboard-LEDESP32 / ESP8266
//const byte OnBoard_LED = 25; // onboard-LED Raspberry Pi pico
//const byte OnBoard_LED = 13; // onboard-LED uno, mega
/*
if ( TimePeriodIsOver(MyTestTimer,1000) ) {
// do something once every 1000 milliseconds
}
*/
//The motor should rotate at the angle I indicate
//I'm using an H L298N bridge
#include <Encoder.h>
#define ENCODER_A_PIN 2
#define ENCODER_B_PIN 3
#define ME_PIN 8
#define M1_PIN 9
#define M2_PIN 10
Encoder myEncoder (ENCODER_A_PIN, ENCODER_B_PIN);
int wantedAngle = 0;
int currentangule = 0;
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
PrintFileNameDateTime();
pinMode(ME_PIN, OUTPUT);
pinMode(M1_PIN, OUTPUT);
pinMode(M2_PIN, OUTPUT);
Serial.println("exiting setup");
}
void loop() {
BlinkHeartBeatLED(OnBoard_LED,250);
byte NrOfChars = Serial.available();
// print "NrOfChars" only in case the value has CHANGED
dbgc("00",NrOfChars);
if (NrOfChars > 0) {
wantedAngle = Serial.parseInt();
}
// print "wantedAngle" only in case the value has CHANGED
dbgc("01", wantedAngle);
int currentangule = myEncoder.read();
// print "currentangule" only in case the value has CHANGED
dbgc("02", currentangule);
int AngleDifference = wantedAngle - currentangule;
// print "AngleDifference" only in case the value has CHANGED
dbgc("03", AngleDifference);
//Serial.println(currentangule);
//Serial.println(AngleDifference);
if (AngleDifference > 0) {
// print once every 500 milliseconds
dbgi("04 diff > 0",AngleDifference,500);
digitalWrite(ME_PIN, HIGH);
digitalWrite(M1_PIN, LOW);
digitalWrite(M2_PIN, HIGH);
}
else if (AngleDifference < 0) {
// print once every 500 milliseconds
dbgi("05 diff < 0",AngleDifference,500);
digitalWrite(ME_PIN, HIGH);
digitalWrite(M1_PIN, HIGH);
digitalWrite(M2_PIN, LOW);
}
else {
// print once every 10 seconds
dbgi("06 else",AngleDifference,10000);
digitalWrite(ME_PIN, LOW);
digitalWrite(M1_PIN, LOW);
digitalWrite(M2_PIN, LOW);
//delay(1000);
}
}
// helper-functions
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
// explanation see here
// https://forum.arduino.cc/t/example-code-for-timing-based-on-millis-easier-to-understand-through-the-use-of-example-numbers-avoiding-delay/974017
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
}
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) );
}
}
code.ino (2,0 KB)
myMotor.getDirection(CLOCKWISE);`
myMotor.getDirection(COUNTERCLOCKWISE);`
These functions are written wrong.
They do not require parameters.
It returns a value.
So it should be something like this:
int myDirection = myMotor.getDirection();
more probable in that place it should be setDirection(CLOCKWISE);
The big mistake you are making is to do too much all at once. Start off with one thing and work up to adding more and more stuff.
An observation:-
The L298N is a very old and obsolete part, and is very very poor at driving motors, as it looses a lot of voltage driving the motors.
You don't seem to be using the rotary encoder in your code, what is this for?
We need to know :-
- What sort of motor you are using
- How it is wired up
- What voltage is it using.
@ariel ,
You have started 2 topics both essentially the same subject and with no meaningful question, although the code is different, and you are not interacting with the volunteers trying to help you. All these things just waste the time of the helpers. Please do not post another question on this subject and please respond to the people trying to help.
You are risking a suspension from the forum if you continues as you are.
Thank you.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.