I am not able to write complicated codes, but I have realized some very useful projects with Arduino UNO and stepper motors.
Here is my question / problem:
I am using a code which allows me to turn a stepper motor for a certain amount of steps and then pause and then run backward.
Here is the part of the code which includes the number of steps to rotate:
for(int x = 0; x<200; x++) { // loop for 200 steps
I will not bore you with the complete code, which is about 20 lines long.
Can you see the "200" in there ? I want to change the value to (let's say) 50 ... 75 ... 100 ... and so forth. Maybe nine stages or something.
Best would be a potentiometer, but I learned from googling around that this is not possible ... or ... maybe too complicated.
Has anyone an idea, how to solve this problem ?
If ever you can: write the complete code for this, for I'm a complete idiot and would not be able to write a code after getting some hints or advice or reference.
Thank you very much in advance from Chris in Hamburg / Germany
post your best attempt (please read How to get the best out of this forum and post accordingly (including code tags and necessary documentation for your ask)).
Hallo Heli-Bob from UK ! Is okay, when I post the code here ? Or do I have to open a complete post ? Here is the code that I fetched from a YouTube video from India. I was able to change the code in so far that the motor now turns in both directions back and forth with a little delay in between. But the code of interest is absolutely the same. Here is the code:
// Stepper motor run code with a4988 driver
// by Superb
const int stepPin = 3; // define pin for step
const int dirPin = 4; // define pin for direction
void setup() {
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
}
void loop() {
digitalWrite(dirPin, HIGH); // set direction, HIGH for clockwise, LOW for anticlockwise
for(int x = 0; x<200; x++) { // loop for 200 steps
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
delay(1000); // delay for 1 second
}
The number of "200" here is what has to be changed. Thank you for your help. # Chris
Thank you very much. I have heard before that one can use a potentiometer as a selector switch. There are some examples on the Internet and on YouTube. But the thing is, that I would not know how to change my specific code line in a way that it receives a result from this procedure.
I also appreciate the link to the Arduino Language reference. I was already searching for something like this. If ever I could, I would like to lean coding in C+. But this might surely take some time.
// Stepper motor run code with a4988 driver
// by Superb
// revised by GFS
const int stepPin = 3; // define pin for step
const int dirPin = 4; // define pin for direction
const byte potPin = A0;
const int trigger = 600;
byte runIfTrue;
void setup() {
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(potPin, INPUT); // will be analogread
digitalWrite(dirPin, HIGH); // set direction, HIGH for clockwise, LOW for anticlockwise
}
void loop() {
if ( analog read( potPin ) >= trigger ) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
}
I will have to assign a Pot-Input (in this case I will use A1 because A0 ist still in action).
The "const int trigger" limits the amount to 600, right ?
While "trigger" is the value that comes from the Potentiometer, right ?
pinMode (potPin,INPUT) makes the Pin A1 "ready for read", right ?
But then:
Do I have to delete the line "for(int x = 0; x<200; x++)"
and use " if ( analog read( potPin ) >= trigger ) {" instead ???
I have prepared a breadboard with two pots. One (the A0) for the speed (that works well already) and the other one (the A1) for the "depth" meaning how far the motor will move back and forth.
I meant "learn" not "lean" ... but yes ... especially in my age ... I'm almost 70 ... this is a hard thing to do. Especially because I'm only USING the codes. Sometimes change them or mix them. Never create new ones from scratch. I was building quite complex machines by using the GRBL code for the most of everything I needed. This works reliably since many years.
Oh yes. I have some experience with the assignment of Potentiometers and I already use one. I have already tried to use "xyz" as the trigger-value ... but nothing happened.
New for me is the line "int x analogRead (A1)"
(I am using A1 because A0 is already occupied).
I shall try to check this out.
You see: since I never learned "C+" all I can do is to process "learning by doing". Sorry for my amateurism !