Replacing delay with mills

I am working on a project to simulate a few things that proxy sensors that my company uses on a drilling rig does. The first thing is tracking the movement of blocks going up and down which we track using an on-on off-off pulse. I wrote the following code to use a joystick from an old RC helicopter remote to simulate the sequence allowing it to decrease the time between the sequence as it gets further away from the joystick neutral position. My code works and it does exactly what I want it to do but I know that using delay is a big no-no when trying to “multitask” the arduino but it was the only example I could find to do what I want to do.

My FIRST issue is trying to convert the delay to mills but I’m not too sure how to do it with the variable delay I’m using. Any help would be appreciated so that I can learn for the future, keep in mind that I’m relatively new to programming but I understand it somewhat.

My SECOND issue is can I do the following things with a single arduino or would I need multiple arduinos?

I also intend to later add a switch to only simulate the “joystick down” portion of the sequence based on the position of another potentiometer. I will never use the joystick and switch to control those outputs at the same time.

I would also like to add 2 more switches with their own potentiometer to control the speed of 2 more single on-off pulses, one for each switch.

I would like to be able to run the 3 switches at the same time and still be able to change the speeds of each sequence independently.

Thanks in advance.

  // put your setup code here, to run once:
  const int joystickInput = A0;
  const int proxA = 13;
  const int proxB = 12;

  int potValue = 0;
  int outputValue = 0;
  
void setup() {
  
  // designate proxA and proxB as an OUTPUT pin for the LED
  pinMode (proxA, OUTPUT);
  pinMode (proxB, OUTPUT);

  // designate joystickInput as an Analog INPUT for the potentiometer
  pinMode (joystickInput, INPUT);

  
  Serial.begin(9600);
  
}

void loop() {
  
  potValue = analogRead(joystickInput);
  outputValue = map(potValue, 0, 1023, 100, -100);  //map full up to 100 and full down to -100
  
  
  Serial.println  (outputValue);

  if (outputValue > 10) {  //do this when joystick is pressed from the neutral position up
    int upDelay = map(outputValue, 10, 100, 1000, 1);  //map the delay progressivly less as joystick is moved from neutral to full up
      digitalWrite (proxA, HIGH);
      delay (upDelay);
      digitalWrite (proxB, HIGH);
      delay (upDelay);
    
      digitalWrite (proxA, LOW);
      delay (upDelay);
      digitalWrite (proxB, LOW);
      delay (upDelay); }

  else if (outputValue < -10) {  //do this when joystick is pressed from the neutral position down
    int downDelay = map(outputValue, -10, -100, 1000, 1);  //map the delay progressivly less as joystick is moved from neutral to full down
      digitalWrite (proxB, HIGH);
      delay (downDelay);
      digitalWrite (proxA, HIGH);
      delay (downDelay);
    
      digitalWrite (proxB, LOW);
      delay (downDelay);
      digitalWrite (proxA, LOW);
      delay (downDelay); }


   else {  //do this when joystick is in neutral position
      digitalWrite (proxB, LOW);
      digitalWrite (proxA, LOW);}
  
}

Have a read up on the state machines In the tutorials section

You need states where you are...

  1. Waiting for an input from the joystick / neutral position
    When you get an input, the you need to move through the states as follows, setting ProxA and proxB outputs appropriately.
  2. Doing delay 1
  3. Doing delay 2
  4. Doing delay 3
  5. Doing delay 4

The trigger for moving between each of the delay states should be that a certain number of millis have elapsed. How to detect that can be seen from the using millis tutorials. The fact you have a variable as the delay (as opposed to a constant) makes next to no difference to the code.

No reason you can’t extend thIs so you are doing more than one thing at once. So no need for multiple Arduinos.

I will look at the state machines and post if I'm still having issues. I know what I want to do my problem is that I haven't really programmed in 25 years and that was Turbo Pascal in high school. I appreciate the help.

Here’s an example

// put your setup code here, to run once:
const int joystickInput = A0;
const int proxA = 13;
const int proxB = 12;

enum { NEUTRAL, UP, DOWN };
int dir = NEUTRAL;
int state;

unsigned long currentDelay;
unsigned long startTime;

void setup() {

  // designate proxA and proxB as an OUTPUT pin for the LED
  pinMode (proxA, OUTPUT);
  pinMode (proxB, OUTPUT);

  // designate joystickInput as an Analog INPUT for the potentiometer
  pinMode (joystickInput, INPUT);

  Serial.begin(9600);
}

