Push Buttom Change State

Hello Everyone.

I've managed to get this far without bothering anyone, however, i am reaching out to the community as I'm out of my depth.

What I have is an Arduino mega 250 with 6 servos that actuate points on a model railroad. They are activated via HIGH or LOW through several toggle switches. I've modified the code so that the servo moves hard against the rail, then move back just enough to stop it buzzing.

What I want to do is use a push button, not toggle switches, so that I can actuate points from different areas in the layout.

Over to you guys. What is the best approach and what would you do. Also, please excuse any programming faux pas as I have no formal training.

#include <Servo.h>
const int switchPin1 = 1; //active low
const int servoPin1 = 7;

const int switchPin2 = 2; //active low
const int servoPin2 = 8;

const int switchPin3 = 3; //active low
const int servoPin3 = 9;

const int switchPin4 = 4; //active low
const int servoPin4 = 10;

const int switchPin5 = 5; //active low
const int servoPin5 = 11;

const int switchPin6 = 6; //active low
const int servoPin6 = 12;

int switchstate1 = 0;         // current state of the button
int previous1 = 0;     // previous state of the button
int runcount1 = 0;           // Count of run cycle. 
int switchstate2 = 0;         // current state of the button
int previous2 = 0;     // previous state of the button
int switchstate3 = 0;         // current state of the button
int previous3 = 0;     // previous state of the button
int switchstate4 = 0;         // current state of the button
int previous4 = 0;     // previous state of the button
int switchstate5 = 0;         // current state of the button
int previous5 = 0;     // previous state of the button

Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
Servo servo6;

void setup() {
  pinMode(switchPin1, INPUT_PULLUP);
  servo1.attach(servoPin1); 

  pinMode(switchPin2, INPUT_PULLUP);
  servo2.attach(servoPin2); 
  
  pinMode(switchPin3, INPUT_PULLUP);
  servo3.attach(servoPin3);

  pinMode(switchPin4, INPUT_PULLUP);
  servo4.attach(servoPin4);

  pinMode(switchPin5, INPUT_PULLUP);
  servo5.attach(servoPin5);
   
  pinMode(switchPin6, INPUT_PULLUP);
  servo6.attach(servoPin6);
  
}

void loop() {
    
switchstate2 = digitalRead(switchPin2);
    if (switchstate2 != previous2) {
        if (switchstate2 == HIGH){
        servo2.write(75);
        delay(200); 
        servo2.write(80);}  // HIGH (switch is off)
    if (switchstate2 == LOW){
        servo2.write(95);
        delay(200); 
        servo2.write(90);}  // HIGH (switch is off)
    //       previous2 = switchstate2;
    }
    
switchstate2 = digitalRead(switchPin2);
    if (switchstate2 != previous2) {
        if (switchstate2 == HIGH){
         servo1.write(95);
        delay(200); 
        servo1.write(90);}  // HIGH (switch is off)
    if (switchstate2 == LOW){
        servo1.write(75);
        delay(200); 
        servo1.write(80);}  // HIGH (switch is off)
        previous2 = switchstate2;
    }

switchstate3 = digitalRead(switchPin3);
    if (switchstate3 != previous3) {
        if (switchstate3 == HIGH){
        servo3.write(94);
        delay(200); 
        servo3.write(89);}  // HIGH (switch is off)
    if (switchstate3 == LOW){
        servo3.write(74);
        delay(200); 
        servo3.write(79);}  // HIGH (switch is off)
        previous3 = switchstate3;
    }
    
switchstate4 = digitalRead(switchPin4);
    if (switchstate4 != previous4) {
        if (switchstate4 == HIGH){
        servo4.write(95);
        delay(200); 
        servo4.write(90);}  // HIGH (switch is off)
    if (switchstate4 == LOW){
        servo4.write(75);
        delay(200); 
        servo4.write(80);}  // HIGH (switch is off)
        previous4 = switchstate4;
    }

switchstate5 = digitalRead(switchPin5);
    if (switchstate5 != previous5) {
        if (switchstate5 == HIGH){
        servo5.write(75);
        delay(200); 
        servo5.write(80);}  // HIGH (switch is off)
    if (switchstate5 == LOW){
        servo5.write(95);
        delay(200); 
        servo5.write(90);}  // HIGH (switch is off)
    //       previous5 = switchstate5;
    }
    
switchstate5 = digitalRead(switchPin5);
    if (switchstate5 != previous5) {
        if (switchstate5 == HIGH){
         servo6.write(95);
        delay(200); 
        servo6.write(90);}  // HIGH (switch is off)
    if (switchstate5 == LOW){
        servo6.write(75);
        delay(200); 
        servo6.write(80);}  // HIGH (switch is off)
        previous5 = switchstate5;
    }

  delay(500);  //delay for debounce
}

