Push button latching a section of code

Hi
i am trying to get a push button to start a loop of relay operations. i have the push button working but it only runs the relays through 1 cycle. i have another push button set up to toggle a relay on and off, it is working the way i would like it to.
The section of code that is working correctly is the bottom section relating to RearSw.
if some one could have a look and let me know where i may have an issue that would be great.
Thanks in advance

int Relay1 = 12;
int Relay2 = 11;
int Relay3 = 10;
int Relay4 = 9;
int Relay5 = 8;
int Relay6 = 7;
int Relay7 = 6;
int Relay8 = 5;
int D1 = 25;
int D2 = 25;
int strobeSw = A1;
int strobeSwValue;
int strobeSwNew;
int strobeSwOld=1;
int strobeState = 0;
int RearSw = A2;
int RearSwValue;
int Relay7State = 0;
int RearSwNew;
int RearSwOld = 1;
int D3 = 100;
void setup() {
  // put your setup code here, to run once:
  pinMode (Relay1, OUTPUT);
  pinMode (Relay2, OUTPUT);
  pinMode (Relay3, OUTPUT);
  pinMode (Relay4, OUTPUT);
  pinMode (Relay5, OUTPUT);
  pinMode (Relay6, OUTPUT);
  pinMode (Relay7, OUTPUT);
  pinMode (Relay8, OUTPUT);
  pinMode (strobeSw, INPUT);
  digitalWrite(strobeSw, HIGH);
  pinMode (RearSw, INPUT);
  digitalWrite(RearSw, HIGH);
  digitalWrite(Relay1, LOW);
  digitalWrite(Relay2, LOW);
  digitalWrite(Relay3, LOW);
  digitalWrite(Relay4, LOW);
  digitalWrite(Relay5, LOW);
  digitalWrite(Relay6, LOW);
  Serial.begin(9600);
}

void loop() {{
  // put your main code here, to run repeatedly:

  strobeSwNew = digitalRead(strobeSw);
  if (strobeSwOld == 0 && strobeSwNew == 1) {
    if (strobeState == 0)

    {
      digitalWrite(Relay1, HIGH);
      delay(D2);
      digitalWrite(Relay2, HIGH);
      delay(D2);
      digitalWrite(Relay3, HIGH);
      delay(D2);
      digitalWrite(Relay4, HIGH);
      delay(D2);
      digitalWrite(Relay5, HIGH);
      delay(D2);
      digitalWrite(Relay6, HIGH);
      delay(D2);
      digitalWrite(Relay1, LOW);
      delay(D1);
      digitalWrite(Relay2, LOW);
      delay(D1);
      digitalWrite(Relay3, LOW);
      delay(D1);
      digitalWrite(Relay4, LOW);
      delay(D1);
      digitalWrite(Relay5, LOW);
      delay(D1);
      digitalWrite (Relay6, LOW);
      delay(D1);
       strobeState = 1;
  }}
  else {
    digitalWrite(Relay1, LOW);
    digitalWrite(Relay2, LOW);
    digitalWrite(Relay3, LOW);
    digitalWrite(Relay4, LOW);
    digitalWrite(Relay5, LOW);
    digitalWrite(Relay6, LOW);
    strobeState = 0;
  }
  strobeSwOld = strobeSwNew;
  delay (D3);
}
  {


    {
      RearSwNew = digitalRead(RearSw);
      if (RearSwOld == 0 && RearSwNew == 1) {
        if (Relay7State == 0) {
          digitalWrite (Relay7, HIGH);
          Relay7State = 1;
        }
        else {
          digitalWrite (Relay7, LOW);
          Relay7State = 0;
        }
      }
      RearSwOld = RearSwNew;
      delay (D3);
    }
  }
}

consider

#undef MyHW
#ifdef MyHW
byte rlyPins [] = { 10, 11, 12, 13 };

#else
byte rlyPins [] = { 5, 6, 7, 8, 9,10, 11, 12 };
#endif

#define N_RLYS  sizeof(rlyPins)

enum { Off = HIGH, On = LOW };

int D2 = 25;

int strobeSw = A1;
int strobeSwNew;
int strobeSwOld=1;

int rearSw  = A2;
int rearSwNew;
int rearSwOld = 1;

int state;

void setup() {
    Serial.begin(9600);

    for (unsigned n = 0; n < N_RLYS; n++)  {
        digitalWrite (rlyPins [n], Off);
        pinMode      (rlyPins [n], OUTPUT);
    }

    pinMode (strobeSw, INPUT_PULLUP);
    strobeSwOld = digitalRead (strobeSw);

    pinMode (rearSw,   INPUT_PULLUP);
    rearSwOld = digitalRead (rearSw);
}

