How to code a mode selector for multiple linear actuators

I currently have code for 3 actuators that are all controlled by 1 extend button and 1 retract button. I am trying to figure out how to code the actuators so that you can control 1 linear actuator at a time by using a mode selector. For example if the button is pressed once the first actuator is controlled. If the button is pressed 2 times then actuator 2 is controlled. If button is pressed 3 times then actuator 3 will be controlled. then if the button is pressed 4 times all the actuators will be controlled at the same time. I am trying to indicate which mode is activated with individual led lights. For example if mode 1 is selected then the first led will be lit up and so on. If all are activated all lights will lit up.

Below is the current code I have that the actuators are all controlled at once with 1 button.

byte mspeed=0;
int RPWM1 = 8;
int LPWM1 = 11;
int RPWM2 = 5;
int LPWM2 = 6;
int RPWM3 = 12;
int LPWM3 = 13;

void setup() {
  // put your setup code here, to run once:
   Serial.begin(9600);
   pinMode(2, INPUT_PULLUP);
   pinMode(3, INPUT_PULLUP);

   pinMode(RPWM1,OUTPUT);
   pinMode(LPWM1,OUTPUT);
   pinMode(RPWM2,OUTPUT);
   pinMode(LPWM2,OUTPUT);
   pinMode(RPWM3,OUTPUT);
   pinMode(LPWM3,OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if( digitalRead(2) == LOW & digitalRead(3) == HIGH  ){
    mspeed = 225;
    analogWrite(RPWM1,0);
    analogWrite(LPWM1, mspeed);
   
    analogWrite(RPWM2,0);
    analogWrite(LPWM2, mspeed);
    
    analogWrite(RPWM3,0);
    analogWrite(LPWM3, mspeed);
  }
  else if( digitalRead(2) == HIGH & digitalRead(3) == LOW  ){
    mspeed = 225;
    analogWrite(RPWM1,mspeed);
    analogWrite(LPWM1, 0);
    
    analogWrite(RPWM2,mspeed);
    analogWrite(LPWM2, 0);
   
    analogWrite(RPWM3,mspeed);
    analogWrite(LPWM3, 0);
      
  }
  else{
     analogWrite(RPWM1,0);
    analogWrite(LPWM1, 0);
    analogWrite(RPWM2,0);
    analogWrite(LPWM2, 0);
    analogWrite(RPWM3,0);
    analogWrite(LPWM3, 0);
  }
 
}

Always show us a good schematic of your proposed circuit.
Show us a good image of your ‘actual’ wiring.
Give links to components.

We need exact information.

example

  • extend switch is pressed 3 times within 2 seconds, actuator 3 extends and stays extended. LED3 turns ON.

  • extend switch is pressed 1 time within 2 seconds, actuator 1 extends and stays extended. LED1 turns ON.

  • extend switch is pressed 2 times within 2 seconds, actuator 2 extends and stays extended. LED2 turns ON.

  • retract switch is pressed 3 times within 2 seconds, actuator 3 retracts and stays retracted. LED3 turns OFF.

  • etc.

Hi,
Which arduino are you using?
What's connected to these pins?

RPWM1
LPWM1
RPWM2
LPWM2
RPWM3
LPWM3

Hi,
If it is arduinoUNO/Nano/mini, the PWM pins are:
3,5,6,9,10,and 11, so using analogWrite() on non-PWM pins will not work correctly.

int RPWM1 = 8;
int LPWM1 = 11;
int RPWM2 = 5;
int LPWM2 = 6;
int RPWM3 = 12;
int LPWM3 = 13;

analogWrite(RPWM1,0); and others

I don't know if I understood your need well, but see if this code does what you need.

byte mspeed = 0;
int RPWM1 = 3;
int LPWM1 = 5;
int RPWM2 = 6;
int LPWM2 = 9;
int RPWM3 = 10;
int LPWM3 = 11;
int bot1 = 8;
int bot2 = 7;
int count1 = 0;
int count2 = 0;
bool flag = false;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);

  pinMode(RPWM1, OUTPUT);
  pinMode(LPWM1, OUTPUT);
  pinMode(RPWM2, OUTPUT);
  pinMode(LPWM2, OUTPUT);
  pinMode(RPWM3, OUTPUT);
  pinMode(LPWM3, OUTPUT);
}

