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

Did you mean int x = analogRead (A1)

You say

so how are you reading the value of that one ?

build up skills slowly

do you understand this code?

const byte potPin = A1; // the pin where the wiper of the potentiometer is connected to

void setup() {
  Serial.begin(115200);
  Serial.println("Ready");
}

void loop() {
  // reading the pot's induced voltage value. 0 means 0V and 1023 means 5V - usually linear in between
  int potSample = analogRead(potPin);

  // map the value that we read between 0 and 1023 into a new value bewteen 100 and 200
  int numberOfSteps = map(potSample, 0, 1023, 100, 200);

  // print the outcome
  Serial.print("Nmber Of Steps = ");  Serial.println(numberOfSteps);

  // wait for 1s before looping
  delay(1000);
}

refer to post 5 for documentation on analogRead() and map()

run it with your Serial monitor opened at 115200 bauds and see what's going on when you move the wiper

1 Like

The analog examples that ship with the IDE provide a nice introduction, too.

Be sure: I tried them all !!!

...and ??

Another reference resource:

https://cplusplus.com/reference/

Yes, trigger is the value to match or beat from the pot, it must be >= the trigger value.

const is short for constant, it cannot be changed.

Change the pin is fine and make your own trigger values. You could set a value to turn in reverse.

Code is creative writing in logic regardless of what language you write in.

Learn C first as it fits small computers much better than C++ does and it is a much shorter study than C++, a thin book.

I'm sorry. I do not code on request. If you don't know how to do something, ask questions and I will do my best to answer them.

You need trust level 1, you can get there by:

  • Entering at least 5 topics
  • Reading at least 30 posts
  • Spend a total of 10 minutes reading posts

The requirements are not very onerous

Users at trust level 1 can…

  • Use all core Discourse functions; all new user restrictions are removed
  • Send PMs
  • Upload images and attachments

I had to wait one day until the community-bot allowed me to post another message.

But now : Well ... I have downloaded it at the recommended Baud-Rate and voilà: I can change the number of steps from 100 to 200 !!! That is absolutely brilliant.

I have now tried to use my brain and followed my intention: II have used the phrase "numberOfSteps" and exchanged it with the value "200" ... and what should I say ? It works !!!

I have now implemented everything into the main program code and it works.

I am working on a project called "Colour Shaker". I shall send you the complete code in another message. And maybe a circuit diagram also.

Chris

Sorry, I was sending the message to the wrong person :anguished:

Actually, maybe to the wrong people. :slight_smile: But in the end, I responded correctly because I know that people learn best by doing things themselves. Now you have that under your belt.

Good job

Did you mean, "another thread"?

I do not know why this happens. I click on "answer" under the thread of a specific person and another one receives it. I'm really sorry.

I don't know what you mean. I thought you were saying that you have a different project you'd like to talk about. So I recommended starting a new thread for that.

Hallo J-M-L !

I am sending you the complete code.

Pot on A0 is controlling the speed
Pot on A1 is controlling the depth

The complete code looks quite amateurish and dilettantish, I believe. Especially the use of brackets and wings look a bit weird.

Could you have a look at this and correct it, maybe ?

If you want, I can send you diagram also.

Chris

I was not able to upload the code as attachment.

So here it is as text:

// Stepper motor run code with a4988 driver
// by Superb and J-M-L Jackson

const byte potPin = A1; // the pin where the wiper of the potentiometer is connected to

const int stepPin = 3; // define pin for step
const int dirPin = 4;  // define pin for direction
int customDelay,customDelayMapped; // Defines variables

void setup() {
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);

  Serial.begin(115200);
  Serial.println("Ready");}