void loop() {

  unsigned long currentTime = millis();

  int potValue = analogRead(joystickInput);
  int outputValue = map(potValue, 0, 1023, 100, -100);  //map full up to 100 and full down to -100

  Serial.println(outputValue);

  if (outputValue > 10) {
    //do this when joystick is pressed from the neutral position up
    currentDelay = map(outputValue, 10, 100, 1000, 1);  //map the delay progressivly less as joystick is moved from neutral to full up
    if (dir != UP) {
      // first time joystick went up from neutral or from down
      dir = UP;
      state = 0;
      startTime = currentTime;
      digitalWrite(proxA, HIGH);
    }
    else {
      // joystick has been up so see if time to move to next state
      if ( currentTime - startTime >= currentDelay ) {
        state++;
        switch (state) {
          case 1:
            digitalWrite(proxB, HIGH);
            break;
          case 2:
            digitalWrite(proxA, LOW);
            break;
          case 3:
            digitalWrite(proxB, LOW);
            break;
          case 4:
            digitalWrite(proxA, HIGH );
            state = 0;  // last state so roll over to zero
            break;
        }
        startTime = currentTime;
      }
    }
  }
  else if (outputValue < -10) {
    //do this when joystick is pressed from the neutral position down
    currentDelay = map(outputValue, -10, -100, 1000, 1);  //map the delay progressivly less as joystick is moved from neutral to full down
    if (dir != DOWN) {
      // first time joystick went down from neutral or from up
      dir = DOWN;
      state = 0;
      startTime = currentTime;
      digitalWrite(proxB, HIGH);
    }
    else {
      // joystick has been up so see if time to move to next state
      if ( currentTime - startTime >= currentDelay ) {
        state++;
        switch (state) {
          case 1:
            digitalWrite(proxA, HIGH);
            break;
          case 2:
            digitalWrite(proxB, LOW);
            break;
          case 3:
            digitalWrite(proxA, LOW);
            break;
          case 4:
            digitalWrite(proxB, HIGH );
            state = 0;  // last state so roll over to zero
            break;
        }
        startTime = currentTime;
      }
    }
  }
  else {
    //do this when joystick is in neutral position
    dir = NEUTRAL;
    digitalWrite (proxB, LOW);
    digitalWrite (proxA, LOW);
  }
}

blh64:
Here’s an example

// put your setup code here, to run once:

const int joystickInput = A0;
const int proxA = 13;
const int proxB = 12;

enum { NEUTRAL, UP, DOWN };
int dir = NEUTRAL;
int state;

unsigned long currentDelay;
unsigned long startTime;

void setup() {

// designate proxA and proxB as an OUTPUT pin for the LED
  pinMode (proxA, OUTPUT);
  pinMode (proxB, OUTPUT);

// designate joystickInput as an Analog INPUT for the potentiometer
  pinMode (joystickInput, INPUT);

Serial.begin(9600);
}

void loop() {

unsigned long currentTime = millis();

int potValue = analogRead(joystickInput);
  int outputValue = map(potValue, 0, 1023, 100, -100);  //map full up to 100 and full down to -100

Serial.println(outputValue);

if (outputValue > 10) {
    //do this when joystick is pressed from the neutral position up
    currentDelay = map(outputValue, 10, 100, 1000, 1);  //map the delay progressivly less as joystick is moved from neutral to full up
    if (dir != UP) {
      // first time joystick went up from neutral or from down
      dir = UP;
      state = 0;
      startTime = currentTime;
      digitalWrite(proxA, HIGH);
    }
    else {
      // joystick has been up so see if time to move to next state
      if ( currentTime - startTime >= currentDelay ) {
        state++;
        switch (state) {
          case 1:
            digitalWrite(proxB, HIGH);
            break;
          case 2:
            digitalWrite(proxA, LOW);
            break;
          case 3:
            digitalWrite(proxB, LOW);
            break;
          case 4:
            digitalWrite(proxA, HIGH );
            state = 0;  // last state so roll over to zero
            break;
        }
        startTime = currentTime;
      }
    }
  }
  else if (outputValue < -10) {
    //do this when joystick is pressed from the neutral position down
    currentDelay = map(outputValue, -10, -100, 1000, 1);  //map the delay progressivly less as joystick is moved from neutral to full down
    if (dir != DOWN) {
      // first time joystick went down from neutral or from up
      dir = DOWN;
      state = 0;
      startTime = currentTime;
      digitalWrite(proxB, HIGH);
    }
    else {
      // joystick has been up so see if time to move to next state
      if ( currentTime - startTime >= currentDelay ) {
        state++;
        switch (state) {
          case 1:
            digitalWrite(proxA, HIGH);
            break;
          case 2:
            digitalWrite(proxB, LOW);
            break;
          case 3:
            digitalWrite(proxA, LOW);
            break;
          case 4:
            digitalWrite(proxB, HIGH );
            state = 0;  // last state so roll over to zero
            break;
        }
        startTime = currentTime;
      }
    }
  }
  else {
    //do this when joystick is in neutral position
    dir = NEUTRAL;
    digitalWrite (proxB, LOW);
    digitalWrite (proxA, LOW);
  }
}