void loop() {

  if ( digitalRead(bot1) == LOW & digitalRead(bot2) == HIGH  )
  {
    delay(30);
    while ( digitalRead(bot1) == LOW & digitalRead(bot2) == HIGH  )
    {
      flag = true;
    }
    if ( digitalRead(bot1) == HIGH & digitalRead(bot2) == HIGH & flag == true )
    {
      {
        mspeed = 225;
        flag =  false;
        count1++;
        if (count1 > 4)
          count1 = 4;
        Serial.println(count1);
      }
    }
  }
  if ( digitalRead(bot2) == LOW & digitalRead(bot1) == HIGH  )
  {
    delay(30);
    while ( digitalRead(bot2) == LOW & digitalRead(bot1) == HIGH  )
    {
      flag = true;
    }
    if ( digitalRead(bot2) == HIGH & digitalRead(bot1) == HIGH & flag == true )
    {
      {
        mspeed = 225;
        flag =  false;
        count2++;
        if (count2 > 4)
          count2 = 4;
        Serial.println(count2);
      }
    }
  }

  switch (count1) {
    case 1:
      analogWrite(RPWM1, 0);
      analogWrite(LPWM1, mspeed);
      break;

    case 2:
      analogWrite(RPWM2, 0);
      analogWrite(LPWM2, mspeed);
      break;

    case 3:
      analogWrite(RPWM3, 0);
      analogWrite(LPWM3, mspeed);
      break;

    case 4:
      analogWrite(LPWM1, mspeed);
      analogWrite(LPWM2, mspeed);
      analogWrite(LPWM3, mspeed);
      break;

    default:
      break;
  }
  switch (count2) {
    case 1:
      analogWrite(RPWM1, mspeed);
      analogWrite(LPWM1, 0);
      break;

    case 2:
      analogWrite(RPWM2, mspeed);
      analogWrite(LPWM2, 0);
      break;

    case 3:
      analogWrite(RPWM3, mspeed);
      analogWrite(LPWM3, 0);
      break;

    case 4:
      analogWrite(RPWM1, mspeed);
      analogWrite(RPWM2, mspeed);
      analogWrite(RPWM3, mspeed);
      break;

    default:
      break;
  }

}

Your other topic on the same subject deleted.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is frustrating for you and frustrating for us. The people who try to help with your pro…

It will help you get the best out of the forum in the future.

Your OS and version can be valuable information, please include it along with extra security you are using.

Always list the version of the IDE you are using and the board version if applicable.

Use quote or add error messages as an attachment NOT a picture.

How to insert an image into your post. ( Thanks @sterretje )

Add your sketch where applicable but please use CODE TAGS ( </> )

Add a SCHEMATIC were needed even if it is hand drawn

Add working links to any specific hardware as needed (NOT links to similar items)

Remember that the people trying to help cannot see your problem so give as much information as you can

COMMON ISSUES

Ensure you have FULLY inserted the USB cables.

Check you have a COMMON GROUND where required. ( Thanks @Perry)

Where possible use USB 2.0 ports or a USB 2.0 POWERED HUB to rule out USB 3.0 issues.

Try other computers where possible.

Try other USB leads where possible.

You may not have the correct driver installed. CH340/341 or CP2102 or FT232 VCP Drivers - FTDI

There may be a problem with the board check or remove your wiring first.

Remove any items connected to pins 0 and 1.

COMPUTER RELATED

Close any other serial programs before opening the IDE.

Ensure you turn off any additional security / antivirus just to test.

There may be a problem with the PC try RESTARTING it.

You may be selecting the wrong COM port.

Avoid cloud/network based installations where possible OR ensure your Network/Cloud software is RUNNING.

Clear your browsers CACHE.

Close the IDE before using any other serial programs.

Preferably install IDE’s as ADMINISTRATOR or your OS equivalent

ARDUINO SPECIFIC BOARDS

CH340/341 based clones do not report useful information to the “get board info” button.

NANO (Old Types) some require you to use the OLD BOOTLOADER option.

NANO (ALL Types) See the specific sections lower in the forum.

NANO (NEW Types) Install your board CORE’s.

Unless using EXTERNAL PROGRAMMERS please leave the IDE selection at default “AVRISP mkII”.

Boards using a MICRO usb connector need a cable that is both DATA and CHARGE. Many are CHARGE ONLY.

CREATE editor install locations.

On macOs ~/Applications/ArduinoCreateAgent-1.1/ArduinoCreateAgent.app/Contents/MacOS/config.ini

On Linux ~/ArduinoCreateAgent-1.1/config.ini

On Windows C:\Users[your user]\AppData\Roaming\ArduinoCreateAgent-1.1

Performing the above actions may help resolve your problem without further help.
Language problem ?
Try a language closer to your native language:

中文 (Chinese)

Deutsch - German

Español - Spanish

Français - French

অসমীয়া, বাংলা, बड़ो - India

Italiano - Italian

Nederlands - Dutch

Portugues - Portuguese

Россия - Russia

Scandinavia Swedish, Suomi - Finnish, Norsk - Norwegian

Thanks to all those who helped and added to this list.

Thank you.

count1 and count2 going never back and remain 4 vorever