void loop()
{
    strobeSwNew = digitalRead(strobeSw);
    if (strobeSwOld != strobeSwNew) {
        strobeSwOld = strobeSwNew;
        delay (10);                 // debounce

        if (strobeSwNew == On) {
            if (! state)  {
                for (unsigned n = 0; n < N_RLYS; n++)  {
                    digitalWrite (rlyPins [n], On);
                    delay(D2);
                }
            }
            else {
                for (unsigned n = 0; n < N_RLYS; n++)
                    digitalWrite (rlyPins [n], Off);
            }

            state = ! state;
        }

        Serial.println (strobeSwOld);
    }

    if (state)  {
        rearSwNew = digitalRead(rearSw);
        if (rearSwOld != rearSwNew) {
            rearSwOld = rearSwNew;
            delay (10);             // debounce

            if (rearSwNew == On)
                digitalWrite (rlyPins [1], ! digitalRead (rlyPins [1]));
        }
    }
}

Thanks. i was able to get what i needed.
i have another question regarding toggling somthing on and off.
i am using a bluetooth module and an app called Dabble. i have been able to get it to control the lights, momentary on off. but i am not sure how to read the data coming from the blue tooth device to create the toggle like a button. the code attached is what i have sofar to achieve a momentary light.
the accelerometer stuff at the bottom will go as it is not needed. for the moment i am just trying to get a light to toggle with the up feature.

`/*
   Gamepad module provides three different mode namely Digital, JoyStick and Accerleometer.

   You can reduce the size of library compiled by enabling only those modules that you want to
   use. For this first define CUSTOM_SETTINGS followed by defining INCLUDE_modulename.

   Explore more on: https://thestempedia.com/docs/dabble/game-pad-module/
*/
int Relay1 = 12;
int Relay2 = 11;
int Relay3 = 10;
int Relay4 = 9;
int Relay5 = 8;
int Relay6 = 7;
int Relay7 = 6;
int Relay8 = 5;
int Upval;
int UpNew;
int UpOld = 1;
int Relay1State=0;

#define CUSTOM_SETTINGS
#define INCLUDE_GAMEPAD_MODULE
#include <Dabble.h>
void setup() {
  // put your setup code here, to run once:
  Serial.begin(250000);      // make sure your Serial Monitor is also set at this baud rate.
  Dabble.begin(9600);      //Enter baudrate of your bluetooth.Connect bluetooth on Bluetooth port present on evive.
pinMode (Relay1, OUTPUT);
  pinMode (Relay2, OUTPUT);
  pinMode (Relay3, OUTPUT);
  pinMode (Relay4, OUTPUT);
  pinMode (Relay5, OUTPUT);
  pinMode (Relay6, OUTPUT);
  pinMode (Relay7, OUTPUT);
  pinMode (Relay8, OUTPUT);
   digitalWrite(Relay1, LOW);
  digitalWrite(Relay2, LOW);
  digitalWrite(Relay3, LOW);
  digitalWrite(Relay4, LOW);
  digitalWrite(Relay5, LOW);
  digitalWrite(Relay6, LOW);
  
}

void loop() {
  Dabble.processInput();             //this function is used to refresh data obtained from smartphone.Hence calling this function is mandatory in order to get data properly from your mobile.
  Serial.print("KeyPressed: ");
if (GamePad.isUpPressed()){
      Serial.print("UP");
         digitalWrite(Relay1,HIGH);
      }
      
      
  
    
      
else {digitalWrite (Relay1,LOW);
}   



 
  if (GamePad.isDownPressed())
  {
    Serial.print("DOWN");
  }

  if (GamePad.isLeftPressed())
  {
    Serial.print("Left");
  }

  if (GamePad.isRightPressed())
  {
    Serial.print("Right");
  }

  if (GamePad.isSquarePressed())
  {
    Serial.print("Square");
  }

  if (GamePad.isCirclePressed())
  {
    Serial.print("Circle");
  }

  if (GamePad.isCrossPressed())
  {
    Serial.print("Cross");
  }

  if (GamePad.isTrianglePressed())
  {
    Serial.print("Triangle");
  }

  if (GamePad.isStartPressed())
  {
    Serial.print("Start");
  }

  if (GamePad.isSelectPressed())
  {
    Serial.print("Select");
  }
  Serial.print('\t');

  int a = GamePad.getAngle();
  Serial.print("Angle: ");
  Serial.print(a);
  Serial.print('\t');
  int b = GamePad.getRadius();
  Serial.print("Radius: ");
  Serial.print(b);
  Serial.print('\t');
  float c = GamePad.getXaxisData();
  Serial.print("x_axis: ");
  Serial.print(c);
  Serial.print('\t');
  float d = GamePad.getYaxisData();
  Serial.print("y_axis: ");
  Serial.println(d);
  Serial.println();
}`

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.