So that works but I’m not sure that I understand it. I’ve looked at the tutorials but I think my lack of programming background is hindering my understanding. Thanks for the code, I’ll just keep plugging away at understanding.

My specific issue is that I don’t understand how you made the delay work based upon the positioning of the pot.

mpboyer3496:
My specific issue is that I don’t understand how you made the delay work based upon the positioning of the pot.

That part of the code comes directly from your code, using the map() function to set currentDelay. The rest of the code then check how much time has passed since we toggled a pin (proxA or proxB) and if enough time has passed, it moves to the next “toggle”. Each time through loop() is will recalculate the currentDelay. The only trick is detecting the difference between when you first move the joystick vs. coming through loop() again and seeing the joystick is still not neutral. The first time, you start the sequence, all subsequent times, you just check elapsed time and move to the next state when needed.

So what you did worked perfectly but I also need to add in the ability for the following switch to do what the "DOWN" function does using the following map to map the adrSpeed(map(potValue, 124, 931, 500, 40)) but I have no idea how to write the code for it. I have tried and failed. I want the potentiometer which is adrSpeed to control how fast only when the switch adrSwitch is on, I will not be using the joystick when this switch is on.

const int adrSwitch = 2;
const int adrSpeed = A1;

Essentially I want to add the following to the sketch using mills()

  // put your setup code here, to run once:
  
  const int proxA = 13;
  const int proxB = 12;
  const int adrSwitch = 2;
  const int adrSpeed = A1;

  int outputValue = 0;
  int buttonState = LOW;
  
void setup() {
  
  // designate proxA and proxB as an OUTPUT pin for the LED
  pinMode (proxA, OUTPUT);
  pinMode (proxB, OUTPUT);

  
  Serial.begin(9600);
  
}

void loop() {
  
  int potValue = analogRead(adrSpeed);
  buttonState = digitalRead(adrSwitch);
  
  Serial.println  (outputValue);

  if (buttonState == HIGH) {  //do this when joystick is pressed from the neutral position up
    int upDelay = map(potValue, 124, 931, 500, 40);  //map the delay progressivly less as joystick is moved from neutral to full up
      digitalWrite (proxB, HIGH);
      delay (upDelay);
      digitalWrite (proxA, HIGH);
      delay (upDelay);
    
      digitalWrite (proxB, LOW);
      delay (upDelay);
      digitalWrite (proxA, LOW);
      delay (upDelay); }



   else {  //do this when switch is off
      digitalWrite (proxB, LOW);
      digitalWrite (proxA, LOW);}
  
}

If you would help me once again I would greatly appreciate it.

How to you have your adrSwitch wired up? The code appears to what an external pulldown resistor. Do you have one installed. A more common approach is to wire one side of the button to ground and the other side of the button to an arduino pin. Then, you have to declare the pin as INPUT_PULLUP to enable the internal pullup resistor. This changes the logic of the button. When pressed, it is shorted to ground and reads LOW. When not pressed, the internal resistor pulls it up to Vcc and it reads HIGH.

blh64:
How to you have your adrSwitch wired up? The code appears to what an external pulldown resistor. Do you have one installed. A more common approach is to wire one side of the button to ground and the other side of the button to an arduino pin. Then, you have to declare the pin as INPUT_PULLUP to enable the internal pullup resistor. This changes the logic of the button. When pressed, it is shorted to ground and reads LOW. When not pressed, the internal resistor pulls it up to Vcc and it reads HIGH.

