Basic help needed on Motorised volume control - Willing to pay

Hi, I’m an engineer but newbie to all this, hoping someone can help with what may be quite a simple problem!

I’ve adapted some code to to control a simple 3 button remote (On/off, Vol up, Vol down).

I can get the On/Off function to work OK, this just controls a 5v relay. No problem.

(Note that I’m using LED’s at the moment to test functionality, but ultimately I’m hoping the Vol up/down signals control an L298N which will control an ALPS motorised pot).

The problem:

When I need to increase the volume the signal switches on an LED, and it stays on until I press the button again. I don’t want this, I want the volume LED to stay on for the duration of the button press, and then turn off when I release the button. At the moment one press means that the volume just keeps ramping up, and I need to press the same button again to make it stop.

Code attached.

I’ve tried various commands, but my old brain has just run out of steam - hoping someone can help me?

New_remote_LED_ver2.1.doc (16.5 KB)

Hello,
You should use a WHILE cycle, something on this lines:

IF the button is pressed {

WHILE the button is pressed { start the motor }

}

hope this helps

Here’s a very simple way to accomplish it. More elegant methods can be done, but this should work.
If you need more detailed help, PM me please.

void  controlMotor(int controlPin, int motorPin, int directionPin, bool upDown)
{
    if (digitalRead(controlPin) == LOW)
    {
        // Set direction
        digitalWrite(directionPin, upDown);
        // Turn on motor
        digitalWrite(motorPin, HIGH);
        while(digitalRead(controlPin) == LOW)
        {
            delay(50);
        }
        digitalWrite(motorPin, LOW);
    }
}

I have used this library in the past for this kind of application:

Does a great job and simple to use.

Thanks for your help guys. All OK now.