Not sure what the question is.... but here are some recommendations

when you repeat something multiple times like you do, it's time to think of a function with parameters or a loop on an array of data

Alternatively you could abstract the whole thing in a class for example you could use the oneButton library if you don't want to have to deal yourself with the buttons, states and bouncing and just attach functions to events like clicks or double clicks

PS:

Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

sorry, I should have been clearer. I'm using a toggle switch that is on/off.

I want to use a momentary switch button switch. So each time it is pressed the server moves from one side to the other.

Certainly consider using arrays to hold data to drastically reduce the code.

As to your question :

What I want to do is use a push button, not toggle switches, so that I can actuate points from different areas in the layout.

It sounds like you want to attach more than one pushbutton switch to each input so that they points can be actuated from any of them. If so you need first to change the program to detect when an input becomes LOW rather than when it is LOW. That way the points can be made to change state when the button becomes pressed and you could have several buttons connected to one input or, given the number of inputs available, each button could have its own input but several could control the same servo.

Look at the StateChangeDetection example in the IDE to see how to do this.

flame_hawk:
I want to use a momentary switch button switch. So each time it is pressed the server moves from one side to the other.

when do you want the action to happen? upon pressing the button or upon releasing the button?

that is:

press --> action, wait until release, press --> action, wait until release etc

or

press, wait until release --> action, press, wait until release --> action, etc

that will influence how you code this

J-M-L:
press --> action, wait until release, press --> action, wait until release etc

if you don't want to create a class and use a simple C like approach to this you could do something like this

You just declare in buttonList your relevant pins

for example in my code below I'm stating that I've buttons on pins 3,4 and 5

button buttonList[] = {
  {[color=red]3[/color], RESTING, 0}, // {pin number, starting State, default chrono value}
  {[color=red]4[/color], RESTING, 0},
  {[color=red]5[/color], RESTING, 0},
};

when you press a button, the pressed() function is called with a parameter representing the index of the button in the array. When you release the button the released() function is called with a parameter representing the index of the button in the array

I've chosen to just print something to the console (which pin is activated or release and what was millis() when pressed and for how long upon release.

this is totally untested.... so more for getting an idea how such a simple state machine could be coded

enum ButtonState : byte {RESTING, ACTIVATED} ;

// all our buttons
struct button {
  byte pinNb; // which pin is connected
  ButtonState state; // this button status
  unsigned long chrono;
};

// HERE YOU DEFINE THE PINS THAT ARE ATTACHED TO A BUTTON (pin <--> button <--> GND)
button buttonList[] = {
  {3, RESTING, 0}, // {pin number, starting State, default chrono value}
  {4, RESTING, 0},
  {5, RESTING, 0},
};

const byte buttonCount = sizeof(buttonList) / sizeof(buttonList[0]);


// ----------------------------------
// action when button n is pressed
// ----------------------------------
void pressed(byte n)
{
  Serial.print(F("Button Pressed Pin: "));
  Serial.print(buttonList[n].pinNb);
  Serial.print(F("\tat Time: "));
  Serial.println(buttonList[n].chrono);
}

// ----------------------------------
// action when button n is released
// ----------------------------------
void released(byte n)
{
  Serial.print(F("Button released Pin:"));
  Serial.print(buttonList[n].pinNb);
  Serial.print(F("\twas pressed during: "));
  Serial.println(buttonList[n].chrono);
}

void setup() {
  Serial.begin(115200);
  for (byte i = 0; i < buttonCount; i++) {
    pinMode(buttonList[i].pinNb, INPUT_PULLUP); // pin --> button --> GND. Will be LOW when pressed
  }
}

void loop() {
  boolean actionTriggered = false; // remember if an action has been triggered to implement a debounce

  // check if needs to trigger actions for all the buttons
  for (byte currentButton = 0; currentButton < buttonCount; currentButton++) {
    switch (buttonList[currentButton].state) {
      case RESTING:
        if (digitalRead(buttonList[currentButton].pinNb) == LOW) {
          // button pressed, trigger the action
          buttonList[currentButton].chrono = millis();
          buttonList[currentButton].state = ACTIVATED;
          pressed(currentButton);
          actionTriggered = true;
        }
        break;
      case ACTIVATED:
        if (digitalRead(buttonList[currentButton].pinNb) == HIGH) {
          // button released
          buttonList[currentButton].chrono = millis() - buttonList[currentButton].chrono;
          buttonList[currentButton].state = RESTING;
          released(currentButton);
          actionTriggered = true;
        }
        break;
    }
  }
  if (actionTriggered) delay(15); // poor's man debounce :)

  // here you can do other stuff



}

hope this helps