I have a 10k resistor installed but if I don’t really need it I will use the INPUT_PULLUP.

It isn’t a momentary button, it is an actual on/off switch, not sure if that changes anything.

I’ve looked at the do multiple things at once example and it looks like you create each “separate function” and then call those in the void loop(), but I’m not really sure if I have to use a different variables for the mills in each function or what.

Regards

You have a switch, the switch has 2 terminals, one of the terminals is connected to an input pin, the other terminal is connected to ? How is the 10k resistor connected?

JCA34F:
You have a switch, the switch has 2 terminals, one of the terminals is connected to an input pin, the other terminal is connected to ? How is the 10k resistor connected?

I attached a image of a Fritzing diagram I just created, there are more things connected but they are mostly irrelevant to how the switch is connected.

This is the code that I tried. The switch portion only lights up one light, no matter what. The blocks portion works but the lights are very dim, they flash properly and when I created the blocks() loop without the adr() loop it worked fine, so something that I did with the adr() loop is interfering with the blocks() loop.

// put your setup code here, to run once:
const int joystickInput = A0;
const int proxA = 13;
const int proxB = 12;
const int adrSwitch = 2;
const int adrControl = A1;

int buttonState = LOW;

enum { NEUTRAL, UP, DOWN };
int dir = NEUTRAL;
int state;

unsigned long currentDelay;
unsigned long speedDelay;
unsigned long startTime;

void setup() {

  // designate proxA and proxB as an OUTPUT pin for the LED
  pinMode (proxA, OUTPUT);
  pinMode (proxB, OUTPUT);

  // designate joystickInput as an Analog INPUT for the potentiometer
  pinMode (joystickInput, INPUT);

  Serial.begin(9600);
}

void loop()
{
  blocks();
  adr();
}


void blocks() {

  unsigned long currentTime = millis();

  int potValue = analogRead(joystickInput);
  int outputValue = map(potValue, 64, 912, 100, -100);  //map full up to 100 and full down to -100

  Serial.println(outputValue);

  if (outputValue > 10) {
    //do this when joystick is pressed from the neutral position up
    currentDelay = map(outputValue, 10, 100, 500, 1);  //map the delay progressivly less as joystick is moved from neutral to full up
    if (dir != UP) {
      // first time joystick went up from neutral or from down
      dir = UP;
      state = 0;
      startTime = currentTime;
      digitalWrite(proxA, HIGH);
    }
    else {
      // joystick has been up so see if time to move to next state
      if ( currentTime - startTime >= currentDelay ) {
        state++;
        switch (state) {
          case 1:
            digitalWrite(proxB, HIGH);
            break;
          case 2:
            digitalWrite(proxA, LOW);
            break;
          case 3:
            digitalWrite(proxB, LOW);
            break;
          case 4:
            digitalWrite(proxA, HIGH );
            state = 0;  // last state so roll over to zero
            break;
        }
        startTime = currentTime;
      }
    }
  }
  else if (outputValue < -10) {
    //do this when joystick is pressed from the neutral position down
    currentDelay = map(outputValue, -10, -100, 500, 1);  //map the delay progressivly less as joystick is moved from neutral to full down
    if (dir != DOWN) {
      // first time joystick went down from neutral or from up
      dir = DOWN;
      state = 0;
      startTime = currentTime;
      digitalWrite(proxB, HIGH);
    }
    else {
      // joystick has been up so see if time to move to next state
      if ( currentTime - startTime >= currentDelay ) {
        state++;
        switch (state) {
          case 1:
            digitalWrite(proxA, HIGH);
            break;
          case 2:
            digitalWrite(proxB, LOW);
            break;
          case 3:
            digitalWrite(proxA, LOW);
            break;
          case 4:
            digitalWrite(proxB, HIGH );
            state = 0;  // last state so roll over to zero
            break;
        }
        startTime = currentTime;
      }
    }
  }
  else {
    //do this when joystick is in neutral position
    dir = NEUTRAL;
    digitalWrite (proxB, LOW);
    digitalWrite (proxA, LOW);
  }
}


