Stay until new value comes

Hello Arduino guys!
I am very new in this amazing area!
I am trying to control a stepper with 2 potentiometers, but I want the motor to move only if new value from either potentiometer comes. If anybody can help me. Thanks in advance So I think that I should make it with "while" loop, but always gives me an error:

In function 'void loop()':
Stepper:44:18: error: request for member 'newwriteValue1Received' in 'potPin1', which is of non-class type 'int'
while(!potPin1.newwriteValue1Received()){
^~~~~~~~~~~~~~~~~~~~~~
Stepper:46:24: error: request for member 'parseInt' in 'potPin1', which is of non-class type 'int'
readValue1=potPin1.parseInt();
^~~~~~~~
Stepper:49:17: error: request for member 'newwriteValue2Received' in 'potPin2', which is of non-class type 'int'
while(!potPin2.newwriteValue2Received()){
^~~~~~~~~~~~~~~~~~~~~~
Stepper:51:24: error: request for member 'parseInt' in 'potPin2', which is of non-class type 'int'
readValue2=potPin2.parseInt();
^~~~~~~~
F:\Projects\Arduino Segov\Stepper\Stepper.ino: At global scope:
Stepper:57:2: error: expected unqualified-id before 'while'
while(!potPin1.newwriteValue1Received()){
^~~~~
Stepper:62:2: error: expected unqualified-id before 'while'
while(!potPin2.newwriteValue2Received()){
^~~~~
exit status 1
request for member 'newwriteValue1Received' in 'potPin1', which is of non-class type 'int'

Welcome to the forum

Please post your full sketch, using code tags when you do

#include <Stepper.h>
int stepsPerRevolution = 2048;
int motSpeed = 10;
int dt1 = 100;
int dt2 = 250;
int potPin1 = A0;
int potPin2 = A1;
int readValue1;
int readValue2;
int writeValue1;
int writeValue2;

Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(potPin1, INPUT);
  pinMode(potPin2, INPUT);
  myStepper.setSpeed(motSpeed);
  myStepper.step(1);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(writeValue1);
  delay(500);
  Serial.println(writeValue2);
  delay(500);
  readValue1 = analogRead(potPin1);
  delay(dt1);
  readValue2 = analogRead(potPin2);
  delay(dt1);
  writeValue1 = (2048. / 1023.) * readValue1;
  delay(dt1);
  writeValue2 = -(2048. / 1023.) * readValue2;
  delay(dt1);
  myStepper.step(writeValue1);
  delay(dt2);
  myStepper.step(writeValue2);
  delay(dt2);

}

Did you deliberately ignore the request to use code tags ?

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

consider

#define PotPin  A0
#define Thresh   10

int potLst;

void setup(){
    Serial.begin (9600);
    potLst = analogRead (PotPin);
}

void doSomething (){
}

void loop(){
    int pot = analogRead (PotPin);

    if (abs(pot - potLst) > Thresh)  {
        potLst = pot;

        doSomething();
    }
}

I'll try as soon as I get home.

posting code is very easy
You can post code by using this method that adds the code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

in your case you should

re-edit your first posting

by following this short explanation

best regards Stefan

1 Like

Thanks, I think I did copy the code.

potPin1 is an integer type. What is newwriteValue1Received() and where did it come from? parseInt() looks suspiciously like a serial function.

I can't tell from your code what you are trying to accomplish.

1 Like

My exercise must be to control a stepper with 2 potentiometers, but the task in my mind is the stepper to move only when a new value of either pot1 or pot2 comes and not to move if the value still same. I tried to do some while loop but I know that what I typed inside is incorrect.

Please post the sketch that produces the error; the sketch in post #3 does not seem to be the one.

You wrote that you are new to coding.
This means there is a chance to have some mis-conceptions about how programming works.

you should write a pure functional description what the stepper-motor shall do.

Pure functional means avoiding any kind of programming terms. Describe the wanted function with normal everyday words.

I connect an extra powersupply to my stepper-motor
then I connect power to my arduino
The stepper-motor shall .......
If I turn potentiometer "A" to a new position the stepper-motor shall do....
if I turn potentiometer "B" to a new position the stepper-motor shall do....
etc. etc.

as a first step we the other users need a clear picture of what you want to do.
only if the wanted functionality is clear further advice can be given.

best regards Stefan

1 Like

I thought that with this command will make the servo to move only if new value comes but it is not what I expected :slight_smile:

It is almost what is in your words

  1. I connect power supply (same for both Stepper driver and Arduino)
  2. Stepper-motor shall start of position 0 of its shaft
  3. Arduino shall read the information of pot1 and pot2 and send it to the Stepper
  4. Once the information is sent and done by the Stepper it must Stay and wait only if new Values of pot1 and pot2 comes to move
  5. lets say if pot1 Value comes should move clockwise and if pot2 Value comes should move anti-clockwise.

this means you have to do a compare of "old" value to actual value.

This means after stepper has moved the values must be stored in two additional variables
and then you have to code comparing
if oldValue1 != actualValue1

and

if oldValue2 != actualValue2

if this condition is true move stepper
and make
oldValue1 = actualValue1
oldValue2 = actualValue2

the behaviour will be

you can turn pot1 to any other position nothing yet happends
as soon as you turn pot2 just a little bit
if oldValue2 != actualValue2 evaluates to true
stepper moves clockwise for value1 steps
stepper moves anticlockwise for 1 step

only one step because as soons as actualValue2 deviates by 1 the condition becomes true
after moving this one step stepper stops again

if you go on turning pot2 nothing will happen. Only if you turn pot1 you get the same behavoiur
moving actualValue2 steps ccw and one step cw

I guess this is not what you want to have.

So you have to describe with even more precision what you want to have

best regards Stefan

1 Like

I will fallow your suggestion and will post what I will done as soon as possible.
Thanks!

#include <Stepper.h>
int stepsPerRevolution = 2048;
int motSpeed = 10;
int dt1 = 100;
int dt2 = 250;
int potPin1 = A0;
int potPin2 = A1;
int readValue1;
int readValue2;
int writeValue1;
int writeValue2;
int actualValue1;
int actualValue2;
int oldValue1;
int oldValue2;


Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);

void setup() {
  // put your setup code here, to run once:
  
  Serial.begin(9600);
  pinMode(potPin1, INPUT);
  pinMode(potPin2, INPUT);
  myStepper.setSpeed(motSpeed);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  actualValue1=readValue1;
  actualValue2=readValue2;
  
  oldValue1=actualValue1;
  oldValue2=actualValue2;
  
  Serial.println(writeValue1);
  delay(75);
  Serial.println(writeValue2);
  delay(75);
  readValue1 = analogRead(potPin1);
  delay(dt1);
  readValue2 = analogRead(potPin2);
  delay(dt1);
  writeValue1 = (2048. / 1023.) * readValue1;
  delay(dt1);
  writeValue2 = -(2048. / 1023.) * readValue2;
  delay(dt1);

  if (oldValue1!=actualValue1) {

    myStepper.step(writeValue1);
    
    }

    if (oldValue2!=actualValue2) {

    myStepper.step(writeValue2);
    
    }
  

}

Could you please tell me how to store old data. Because what I did in the "If loop" of course doesn't work because I did it the old to be equal to the new... And I do not know how to describe witch is the old and to compare with the new.

you just follow my description

more precisely

has to be coded after moving the stepper
which means it has to be below the commands that move the stepper

There is a very old programmer-wisdom:
Your code does always what you have coded. If the code does something different than you expected you don't yet understand what you have coded.

programming means to

analyse

very very VERY precisely what functionality you want to have.
To be able to do this you have to

know

quite a lot
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

1 Like

you're doing it. but whyu the delay?

  oldValue1=actualValue1;
  readValue1 = analogRead(potPin1);

without any hysterisis, doesn't this cause a lot of jitter?

is the pot intended to be the desired position or the distance to move?
how do you change the direction of the stepper motor or is there a command to move it to a specific position instead of some # of steps?

1 Like

Actually this is just a task in my mind, because I want to learn arduino but in the same time to do some task wich has some purpose. I imagine how this Stepper is engaged with a shaft lets say to open and close but also to have not only open and close positions but to smoothly control a flow thru some pipe or whatever is needed. So my thoughts about those potentiometers are like they simulate some sensors.