Steppermotor with tof10120

Hello everyone,
I'm trying to controll a steppermotor with a tof10120. I know how to read the distance in mm from the tof10120 and I can controll the steppermotor speratly. Since I'm a beginner I don't know how to tell the motor to take let's just say 10 steps cw if the distance shostens by 1mm. So the motor should take 100 steps cww if the distance increases by 10mm. Can anyone help me with a code?
Thank you very much and have a nice day!

Hi @MigiHunzi ,

welcome to the arduino-forum.
IMHO this forum is meant to help. Not to deliver ready to use code.
You should post your try how you tried to write the code.

And you should post your code as a code-section.

how the code looks like depends on your hardware so please post information about

  • the exact type of microcontroller that you using
  • a link to the datasheet of your stepper-motor-driver
  • link to the datasheet of your stepper-motor
  • a link to the datasheet of the ToF-sensor
  • a link to the library that you are using for the ToF-sensor

You should re-read this question.

What do you mean by writing "shostens" ?

I assume "cw" means clockwise.
But what is cww?

If I assume you meant ccw counter-clock-wise
I still do not understand what you mean with writing

I could start speculating and ask a lot of options. But I won't do this.
You can describe with more detail and more precision what the stepper-motor shall do.

An easy way to describe something like this is describe an example with example-numbers.

best regards Stefan

Hi, @MigiHunzi
Welcome to the forum.

Please tell us what your project is, so we can help you better.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

Thank you for your reply.
I've written the code in python but I'm new to C++ so it's very difficult for me. I have converted the code with an online compiler but I don't know if it works. I would be very grateful if you could tell me if there is a mistake. The python code works that I have tested. Sorry I don't know how to format the code so sorry about that. Here is the C++ code:

#include // std::cout
#include // srand, rand
#include // time
using namespace std;
int main() {
int number_two = 0;
while (true) {
int number_one = rand() % 10; // generate a random integer from 0 to 9
cout << number_one - number_two << endl; //print numb1-numb2 to the console
sleep(3); // wait for three seconds

number_two = number_one;       // update value of `number_two` 

}

return 0;
}

And here is the python code which I know works:
import random
import time

numb2 = 0

while True:
numb1 = random.randrange(0, 10)
print(numb1 - numb2)
time.sleep(1)
numb2 = numb1

Hi, @MigiHunzi

Have you enquired on a Python forum?

What model Arduino are you using?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

Hi Michelle,

your code has absolutely nothing to do with a stepper-motor.

possible explanations:

  • accidently unintentionally pasted the wrong clipboard-content
    or
  • lazyness
    or
  • trolling

tips how to improve your posting style

best regards Stefan

No I'm sorry for the misunderstanding, I meantioned in the reply that I now have the Code for the tof10120 and the stepper motor and that part I can now do myself, but I need help with that python code which I wrote by myself. I'm not lazy I now did everything except I need to know if that code is correct in C++ because I'm a beginner at C++. To explain my code:
That random number1 between 0 and 10 simulates the the variable with the distance in mm. And with the rest I calculate the differenz of the distance now and 1 second before because the tof remeasures every 1 second so number1 always changes. With the differenz I know how much the distance increased or decreased within one second. So if the distance doesn't change I get a return of 0, when the distance increases from 20 to 21 then it returns 1 which then tells the motor to rotate let's say 1 step. If the distance decreases from 20 to 18 for example it returns -2 which then tells the motor to turn 2 steps in the opposite direction. The python code does exactly that differenz calculating so I need to know if I converted it right into C++ so then I can add the code for the motor and the code for the tof. I'm working with an Arduino Uno.
Again I'm sorry for the misunderstanding but I hope you can still help me verify that C++ code or tell me about mistakes which I did.
I really apreciate you two helping me, and anjoy your day!

I was thinking about not answering at all
because you have written the description without any formatting.

Your posting should look like this

I'm working with an Arduino Uno.

No I'm sorry for the misunderstanding, I meantioned in the reply that I now have the Code for the tof10120 and the stepper motor and that part I can now do myself.

But I need help with that python code which I wrote by myself. I'm not lazy I now did everything except I need to know if that code is correct in C++ because I'm a beginner at C++.

To explain my code:
That random number1 between 0 and 10 simulates the the variable with the distance in mm. And with the rest I calculate the differenz of the distance now and 1 second before because the tof remeasures every 1 second so number1 always changes.

With the differenz I know how much the distance increased or decreased within one second. So if the distance doesn't change I get a return of 0, when the distance increases from 20 to 21 then it returns 1 which then tells the motor to rotate let's say 1 step.

If the distance decreases from 20 to 18 for example it returns -2 which then tells the motor to turn 2 steps in the opposite direction.

The python code does exactly that differenz calculating so I need to know if I converted it right into C++ so then I can add the code for the motor and the code for the tof.

The code you have posted is standard C++-code which is not compilable with the arduino-IDE

The arduino-IDE expects two functions

void setup() {
}

void loop() {
}

From again not posting your existing ToF and your existing stepper-motor-code and using an online-service to translate python to C++
with mentioning

my assuming is:
You seem to expect copy & paste the three parts of the code and it will work. No it will surely not.

I recommend that for learning C++
Take a look into this tutorial:

Arduino Programming Course

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.

best regards Stefan

Thank you for your fast reply.
Of course I'll have to program more then just copy pasting these three parts together but I'm overburdened with writing all this code at once in Arduino which I choose to sepperate it into those three programming parts.

So now I know how to programm the tof and the stepper motor and I now know how to programm the part with which the tof can give the needed inforation to the stepper motor.
But I now have that code in python.

The tof will be delivered next week so I'll then programm with these 3 parts into 1 code and try to verify or correct the code which I'll have to take from the online Compiler. I realised that I need to have everything including the whole code in oder to ask for an opinion on a tiny piece of code.

You'll hear from me next week and enjoy your day!
Regards Michael

No.
You don't have to

You just have to post your own attempt of a code.

So far you have learned that you need a basic structure

for writing code with the arduino-IDE

You are claiming that you

So add at least a single line of code to your stepper-motor-demo-code
where in this added code you try to

into the direction you want the motor to rotate

And then post this non eventually non compiling but from you modified code

the most important part to get answers is the -

attention - the next line is not meant as shouting
It just shall emphasise

OWN ATTEMPT

This own attempt doesn't even have to compile. It just has to be an own attempt combined with a

SPECIFIC

question.

best regards Stefan

P.S. as a rhetoric question:

if you write and post such a modified code as described above

  • does your head explode?
  • will you get a very bad headache from it?
  • will this cause a hurricane or an earth-quake?
  • will your arduino or computer be overloaded and burn through?

NO ! Absolutely nothing special will happen. The only thing that happends is:

You will get answers how to modify your code to make it work.
If you are afraid of getting answers that really help - of course stay away!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.