void adr()
{
  int adrSpeed = analogRead(adrSpeed);
  buttonState = digitalRead(adrSwitch);
  unsigned long adrTime = millis();

  if (buttonState == HIGH)
  {
    speedDelay = map(adrSpeed, 124, 931, 500, 40);

    state = 0;
    startTime = adrTime;
    digitalWrite(proxB, HIGH);

    if ( adrTime - startTime >= speedDelay )
    {
      state++;
        switch (state) 
        {
          case 1:
            digitalWrite(proxA, HIGH);
            break;
          case 2:
            digitalWrite(proxB, LOW);
            break;
          case 3:
            digitalWrite(proxA, LOW);
            break;
          case 4:
            digitalWrite(proxB, HIGH );
            state = 0;  // last state so roll over to zero
            break;
        }   
    }
    startTime = adrTime;
  }
  else
  {
    digitalWrite (proxB, LOW);
    digitalWrite (proxA, LOW);
  }

  
}

I was able to fix the dim lights on the blocks() loop but the adr() loop still only lights the one light.

// put your setup code here, to run once:
const int joystickInput = A0;
const int proxA = 13;
const int proxB = 12;
const int adrSwitch = 2;
const int adrControl = A1;

int buttonState = LOW;

enum { NEUTRAL, UP, DOWN };
enum { ON, OFF};
int dir = NEUTRAL;
int sw = OFF;
int state;

unsigned long currentDelay;
unsigned long speedDelay;
unsigned long startTime;

void setup() {

  // designate proxA and proxB as an OUTPUT pin for the LED
  pinMode (proxA, OUTPUT);
  pinMode (proxB, OUTPUT);

  // designate joystickInput as an Analog INPUT for the potentiometer
  pinMode (joystickInput, INPUT);

  Serial.begin(9600);
}

void loop()
{
  blocks();
  adr();
}


void blocks() {

  unsigned long currentTime = millis();

  int potValue = analogRead(joystickInput);
  int outputValue = map(potValue, 64, 912, 100, -100);  //map full up to 100 and full down to -100

  Serial.println(outputValue);

  if (outputValue > 10) {
    //do this when joystick is pressed from the neutral position up
    currentDelay = map(outputValue, 10, 100, 500, 1);  //map the delay progressivly less as joystick is moved from neutral to full up
    if (dir != UP) {
      // first time joystick went up from neutral or from down
      dir = UP;
      state = 0;
      startTime = currentTime;
      digitalWrite(proxA, HIGH);
    }
    else {
      // joystick has been up so see if time to move to next state
      if ( currentTime - startTime >= currentDelay ) {
        state++;
        switch (state) {
          case 1:
            digitalWrite(proxB, HIGH);
            break;
          case 2:
            digitalWrite(proxA, LOW);
            break;
          case 3:
            digitalWrite(proxB, LOW);
            break;
          case 4:
            digitalWrite(proxA, HIGH );
            state = 0;  // last state so roll over to zero
            break;
        }
        startTime = currentTime;
      }
    }
  }
  else if (outputValue < -10) {
    //do this when joystick is pressed from the neutral position down
    currentDelay = map(outputValue, -10, -100, 500, 1);  //map the delay progressivly less as joystick is moved from neutral to full down
    if (dir != DOWN) {
      // first time joystick went down from neutral or from up
      dir = DOWN;
      state = 0;
      startTime = currentTime;
      digitalWrite(proxB, HIGH);
    }
    else {
      // joystick has been up so see if time to move to next state
      if ( currentTime - startTime >= currentDelay ) {
        state++;
        switch (state) {
          case 1:
            digitalWrite(proxA, HIGH);
            break;
          case 2:
            digitalWrite(proxB, LOW);
            break;
          case 3:
            digitalWrite(proxA, LOW);
            break;
          case 4:
            digitalWrite(proxB, HIGH );
            state = 0;  // last state so roll over to zero
            break;
        }
        startTime = currentTime;
      }
    }
  }
  else {
    //do this when joystick is in neutral position
    dir = NEUTRAL;
    digitalWrite (proxB, LOW);
    digitalWrite (proxA, LOW);
  }
}


