Can go up, but how to go down?

Right, I have been trying to edit this code that I have written (with a lot of help from the forum). The thing is that this is used to go incrementally through speeds on an ESC, with the usage of only one button. This all works, however, I can only go up.

I want to add the feature to go down in the speed, with another button. Sadly, I have not been able to do much as I did not have time and there was a lot of python getting mixed in, because of school. The part that is confusing me, is that a certain speed is chosen, by going through the array up, however how do I start at the speed that I was at, at that moment and how do I go down? I thought of making just another array same as the writeValues, just in reverse. But that would create the problem that if I would go down, it would just throw me to the highest value because I am switching to the first value in a different array. How should I approach this?

I will very much appreciate any ideas or opinions! Cheers.

#include <Servo.h>
Servo m1;
const byte upPin = 3;
const byte downPin = 5;
const int (servoPin) = 9;
int writeValues[5] = {1000, 1250, 1500, 1750, 2000};
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;


void setup()
{
  m1.attach(servoPin);
  // initialize the button pin as a input:
  pinMode(upPin,INPUT_PULLUP):
  pinMode(downPin, INPUT_PULLUP);
  // initialize the LED as an output:
  pinMode(servoPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  buttonState = digitalRead(upPin);
  if (buttonState != lastButtonState)
  {
    // if the state has changed, increment the counter
    if (buttonState == LOW)
    {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println(buttonPushCounter);
      Serial.print("buttonPushCounter % 5 = ");
      Serial.println(buttonPushCounter % 5);
      Serial.print("writing ");
      Serial.println(writeValues[buttonPushCounter % 5]);
    }
    else
    {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  lastButtonState = buttonState;
  m1.writeMicroseconds(writeValues[buttonPushCounter % 5]);
}

Have one button count up and the other button, count down (same counter variable).

ok, thanks, but could you help me a bit more?

You only need one array and one buttonPushCounter to use as an index to it

Repeat the code for upPin button with another for the downPin button and if the seond button has become pressed decrement buttonPushCounter instead of incrementing it

You will need separate variables for the current and previous states of the two buttons

Ok huge thanks, I'll do all that and get back if any further problems arise :slight_smile: