Changing values in a code by using a potentiometer or selector switch

Hello world !

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

Welcome to the forum

I promise you that we will not be bored

Auto Format the sketch in the IDE then copy it using "Copy for forum" to automatically add code tags and post it here in a new post

Maybe you could adapt this:

Moin chris-kleinfeld

We want to learn something as well.

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)).

this could be useful :

reading a pot connected to A0

int x = analogRead(A0); ➜ read https://docs.arduino.cc/built-in-examples/basics/ReadAnalogVoltage

mapping a value from a given range to another range
int y = map(x, 0, 1023, 50, 200); // from [0, 1023] to [50,200] ➜ read map() - Arduino Reference

if you want to use a rotary encoder ➜ use the encoder library to make your life easy.

you might also want to constrain() the value

1 Like

A new post in this topic, which is what you have done, rather than a new topic

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

Seems like we are on the right track. Nice people around. They all want to help us. # 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.

Chris

Those examples will be reading a number using analogRead. Put that number into am int variable and use it instead of 200.

Which part of

Ein neuer Beitrag in diesem Thema, was Sie getan haben, und kein neues Thema

do you not understand ?

Please do not laugh ! Do I have to write (at the end of the recommended code from you) :

int xyz

and then put the "xyz" into the space where the "200" was ?

Oh, I feel so inferior ! # Chris

if you want to use xyz instead of 200, it would make a lot of sense to load into xyz the right value before the for loop, don't you think?

How does one do that ? :innocent:

By reading post 5 and using xyz instead of x?

This might work, untested.

// 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);
 }
}

Yes. It's not usually something people pick up overnight.

Thank you ! Do I understand this right ?

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 will try to check this out. # Chris

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 !