void adr()
{
  int adrSpeed = analogRead(adrControl);
  buttonState = digitalRead(adrSwitch);
  unsigned long adrTime = millis();

  if (buttonState == HIGH) 
  {
    speedDelay = map(adrSpeed, 124, 931, 500, 40);  //map the delay progressivly less as joystick is moved from neutral to full up
    if (sw != ON) 
    {
      //first time switch is turned on
      sw = ON;
      state = 0;
      startTime = adrTime;
      digitalWrite(proxB, HIGH);
    }
    else 
    {
      // switch has been on
      if ( adrTime - startTime >= speedDelay ) 
      {
        state++;
        switch (state) 
        {
          case 1:
            digitalWrite(proxA, HIGH);
            break;
          case 2:
            digitalWrite(proxB, LOW);
            break;
          case 3:
            digitalWrite(proxA, LOW);
            break;
          case 4:
            digitalWrite(proxB, HIGH );
            state = 0;  // last state so roll over to zero
            break;
        }
        startTime = adrTime;
      }
      else
      {
        sw = OFF;
        digitalWrite (proxB, LOW);
        digitalWrite (proxA, LOW);
      }
    }
  }

  
  
}

I don’t think you want to call both functions all the time. You need to use the idea in the StateChangeDetection example to only react when the button is pressed or released, not if it is currently pressed.

This might get you closer…

// put your setup code here, to run once:
const int joystickInput = A0;
const int proxA = 13;
const int proxB = 12;
const int adrSwitch = 2;
const int adrControl = A1;

int buttonState = LOW, prevButtonState;

enum { NEUTRAL, UP, DOWN };
enum { ON, OFF};
int dir = NEUTRAL;
int sw = OFF;
int state;

unsigned long currentDelay;
unsigned long speedDelay;
unsigned long startTime;

void setup() {

  // designate proxA and proxB as an OUTPUT pin for the LED
  pinMode (proxA, OUTPUT);
  pinMode (proxB, OUTPUT);

  // designate joystickInput as an Analog INPUT for the potentiometer
  pinMode (joystickInput, INPUT);

  Serial.begin(9600);

  prevButtonState = digitalRead(adrSwitch);
}

void loop()
{
  buttonState = digitalRead(adrSwitch);

  if ( buttonState != prevButtonState ) {
    // switch has changed, react
    if ( buttonState == HIGH ) {
      // button was just pressed
      sw = ON;
      state = 0;
      startTime = millis();
      digitalWrite(proxB, HIGH);
    }
    else {
      // button was just released
      sw = OFF;
      digitalWrite (proxB, LOW);
      digitalWrite (proxA, LOW);
    }
    delay(50); // debounce switch
    prevButtonState = buttonState;
  }

  if ( sw == OFF ) {
    blocks();
  }
  else {
    adr();
  }
}


void blocks() {

  unsigned long currentTime = millis();

  int potValue = analogRead(joystickInput);
  int outputValue = map(potValue, 64, 912, 100, -100);  //map full up to 100 and full down to -100

  Serial.println(outputValue);

  if (outputValue > 10) {
    //do this when joystick is pressed from the neutral position up
    currentDelay = map(outputValue, 10, 100, 500, 1);  //map the delay progressivly less as joystick is moved from neutral to full up
    if (dir != UP) {
      // first time joystick went up from neutral or from down
      dir = UP;
      state = 0;
      startTime = currentTime;
      digitalWrite(proxA, HIGH);
    }
    else {
      // joystick has been up so see if time to move to next state
      if ( currentTime - startTime >= currentDelay ) {
        state++;
        switch (state) {
          case 1:
            digitalWrite(proxB, HIGH);
            break;
          case 2:
            digitalWrite(proxA, LOW);
            break;
          case 3:
            digitalWrite(proxB, LOW);
            break;
          case 4:
            digitalWrite(proxA, HIGH );
            state = 0;  // last state so roll over to zero
            break;
        }
        startTime = currentTime;
      }
    }
  }
  else if (outputValue < -10) {
    //do this when joystick is pressed from the neutral position down
    currentDelay = map(outputValue, -10, -100, 500, 1);  //map the delay progressivly less as joystick is moved from neutral to full down
    if (dir != DOWN) {
      // first time joystick went down from neutral or from up
      dir = DOWN;
      state = 0;
      startTime = currentTime;
      digitalWrite(proxB, HIGH);
    }
    else {
      // joystick has been up so see if time to move to next state
      if ( currentTime - startTime >= currentDelay ) {
        state++;
        switch (state) {
          case 1:
            digitalWrite(proxA, HIGH);
            break;
          case 2:
            digitalWrite(proxB, LOW);
            break;
          case 3:
            digitalWrite(proxA, LOW);
            break;
          case 4:
            digitalWrite(proxB, HIGH );
            state = 0;  // last state so roll over to zero
            break;
        }
        startTime = currentTime;
      }
    }
  }
  else {
    //do this when joystick is in neutral position
    dir = NEUTRAL;
    digitalWrite (proxB, LOW);
    digitalWrite (proxA, LOW);
  }
}


