Activate motors when potvalue is read, using millis

Hello Arduino people,

For a project for school I must use a stepper motor, geared DC motor and a servo motor.
Now I have written this code but it doesn't do anything and I don't know what's wrong.
I don't have the potmeter yet so I don't know those values yet eather.
I have tested my code without the ''potmeter if statements''.
This is probably a minor mistake again, but I am still a big rookie with Arduino and programming.

Can someone take a look at my code and help me? It would mean a lot to me!
The code can be found in the attachment.

Hot_Beverage_Machine.ino (8.83 KB)

Have you managed to control the 3 types of motor in individual sketches ?
How are the motors powered and connected to the Arduino ?

chineesinspace:
Now I have written this code but it doesn't do anything and I don't know what's wrong.

"doesn't do anything" gives us no useful information from which to help you.

Presumably you tested the program several times before it got to its present size. At what point did you first run into a problem?

...R

The motors are able to run at the same time in another project with the motors. Although I still have problems with the speed of the steppermotor. Your code waits for the steppermotor to complete its steps, so that might cause your problem(?).

And i'm sorry for the vague description. I indeed tested it multiple times. The motors do not run at all. Sometimes I do notice a bit of weird twitching at my servo... But that is not what it supposed to do.

Also further explanation on what the motors should do can be read in the comments of my code.

chineesinspace:
Your code waits for the steppermotor to complete its steps, so that might cause your problem(?).

What code would that be ?

chineesinspace:
And i'm sorry for the vague description. I indeed tested it multiple times.

I presume some of those earlier versions worked? If so post the latest working version and focus on what has changed since then.

...R

chineesinspace:
I don't have the potmeter yet so I don't know those values yet eather.
I have tested my code without the ''potmeter if statements''.

In the absence of an actual pot, you can hard-code or randomise some values for testing. Say you have a variable called potVal, to represent the value read from say potPin then instead of:

potVal = analogRead(potPin);

... you could hard-code say

potVal = 326;

... and later re-compile as:

potVal = 927;

.... or whatever.

Or use random() :

potVal = random(0, 1024); // goes from 0 to 1024-1

There's some weird and I think flawed logic in your servomotors routine, for one (I didn't look at the others...)

You update currentMillis once and then enter a loop waiting for it to be greater than 20000; I don't see you update currentMillis within the loop so I don't think this condition will ever be met.

As well, right after the while() you "previousMillis2 = currentMillis;", further ensuring the two won't differ by one, let alone 20000.

void servomotorfunction() 
{                                                 
    //Once the potmeter reads a value, the servo should go to a position and go back to initial position in steps of 2.3 with time interval of 0.5s . All this will happen within 20 s.


    unsigned long 
        currentMillis = millis();       // set a variable which has the current time as value
    
    if (potvalue > 2) 
    {                                                  
        //if potvalue is bigger than 2
        while( currentMillis - previousMillis2 < 20000 ) 
        {
            //while millis time is not 200000 ms yet then....
            previousMillis2 = currentMillis;
            if (currentMillis - previousMillis21 > 500) 
            {                       
                // each 0.5 second, the if statement will happen
                previousMillis21 = currentMillis;                                // reset the timer, so it starts measuring time again until interval of 500 is reached
                //give servo position
                servomotor.write(x);                                             
                if (x < 0) 
                {
                    //if x = smaller than 0 give sevro position -90                                                    
                    x = -90;                                                       
                }//if
                
                // add 2.3 to position x every 0.5 second
                x += 2.3;
                
            }//if
            
        }//while
        
    }//if
    
}//servomotorfunction

UKHeliBob:
What code would that be ?

It is just a code rule in general i think. I had a problem last time that my servo did not move correctly because of the code was waiting for the stepper motor to finish its steps

Blackfin:
You update currentMillis once and then enter a loop waiting for it to be greater than 20000; I don't see you update currentMillis within the loop so I don't think this condition will ever be met.

As well, right after the while() you "previousMillis2 = currentMillis;", further ensuring the two won't differ by one, let alone 20000.

Ahaa so that might be the mistake, although I do not fully understand what you mean. I did update the currentMillis wit the previousMillis2 and the currentMillis with previousMillis21 right? How should I do this correct?

My goal is that the servo starts taking positions (after potvalue is read) during a time of 20 seconds and the switch of position should take place every half a second.... but I don't know how else I should do this.