Solved - running servo with four intervals in if statement w/o delay

Hi all, I’m pretty new at programming and I can’t figure out a way to make this happen the way I’d like it to. I used SeveralThingsAtTheSameTime and pulled out what I needed. I’m using a servos that I would like to run with four different interval speeds. I will eventually be running this alongside a second servos doing something similar but I wanted to get one working before I got the second one going. I want it to loop through the four interval speeds (millisecs) from one to four and restart at one.

The part I’m having issues with is

if (servoInterval == servoInterval1) {
servoInterval = servoInterval2;

}
else if (servoInterval == servoInterval2) {
servoInterval = servoInterval3;
}
else {
servoInterval = servoInterval4;
}

I can get it to interval 3 but not to 4. I’ve tried a bunch of different types of structure to make it loop back to interval1 and I can’t seem to make it work since it has completed the else statement and stays on Interval 3. I’m not sure what I’m missing but it seems like it would be a simple thing. It seems like this is a case of, I don’t know what I don’t know. Is there a specific way I should be setting this up or using loop or return?

It is going to be a pan and tilt with a laser with the only other input being a light sensor so that when it is light out, the servos will turn on. I’ll add the light sensor part later.

// SeveralThingsAtTheSameTimeRev1.ino

// An expansion of the BlinkWithoutDelay concept to illustrate how a script
//  can appear to do several things at the same time

// this sketch does the following
//   
//    it sweeps a servo (connected to pin 5) back and forth at different speeds


//  The Arduino is not capable of supplying enough 5v power to operate a servo
//    The servo should have it's own power supply and the power supply Ground should
//      be connected to the Arduino Ground.

// The sketch is written to illustrate a few different programming features.
//    The use of many functions with short pieces of code. 
//       Short pieces of code are much easier to follow and debug
//    The use of variables to record the state of something (e.g. onBoardLedState) as a means to
//       enable the different functions to determine what to do.
//    The use of millis() to manage the timing of activities
//    The definition of all numbers used by the program at the top of the sketch where 
//       they can easily be found if they need to be changed

//=======

// -----LIBRARIES

#include <Servo.h>

// ----CONSTANTS (won't change)

const int servoPin = 9; // the pin number for the servo signal

const int servoMinDegrees = 0; // the limits to servo movement
const int servoMaxDegrees = 45;


//------- VARIABLES (will change)



Servo myservo;  // create servo object to control a servo 

int servoPosition = 0;     // the current angle of the servo - starting at 0.
int servoInterval1 = 100; // millisecs between servo moves
int servoInterval2 = 10;
int servoInterval3 = 50;
int servoInterval4 = 80;
int servoInterval = servoInterval1; // initial millisecs between servo moves
int servoDegrees = 2;       // amount servo moves at each step 
                           //    will be changed to negative value for movement in the other direction

unsigned long currentMillis = 0;    // stores the value of millis() in each iteration of loop()

unsigned long previousServoMillis = 0; // the time when the servo was last moved

//========

void setup() {

 Serial.begin(9600);
 Serial.println("Starting SeveralThingsAtTheSameTimeRev1.ino");  // so we know what sketch is running

 myservo.write(servoPosition); // sets the initial position
 myservo.attach(servoPin);

}

//=======

void loop() {

     // Notice that none of the action happens in loop() apart from reading millis()
     //   it just calls the functions that have the action code

 currentMillis = millis();   // capture the latest value of millis()
                             //   this is equivalent to noting the time from a clock
 
 servoSweep();

}

//========


//========

void servoSweep() {

     // this is similar to the servo sweep example except that it uses millis() rather than delay()

     // nothing happens unless the interval has expired
     // the value of currentMillis was set in loop()
 
 if (currentMillis - previousServoMillis >= servoInterval) {
       // its time for another move
   previousServoMillis += servoInterval;
   
   servoPosition = servoPosition + servoDegrees; // servoDegrees might be negative
   if (servoPosition <= servoMinDegrees) {
         // when the servo gets to its minimum position change the interval to change the speed
        
        if (servoInterval == servoInterval1) {
        servoInterval = servoInterval2;
        
        }
        else if (servoInterval == servoInterval2) {
       servoInterval = servoInterval3;
        }
        else {
          servoInterval = servoInterval4;
        }
      
      
   }
   if ((servoPosition >= servoMaxDegrees) || (servoPosition <= servoMinDegrees))  {
         // if the servo is at either extreme change the sign of the degrees to make it move the other way
     servoDegrees = - servoDegrees; // reverse direction
         // and update the position to ensure it is within range
     servoPosition = servoPosition + servoDegrees; 
   }
       // make the servo move to the next position
   myservo.write(servoPosition);
       // and record the time when the move happened
 }
}


//=====END

dukekyler:
The part I'm having issues with is

Please post your program and tell us exactly what happens when you run it and what you want it to do that is different.

...R

Your if never set servoInterval to servoInterval1, so servoInterval stops changing when you set it to servoInterval4.

The code should be

if (servoInterval == servoInterval1) 
{
        servoInterval = servoInterval2;
}

else if (servoInterval == servoInterval2) 
{
       servoInterval = servoInterval3;
}

else if (servoInterval == servoInterval3)
{
          servoInterval = servoInterval4;
}

else
{
          servoInterval = servoInterval1;
}

vlc0617:
Your if never set servoInterval to servoInterval1, so servoInterval stops changing when you set it to servoInterval4.

The code should be

The issue is that it doesn't progress past the Interval3 section and simply goes back and forth at the interval3 speed indefinitely, whether or not I've added the next else if section.

here is that section updated and it still stays at the interval3 speed.

if (servoInterval == servoInterval1) {
        servoInterval = servoInterval2;
        
        }
        else if (servoInterval == servoInterval2) {
       servoInterval = servoInterval3;
        }
        else if (servoInterval == servoInterval3) {
          servoInterval = servoInterval4;
        }
        else {
          servoInterval = servoInterval1;
        }
      
   }

Robin2:
Please post your program and tell us exactly what happens when you run it and what you want it to do that is different.

...R

I posted the full code at the bottom of the post. When I run the program, it goes through the interval1, interval2 and interval3 speeds properly and then stays at the interval3 speed indefinitely.

Well, it seems to be working now, for no real reason that I can detect other than fiddling with the interval speeds. But thank you anyway!!!

dukekyler:
I posted the full code at the bottom of the post.

The only program that I can see is SeveralThingsAtTheSameTimeRev1.ino at the bottom of your Original Post

...R