void adr()
{
  int adrSpeed = analogRead(adrControl);
  unsigned long adrTime = millis();

  speedDelay = map(adrSpeed, 124, 931, 500, 40);  //map the delay progressivly less as joystick is moved from neutral to full up
  if ( adrTime - startTime >= speedDelay )
  {
    state++;
    switch (state)
    {
      case 1:
        digitalWrite(proxA, HIGH);
        break;
      case 2:
        digitalWrite(proxB, LOW);
        break;
      case 3:
        digitalWrite(proxA, LOW);
        break;
      case 4:
        digitalWrite(proxB, HIGH );
        state = 0;  // last state so roll over to zero
        break;
    }
    startTime = adrTime;
  }
}

That worked perfectly. One more question, would there be a way to add another switch and potentiometer to control the flashing of a different single light that can run at the same time but independently of the "adrSwitch."

yes. Very similar to the adrSwtich code. Give it a try.

I will do. Thank you.

blh64:
yes. Very similar to the adrSwtich code. Give it a try.

So I tried this by creating separate loops for the ADR and Rotary, they work independently of each other but two things occur, when I use the rotary and turn it off it stays lit at the last light status(high or low) and when I turn on the ADR switch the rotary stops working. The following is the code I wrote, if I should not use the loops independently and then call them in the main loop I’m curious as to why I shouldn’t do it. Also can you maybe point out why it isn’t working. I appreciate all you’ve done so far.

// put your setup code here, to run once:
const int joystickInput = A0;
const int depthProxA = 12;
const int depthProxB = 11;
const int adrSwitch = 2;
const int adrControl = A1;
const int rotarySwitch = 3;
const int rotaryControl = A2;
const int rotaryProx = 10;

int adrButtonState = LOW, adrPrevButtonState;
int rotaryButtonState = LOW, rotaryPrevButtonState;


enum { NEUTRAL, UP, DOWN };
enum { ON, OFF};
int dir = NEUTRAL;
int adrSw = OFF;
int rotarySw = OFF;
int state;

unsigned long currentDelay;
unsigned long adrSpeedDelay;
unsigned long startTime;
unsigned long rotarySpeedDelay;

void setup() {

  // designate depthProxA and depthProxB as an OUTPUT pin for the LED
  pinMode (depthProxA, OUTPUT);
  pinMode (depthProxB, OUTPUT);
  pinMode (rotaryProx, OUTPUT);

  // designate joystickInput as an Analog INPUT for the potentiometer
  pinMode (joystickInput, INPUT);

  Serial.begin(9600);

  adrPrevButtonState = digitalRead(adrSwitch);
  rotaryPrevButtonState = digitalRead(rotarySwitch);
}

void loop()
{
  adrLoop();
  rotaryLoop();
}


void blocks() {

  unsigned long currentTime = millis();

  int potValue = analogRead(joystickInput);
  int outputValue = map(potValue, 64, 912, 100, -100);  //map full up to 100 and full down to -100

  Serial.println(outputValue);

  if (outputValue > 10) {
    //do this when joystick is pressed from the neutral position up
    currentDelay = map(outputValue, 10, 100, 500, 1);  //map the delay progressivly less as joystick is moved from neutral to full up
    if (dir != UP) {
      // first time joystick went up from neutral or from down
      dir = UP;
      state = 0;
      startTime = currentTime;
      digitalWrite(depthProxA, HIGH);
    }
    else {
      // joystick has been up so see if time to move to next state
      if ( currentTime - startTime >= currentDelay ) {
        state++;
        switch (state) {
          case 1:
            digitalWrite(depthProxB, HIGH);
            break;
          case 2:
            digitalWrite(depthProxA, LOW);
            break;
          case 3:
            digitalWrite(depthProxB, LOW);
            break;
          case 4:
            digitalWrite(depthProxA, HIGH );
            state = 0;  // last state so roll over to zero
            break;
        }
        startTime = currentTime;
      }
    }
  }
  else if (outputValue < -10) {
    //do this when joystick is pressed from the neutral position down
    currentDelay = map(outputValue, -10, -100, 500, 1);  //map the delay progressivly less as joystick is moved from neutral to full down
    if (dir != DOWN) {
      // first time joystick went down from neutral or from up
      dir = DOWN;
      state = 0;
      startTime = currentTime;
      digitalWrite(depthProxB, HIGH);
    }
    else {
      // joystick has been up so see if time to move to next state
      if ( currentTime - startTime >= currentDelay ) {
        state++;
        switch (state) {
          case 1:
            digitalWrite(depthProxA, HIGH);
            break;
          case 2:
            digitalWrite(depthProxB, LOW);
            break;
          case 3:
            digitalWrite(depthProxA, LOW);
            break;
          case 4:
            digitalWrite(depthProxB, HIGH );
            state = 0;  // last state so roll over to zero
            break;
        }
        startTime = currentTime;
      }
    }
  }
  else {
    //do this when joystick is in neutral position
    dir = NEUTRAL;
    digitalWrite (depthProxB, LOW);
    digitalWrite (depthProxA, LOW);
  }
}


