I have a problem with my sketch and can't find the error.
I am using an Arduino Uno Wifi Rev2. Firmware etc. are all up to date.
In my sketch I make a motor keep turning after certain conditions. However, when I press a button, I want the motor to move back to position 0, but after a predefined waiting time, it moves back to the previous state.
I have tried this with the timer0_millis function. Unfortunately, I get an error when I compliate it and I can't find the error. Can you help?
/var/folders/6l/xjdrdhjs54x4c1_yxd7ltlz00000gn/T//ccwUxPPH.ltrans0.ltrans.o: In function `loop':
moni/v1_280223.ino:114: undefined reference to `timer0_millis'
moni/v1_280223.ino:114: undefined reference to `timer0_millis'
moni/v1_280223.ino:114: undefined reference to `timer0_millis'
moni/v1_280223.ino:114: undefined reference to `timer0_millis'
moni/v1_280223.ino:117: undefined reference to `timer0_millis'
/var/folders/6l/xjdrdhjs54x4c1_yxd7ltlz00000gn/T//ccwUxPPH.ltrans0.ltrans.o:/moni/v1_280223.ino:117: more undefined references to `timer0_millis' follow
collect2: error: ld returned 1 exit status
exit status 1
Compilation error: exit status 1
Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE
After 1 minute, move to -7000
After 5 hours, move to -3200
After 6 hours, move to 0
After 7 hours, move to 3200
After 8 hours, move to 6400
After 11 hours, move to 0
If the button is pressed, move to 0 and do nothing for 2 hours.
Is that what you want to do? If so, you don't need "timer0_millis", whatever that is.
indeed you should confirm or correct what @johnwasser was asking
is this a correct description of what you want the code to do?
confirm or correct.
If it is not correct post a corrected description in the style like johnwasser has written it.
the character of how johnwasser described it is
avoiding all programming terms
just use
time and step-positions
You are trying to reset function millis() to zero.
This works for an arduino uno just fine.
The
this board uses the ATmega4809 and a different core.
Core here means the basic code that "translates" the C++-functions like pinMode(), digitalWrite() etc. to adress the real hardware of the ATmega4809.
The people who have written this core seem to have decided to not use the core-variable timer0_millis anymore.
You should'nt mess around with core-variables anyway. If you manipulate core-variables you are in the danger of screwing up things.
Your code does some timing things which can be quite easy done without resetting the function millis().
You seem to not yet fully understand how function millis() works and how to use it.
I have written a tutorial that shall explain timing how you are using it.
This tutorial is dedicated to beginners and shall be explicitly easy to understand.
To improve the tutorial questions are always welcome.
Hello and thank you for your answers.
They have already helped a little.
@Johnwasser?
I wanted to implement a second counter with the timer0_millis function. The function should contain that when the button is pressed, the motor is moved to position 0, pauses there for 2 hours and then moves to the position it should have.
Example:
7 hours after starting, the motor goes to position 3200. When I then press the button, the motor sets itself to position 0. It waits there for two hours. After the two hours, however, it should move to the position it should have after 9 hours. In this case, the 7 hours before + the 2 hours waiting time.
In the example, it would then be at position 6400 (after 8 hours).
Do you understand what I mean?
You want to be able to press the button at any time ???
If the button has been pressed:
move motor to position 0
stay at position 0 for 2 hours
after 2 hours have passed by move motor to the same position that the motor would have if the button would had not been pressed
This means you have certain time-windows and there is a reference-point in time.
The windows are
right from START until 1 minute
or to say it different
between START and 1 minute past START
between 1 minute and 5 hours past START
between 5 hours and 6 hours past START
between 6 hours and 7 hours past START
between 7 hours and 8 hours past START
between 8 hours and 11 hours past START
whenever the button is pressed move to position zero
then wait for two hours relative to the time when the button was pressed
after 2 hours have passed by move motor to position according to the description above
is this a correct description of the wanted behaviour?
here is a code-version that should do what the description above is.
This code keeps the repetetive character of your original code.
The code can be much shorter by using arrays. Then you would store the times and the position into these arrays and then just iterate through the elements of the array
This code is prepared to let run the microcontroller all the time
if the 11 hours have passed by no new position will be set
But you could add code that pressing a different button that your "go to pos 0"-button
would start a new cycle without the need to powercycle the microcontroller
the fact that timer millis() is counting up more and more is handled correct by the code
even if millis() has a rollover.
This is done by calculating time-differencies with unsigned integers.
I recommend that you define constants for your positions.
Then you have only one place to change the numbers of the positions
#include <AccelStepper.h>
const long OneMinuteIn_ms = 60000;
#define dirPin 2
#define stepPin 4
#define motorInterfaceType 1
#define PIN_BUTTON 7
unsigned long oneMinute_ms = 60 * 1000UL;
unsigned long twoHours_ms = oneMinute_ms * 60 * 2;
unsigned long fiveHours_ms = oneMinute_ms * 60 * 5;
unsigned long sixHours_ms = oneMinute_ms * 60 * 6;
unsigned long sevenHours_ms = oneMinute_ms * 60 * 7;
unsigned long eightHours_ms = oneMinute_ms * 60 * 8;
unsigned long elevenHours_ms = oneMinute_ms * 60 * 11;
unsigned long StartOfCycle;
unsigned long timeSinceStart;
unsigned long StartOfWaitAtPos0;
boolean waitAtPos0_active = false;
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
stepper.setMaxSpeed(600);
stepper.setAcceleration(400);
pinMode(PIN_BUTTON, INPUT_PULLUP);
Serial.begin(9600);
delay (1000);
StartOfCycle = millis();
}
void loop() {
// check if button is pressed
if (digitalRead(PIN_BUTTON) == HIGH) {
// if flag-varaiable is not yet set
if (!waitAtPos0_active) {
waitAtPos0_active = true; // set flag-variable
// store snap-shot of time right in the moment the button is pressed
StartOfWaitAtPos0 = millis();
stepper.moveTo(0);
stepper.runToPosition();
}
}
// check if two hours have passed by since button has been pressed
if (millis() - StartOfWaitAtPos0 >= twoHours_ms) {
waitAtPos0_active = false;
}
// if waiting at position 0 is NOT active
if (!waitAtPos0_active) {
managePositions();
}
}
void managePositions() {
// calculate how much time has passed by since cylce started
timeSinceStart = millis() - StartOfCycle;
// according to the time-slot
if (timeSinceStart > 0 && timeSinceStart <= oneMinute_ms) {
// if target-position is not yet reached
if (stepper.currentPosition() != -7000) {
stepper.moveTo(-7000); // set position
stepper.runToPosition();
}
}
if (timeSinceStart > oneMinute_ms && timeSinceStart <= fiveHours_ms) {
if (stepper.currentPosition() != -3200) {
stepper.moveTo(-3200);
stepper.runToPosition();
}
}
if (timeSinceStart > fiveHours_ms && timeSinceStart <= sixHours_ms) {
if (stepper.currentPosition() != 0) {
stepper.moveTo(0);
stepper.runToPosition();
}
}
if (timeSinceStart > sixHours_ms && timeSinceStart <= sevenHours_ms) {
if (stepper.currentPosition() != 3200) {
stepper.moveTo(3200);
stepper.runToPosition();
}
}
if (timeSinceStart > sevenHours_ms && timeSinceStart <= eightHours_ms) {
if (stepper.currentPosition() != 6400) {
stepper.moveTo(6400);
stepper.runToPosition();
}
}
if (timeSinceStart > eightHours_ms && timeSinceStart <= elevenHours_ms) {
if (stepper.currentPosition() != 0) {
stepper.moveTo(0);
stepper.runToPosition();
}
}
}
thanks for the code. An interesting and yet simple solution. I will try it out in live operation later. It sounds logical to me at first. But I will take a closer look to understand the code.
Defining the positions with a constant makes sense and I will implement it directly. Thanks for the tip.
The arrays are a good idea. I would also like to implement it, as it makes things much easier. However, I would still need a lot of support here to learn.
I bought my first Arduino a week ago and have only just started with programming.
If you still have time and want to, maybe you can help me with the arrays here. As a learning example, which I can use to continue learning.
Thanks a lo
Thank you and everyone else for the support so far!
It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.
arrays are explained in paragraph 17.
From the fact that arrays are in §17 you can conclude that arrays are a bit advanced.
If you at least have understood what variables are you can jump ahead trying to understand arrays
Thank you for the link. I'll take my time and work through it. But I need time for that. The first pages were already well structured.
But I still have one question about the code. Maybe you can answer it for me.
If I wanted to add a condition that the "waitAtPos0" is always reset when it is triggered, how could I incorporate that into the code?
Example: The switch was confirmed and the motor turns to position 0, where it waits for two hours. After one hour, the button is pressed again. Then the waiting time should start again at zero.
How could I incorporate this into the if loop? Or do I need an extra counter for this?
// check if button is pressed
1. if (digitalRead(PIN_BUTTON) == HIGH) {
// if flag-varaiable is not yet set
2. if (!waitAtPos0_active) {
3. waitAtPos0_active = true; // set flag-variable
// store snap-shot of time right in the moment the button is pressed
4. StartOfWaitAtPos0 = millis();
5. stepper.moveTo(0);
6. stepper.runToPosition();
}
}
explain in your own words what the lines numbered with
1.
2.
3.
4.
5.
6.
are doing.
The important thing is: find your own words.
What do you mean with "your own words"?
Sounds like a learning task at school
If the button is pressed or not set to "high"...
Query whether "waitAtPos0" is already active (defined as false at the beginning)
Set "waitAtPos0" to true
Start the counter in milliseconds
Set position to 0
Let the motor go to position of point 5
My thoughts on this.
Since I have stored the if query in the loop, it is always executed when the button is confirmed. This also means that the timer is always reset and starts again at 0.
almost.
There is a tiny tiny but very important detail that reverses the logic:
This tiny tiny detail is the not-operator.
Huh where does the not-operator hide?
The not-operator is the attention-mark
!
the not-operator simply inverts the logic value
not false = true
not true = false
in programming the not-operator is the attention-mark ! false = true ! true = false
So take a very very close look again at the line of code
if (!waitAtPos0_active) {
there is an attention-mark ! before the variable
if (!waitAtPos0_active) {
this means initially / normally the boolean variable waitAtPos0_active has value false
this means the if-condition is
if (!false) {
which in result means
if (true) {
// do what is inside the curly braces of the if-condition
}
what is done inside the if-condition is to set variable waitAtPos0_active to true
so the next time the if-condition is executed means
if (!true) {
which is
if (false) {
the if-condition is not executed because !waitAtPos0_active evaluates to false
This means the if-condition
if (!Variable) {
Variable = true;
is some kind of a self-locking mechanism
that has the effect of
a new buttonpress does not reset the wait_at_position_0_timer.
if you remove this if-condition the wait_at_position_0_timer is reset to zero
For testing you can do two things:
reduce the times from hours to seconds
add serial printing that makes visible what line of code is already executed
I might consider this as "reset the timer" or "update the waiting time start", since the millis() counter part of the timer is always running, and how use use millis() is to compare its count to a starting time.
Then from:
... it might be clear that to "update the waiting time start" with each button press, you need to move the line from the "Query whether "2. waitAtPos0" is already active..." conditional and up to the "button is pressed again" conditional.