void loop() {

  // reading the pot's induced voltage value. 0 means 0V and 1023 means 5V - usually linear in between
  int potSample = analogRead(potPin);

  // map the value that we read between 0 and 1023 into a new value bewteen 100 and 200
  int numberOfSteps = map(potSample, 0, 1500, 20, 300);

  digitalWrite(dirPin, HIGH); // set direction, HIGH for clockwise, LOW for anticlockwise
 
 customDelayMapped = speedUp();
 
 for(int x = 0; x<numberOfSteps; x++) { // loop for 100 steps
  
  digitalWrite(stepPin,HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin,LOW); 
  delayMicroseconds(customDelayMapped);
 }
delay(200); // delay for 1 second
 
 digitalWrite(dirPin, LOW); // set direction, HIGH for clockwise, LOW for anticlockwise
 
 customDelayMapped = speedUp();
 
 for(int x = 0; x<numberOfSteps; x++) { // loop for 100 steps
  
  digitalWrite(stepPin,HIGH);
  delayMicroseconds(customDelayMapped);
  digitalWrite(stepPin,LOW); 
  delayMicroseconds(customDelayMapped);
 }
delay(200); // delay for 1 second
}
// Function for reading the Potentiometer
int speedUp() {
  int customDelay = analogRead(A0); // Reads the potentiometer
  int newCustom = map(customDelay, 0, 1500, 800, 17000); // Converts the read values of the potentiometer from 0 to 1023 (=5V !!!) into desireded delay values (300 to 4000)
  return newCustom;  
}

That is the correct way. An attachment is not the right way, unless the program is too large to post in a message.

If you only want to communicate with a single forum member, you can send a private message. But most helpers here, don't accept them. They want to keep the discussions public.

if you keep comments, try to make them useful or delete them.. There is no point to document something that is clearly not what the code does...

for the map : the analog read returns something between 0 and 1023, so why do you put 1500 there?


why doesn't A0 have a name?
also You might want to use unsigned long for anything related to timing


this would be somewhat better (the speed and number of steps is only read once per loop)

const byte potStep = A1;        // the pin where the wiper of the potentiometer is connected to
const byte potSpeedPin = A0;    // pot pin for handling speed
const int stepPin = 3;          // define pin for step
const int dirPin = 4;           // define pin for direction

void setup() {
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);

  Serial.begin(115200);
  Serial.println(F("Ready"));
}

void loop() {

  // reading the pot's induced voltage value. 0 means 0V and 1023 means 5V - usually linear in between
  int potSample = analogRead(potStep);

  // map the value that we read between 0 and 1023 into a new value between 20 and 300
  int numberOfSteps = map(potSample, 0, 1023, 20, 300);

  // calculate the desired speed, here we do it all in one go
  unsigned long customDelay = map(analogRead(potSpeedPin), 0, 1023, 800, 17000);

  // set direction clockwise
  digitalWrite(dirPin, HIGH);

  for (int x = 0; x < numberOfSteps; x++) {
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(customDelay);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(customDelay);
  }
  delay(200);

  // set direction anticlockwise
  digitalWrite(dirPin, LOW);

  for (int x = 0; x < numberOfSteps; x++) { // loop for 100 steps
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(customDelay);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(customDelay);
  }
  delay(200);
}

Hallo L-M-L !

Thank you very much ! The code looks great and very professional. I have accepted everything you changed there, and it works.

Without one exception: the Speed-Adjustment works only with 1500 instead of 1023. I know very well that 1023 means 5 Volt ... but ... if I set it to 1023 ... then the motors just trembles a bit ... and that's all. Don't ask me what that is ! Maybe it is the driver, which is not appropriate for the motor. I shall have a much bigger driver and motor for this project.

To explain what the device does: I constructed a very complex machine at the size of a billiard table. It can paint oil pictures in impressionistic style. I am the only one in this world who has one. Along with the machine there is a mixing console. Before mixing colors, they have to be diluted with turpentine. I do that in plastic bottles with a mixing ball. Until today, I was always shaking these bottles by hand but since I wrecked my shoulder from a bike accident, I am not able to do that anymore.

And here comes the color Shaker !

If you want I can delete all comments accept this one. What do you think ?