void adr()
{
  int adrSpeed = analogRead(adrControl);
  unsigned long adrTime = millis();

  adrSpeedDelay = map(adrSpeed, 124, 931, 500, 40);  //map the delay progressivly less as joystick is moved from neutral to full up
  if ( adrTime - startTime >= adrSpeedDelay )
  {
    state++;
    switch (state)
    {
      case 1:
        digitalWrite(depthProxA, HIGH);
        break;
      case 2:
        digitalWrite(depthProxB, LOW);
        break;
      case 3:
        digitalWrite(depthProxA, LOW);
        break;
      case 4:
        digitalWrite(depthProxB, HIGH );
        state = 0;  // last state so roll over to zero
        break;
    }
    startTime = adrTime;
  }
}

void rotary()
{
  int rotarySpeed = analogRead(rotaryControl);
  unsigned long rotaryTime = millis();

  rotarySpeedDelay = map(rotarySpeed, 124, 931, 500, 40);  //map the delay progressivly less as joystick is moved from neutral to full up
  if ( rotaryTime - startTime >= rotarySpeedDelay )
  {
    state++;
    switch (state)
    {
      case 1:
        digitalWrite(rotaryProx, HIGH);
        break;
      case 2:
        digitalWrite(rotaryProx, LOW);
        state = 0;  // last state so roll over to zero
        break;
    }
    startTime = rotaryTime;
  }
}

void adrLoop()
{
  adrButtonState = digitalRead(adrSwitch);
  
  if ( adrButtonState != adrPrevButtonState ) // adrSwitch has changed, react
  {
    if ( adrButtonState == HIGH ) //adrSwitch was just turned on
    {
      adrSw = ON;
      state = 0;
      startTime = millis();
      digitalWrite (depthProxB, HIGH);
    }
    else  //adrSwitch was just turned off
    {
      adrSw = OFF;
      digitalWrite (depthProxB, LOW);
      digitalWrite (depthProxA, LOW);
    }
    delay(50); //debounce adrSwitch
    adrPrevButtonState = adrButtonState;
  }
  
  if ( adrSw == OFF ) 
  {
    blocks();
  }
  else 
  {
    adr();
  }
}

void rotaryLoop()
{
  rotaryButtonState = digitalRead(rotarySwitch);
  
  if ( rotaryButtonState != rotaryPrevButtonState ) // rotarySwitch has changed, react
  {
    if ( rotaryButtonState == HIGH ) //rotarySwitch was just turned on
    {
      rotarySw = ON;
      state = 0;
      startTime = millis();
      digitalWrite (rotaryProx, HIGH);
    }
    else  //adrSwitch was just turned off
    {
      rotarySw = OFF;
      digitalWrite (rotarySw, LOW);
    }
    delay(50); //debounce rotarySwitch
    rotaryPrevButtonState = rotaryButtonState;
  }
  
  if ( rotarySw == OFF ) 
  {
    digitalWrite (rotarySw, LOW);
  }
  else 
  {
    rotary();
  }
}

If the second rotary switch which controls the LED flashing is completely independent, it can not use the same 'state' variable. Those two functions are stomping over each other. The same is true for 'startTime'

Create new variables.

blh64:
If the second rotary switch which controls the LED flashing is completely independent, it can not use the same ‘state’ variable. Those two functions are stomping over each other. The same is true for ‘startTime’

Create new variables.

So basically create an “rotaryState” and “rotaryStartTime” and I should be good?