Wave Machine Project

Hi All and thank you in advance for any help. First time posting so sorry if I get it a bit wrong.

I am building a wave machine with a stepper motor, driving a belt connected to a paddle submergedged in a water tank to generate small waves.

I have managed to adapt someones code so the paddle moves back and forth the the correct amount however, I wish to control its speed with a potentiometer (with the back and fourth motion looping).

I have tried unsuccessfully to further adapt code to use the potentiometer as a speed control but with no success. I’ll post the code that I’ve written at the bottom.

I am using:

Nema 17 Steppers
TB6600 Motor Driver
B10K Potentiometer
Freenove Uno (I believe it’s the same as the arduino uno as it does work)

Here is the code:

#define dirPin 2
#define stepPin A0 // Potentiometer
#define stepsPerRevolution 320

// Variables

int pd = 500; //Pulse Delay Period

void setup() {

pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);

}
void loop() {

pd = map((analogRead(pd)),0,1023,1023,0);
digitalWrite(dirPin, HIGH);

for (int i = 0; i < stepsPerRevolution; i++) {

digitalWrite(stepPin, HIGH);
delayMicroseconds(pd);
digitalWrite(stepPin, LOW);
delayMicroseconds(pd);
}
delay(300);
pd = map((analogRead(pd)),0,1000,2000,100);
digitalWrite(dirPin, LOW);

for (int i = 0; i < stepsPerRevolution; i++) {

digitalWrite(stepPin, HIGH);
delayMicroseconds(pd);
digitalWrite(stepPin, LOW);
delayMicroseconds(pd);
}

delay(300);
}

Thank you in advance.

First time posting so sorry if I get it a bit wrong

Please Read this before posting a programming question and follow the instructions

Properly presented code is more likely to be read and answers provided

Besides posting the code properly, it would help if you describe what the code actually does and how that differs from what you want.

pd = map((analogRead(pd)),0,1023,1023,0); :o

Hi groundFungus

The code runs the motor 320 steps in forward then in reverse. This part does work. I've then tried to write code to incorporate a potentiometer to allow speed control.

At the moment as the potentiometer is turned the motor stops, however when the potentiometer is brought back to zero(?) the motor then increases and sometimes decreases in speed. It's very hit and miss. Currently the potentiometer does not allow for a smooth increase in speed at all.

In the line:

pd = map((analogRead(pd)),0,1023,1023,0);

I have tried playing with the numbers to match the potentiometer but had no luck.

Many thanks for taking a look.

I have tried playing with the numbers to match the potentiometer but had no luck.

What exactly are you trying to do with that line ?

Have you tried printing the value of the analogRead() ? Have you tried printing the value of pd ?

Currently the potentiometer does not allow for a smooth increase in speed at all.

That is likely because you are using, blocking, delay() in your code. The Arduino does nothing during a delay() so cannot respond to input quickly.

Robin2's simple stepper code has an example using micros() for non-blocking timing stepper steps.

Non-blocking timing tutorials: Several things at a time. Beginner's guide to millis(). Blink without delay().

If you want to reverse the value from analogWrite() you can use:

reverseValue = 1023 - analogRead(analogInputPin);  //  analog read = 1023, reverseValue = 0

Read the how to use this forum-please read sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

Hi UKHeliBob

I'm sorry but I don't understand what printing the value means?

I was trying to match the numbers to the potentiometer, I thought 1023 was full speed and 0 was stop?!

I'm beginning to realise I'm probably a long way from understanding the coding properly. :o

Look up serial print

I'm sorry but I don't understand what printing the value means?

I meant that you should read the analogue value into a variable and print the value. The do the map() and print the value of pd to see whether the values looked reasonable and were printed when you expected them to be