Push Button and SPST switch coding

Hi, I need to make a circuit using arduino Nano with three LEDs connected with D10,11 and 12. Also one Push Button and two SPST button connected with Analog pin A3 as shown in the diagram below.
x
The Switches S1, S2 and S3 connected in such a way that the pin connected with switches will produce 4v, 2v and 0volt with the register network and the analog pin A3 will read all the data.
Now my requirement:

  1. When I press and release the SW1 push button the led effect will do for D1 and D2 as per the flashing effect in the code.

  2. When I press the button SW2 SPST switch, the D1 led will blink as per the code in Flasher13 function. After release the button the D1 will stop immediately and turnoff.

  3. When I press the button SW3 SPST switch, the D2 led will blink as per the code in Flasher13 function. After release the button the D2 will stop immediately and turnoff.

Now my problem:

  1. The led D1 blink continusly even after release the button. But I want to stop it as soon as I release the button. Similiarly the D2 will have to function.

Here is the Code:

const int leftLight = 10;
const int rightLight = 11;
const int buzzerPin = 12;

const int buttonPin = A3;
byte counter;
byte OldCounter = 0;
unsigned long previousMillis = 0;
uint8_t interval = 50;
uint8_t brightness = 0;    // how bright the LED is
uint8_t fadeAmount = 5;

long blinkerButtonTime = 0; // the last time the output pin was toggled
uint8_t debounceDelay = 1000;



struct Sequence {
  byte leftLed;
  byte rightLed;
  byte buzzer;
  uint16_t period;
};
const Sequence s1[] PROGMEM = {
  /* Flasher 2 */
  {HIGH, HIGH, HIGH , 500},
  {LOW,  LOW, LOW , 500},
  /* Flasher 3 */
  {HIGH, LOW,  LOW , 40},
  {LOW,  LOW, LOW ,  40},
  {HIGH, LOW,  LOW , 40},
  {LOW,  LOW, LOW ,  40 + 200},
  {LOW, HIGH, LOW ,  40},
  {LOW,  LOW, LOW ,  40},
  {LOW, HIGH, LOW ,  40},
  {LOW,  LOW, LOW ,  40 + 200},

  // Flasher 4  array 10 to 17
  {HIGH, LOW,  LOW , 50},
  {LOW,  LOW, LOW ,  50},
  {HIGH, LOW, LOW ,  50},
  {LOW,  LOW, LOW ,  1000},
  {LOW, HIGH, LOW ,  50},
  {LOW,  LOW, LOW ,  50},
  {LOW, HIGH, LOW ,  50},
  {LOW,  LOW, LOW ,  1000},

  // Flasher 5 array 18 to 25
  {HIGH, LOW, LOW ,  30},
  {LOW,  LOW, LOW ,  30 + 80},
  {HIGH, LOW, LOW ,  30},
  {LOW,  LOW, LOW ,  30 + 1000},
  {LOW, HIGH, LOW ,  30},
  {LOW,  LOW, LOW ,  30 + 80},
  {LOW, HIGH, LOW ,  30},
  {LOW,  LOW,  LOW , 30 + 1000},

  // Flasher 6 array 26 to 41
  {HIGH, LOW, LOW ,  50},
  {LOW, LOW,  LOW , 50},
  {HIGH, LOW, LOW ,  50},
  {LOW, LOW,  LOW , 50},
  {LOW,  HIGH, LOW ,  50},
  {LOW,  LOW, LOW ,  50},
  {LOW,  HIGH, LOW ,  50},
  {LOW, LOW,  LOW , 1000},
  {LOW,  HIGH, LOW , 50},
  {LOW,  LOW, LOW ,  50},
  {LOW,  HIGH, LOW ,  50},
  {LOW,  LOW, LOW ,  50},
  {HIGH, LOW, LOW ,  50},
  {LOW, LOW, LOW ,  50},
  {HIGH, LOW, LOW ,   50},
  {LOW,  LOW, LOW , 1000},

  // Flasher 7  array 42 to 45
  {HIGH, HIGH, LOW , 50},
  {LOW,  LOW,  LOW , 50},
  {HIGH, HIGH, LOW , 50},
  {LOW,  LOW, LOW ,  500},


  // Flasher 8  array 46 to 55
  {HIGH, LOW, LOW , 50},
  {LOW,  LOW,  LOW , 50},
  {LOW, HIGH, LOW , 100},
  {LOW,  LOW, LOW ,  50},
  {HIGH, LOW, LOW , 50},
  {LOW,  LOW, LOW ,  50},
  {LOW, HIGH, LOW , 100},
  {LOW,  LOW, LOW ,  50},
  {HIGH, LOW, LOW , 50},
  {LOW,  LOW,  LOW , 1500},

  // Flasher 9 array 56 to 67
  {HIGH, LOW, LOW , 50},
  {LOW,  LOW, LOW , 50},
  {HIGH, LOW, LOW , 50},
  {LOW,  LOW, LOW ,  50},
  {HIGH, LOW, LOW , 50},
  {LOW,  LOW, LOW ,  50 + 300},
  {LOW, HIGH, LOW , 50},
  {LOW, LOW,  LOW , 50},
  {LOW, HIGH, LOW , 50},
  {LOW, LOW, LOW ,  50},
  {LOW, HIGH, LOW , 50},
  {LOW, LOW, LOW ,  50 + 300},

  //  Flasher 10array 68 to 74
  {HIGH, LOW, LOW ,  50},
  {LOW,  LOW, LOW ,  0},
  {LOW, HIGH, LOW , 50},
  {LOW,  LOW, LOW ,  2000},
  {LOW, HIGH, LOW , 50},
  {LOW,  LOW, LOW ,   0},
  {HIGH, LOW, LOW , 50},

  // Flasher 11 array 75 to 76
  {HIGH, HIGH, LOW , 50},
  {LOW,  LOW, LOW ,  3000},

  //  Flasher 12array 77 to 86
  {HIGH, LOW, LOW ,  50},
  {LOW, LOW,  LOW , 50},
  {HIGH, LOW, LOW ,  50},
  {LOW, LOW,  LOW , 50},
  {HIGH,  LOW, LOW ,  300},
  {LOW,  HIGH, LOW , 50},
  {LOW,  LOW,  LOW , 50},
  {LOW,  HIGH, LOW ,  50},
  {LOW,  LOW, LOW ,  50},
  {LOW, HIGH,  LOW, 300},

  //  Flasher 13 Left Indicatorarray 87 to 88
  {HIGH, LOW, HIGH, 500},
  {LOW,  LOW, LOW, 500},

  //  Flasher 14 Right Indicator array 89 to 90
  {LOW, HIGH, HIGH,  500},
  {LOW,  LOW, LOW , 500},
  //  Flasher 15 NoFlash array 91 to 91
  {LOW,  LOW, LOW , 0}
};
int step;
unsigned long lastChange;

void Flasher(int first, int last) {
  if (step < first || step > last || millis() - lastChange >= pgm_read_word (&(s1[step].period))) {
    /* move to next step */
    step++;
    if (step < first || step > last) step = first;
    /* Update Leds */
    digitalWrite(leftLight, pgm_read_byte (&(s1[step].leftLed)));
    digitalWrite(rightLight, pgm_read_byte (&(s1[step].rightLed)));
    digitalWrite(buzzerPin, pgm_read_byte (&(s1[step].buzzer)));
    lastChange = millis();
  }
}

void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(leftLight, OUTPUT);
  pinMode(rightLight, OUTPUT);
  pinMode(buzzerPin, OUTPUT);
  Serial.begin(9600);
}

void loop()
{

  OldCounter = counter;
  switch (OldCounter)
  {
    case 1: Flasher1(); Serial.println (OldCounter); break;
    case 2: Flasher(0, 1); Serial.println (OldCounter);  break;
    case 3: Flasher(2, 9);  Serial.println (OldCounter); break;
    case 4: Flasher(10, 17); Serial.println (OldCounter);  break;
    case 5: Flasher(18, 25); Serial.println (OldCounter);  break;
    case 6: Flasher(26, 41); Serial.println (OldCounter);  break;
    case 7: Flasher(42, 45); Serial.println (OldCounter); break;
    case 8: Flasher(46, 55); Serial.println (OldCounter); break;
    case 9: Flasher(56, 67);  Serial.println (OldCounter); break;
    case 10: Flasher(68, 74); Serial.println (OldCounter);  break;
    case 11: Flasher(75, 76); Serial.println (OldCounter);  break;
    case 12: Flasher(77, 86); Serial.println (OldCounter);  break;
    case 13: Flasher(87, 88); Serial.println (OldCounter); break;
    case 14: Flasher(89, 90);  Serial.println (OldCounter);  break;
    case 15: Flasher(91, 91); Serial.println (OldCounter);  break;
    default: Flasher(75, 76); Serial.println (OldCounter);  break;
  }

  int button;

  button = (analogRead(buttonPin) * 5 + 512) / 1024;
 
    
    if      (button == 2 )counter = 13; // SPST SWITCH for left LED D1

    else if (button == 0 )counter = 14; // SPST SWITCH for right LED D2
  

    //for D3 blinker  PUSH button
    if ((millis() - blinkerButtonTime) > debounceDelay)
    {
      if (button == 4 )
      {
        counter++;
        if (counter > 12)counter = 1;
      }
      blinkerButtonTime = millis();

    }
  }

void Flasher1() {

  unsigned long currentMillis = millis();  //grab current time
  analogWrite(leftLight, brightness);
  analogWrite(rightLight, brightness);

  if (currentMillis - previousMillis >= interval) {
    brightness = brightness + fadeAmount; // change the brightness for next time through the loop:
    if (brightness <= 0 || brightness >= 255) //reverse the direction of the fading at the ends of the fade
    {
      fadeAmount = -fadeAmount;
    }
    previousMillis = millis();
  }

}


please read buzzerPin in the code as LED D3.

A solution to the above topic is highly appriciable.

Thanks in Advance.

What is the value of button when no buttons are pressed? You have no code that deals with that situation so when a button is released, nothing changes.

Hello
And a short question:
Why you don´t connect the buttons directly to the NANO?
Have a nice day and enjoy coding in C++.

Hi, I need to run my other pin as other purposes, so i just connected with a3 with such arrangements

And what about the code sir? Remember, button 1 and 2 will b spst switch and 3 will b push button.

Friend, I got the Solution

Here is the revised code:

void loop()
{
  button = (analogRead(buttonPin) * 5 + 512) / 1024;
  if (button == 2 )
  {
    Flasher(87, 88);
    counter = 13;
  }
  else if (button == 0 )
  {
    Flasher(89, 90);
    counter = 13;
  }
  else FlasherEffect();
}

void FlasherEffect() {
  OldCounter = counter;
  switch (OldCounter)
  {
    case 1: Flasher1(); break;
    case 2: Flasher(0, 1);  break;
    case 3: Flasher(2, 9);  break;
    case 4: Flasher(10, 17);  break;
    case 5: Flasher(18, 25);  break;
    case 6: Flasher(26, 41);  break;
    case 7: Flasher(42, 45); break;
    case 8: Flasher(46, 55); break;
    case 9: Flasher(56, 67);  break;
    case 10: Flasher(68, 74);  break;
    case 11: Flasher(75, 76);  break;
    case 12: Flasher(77, 86);  break;
    case 13: Flasher(91, 91);  break;
    default: Flasher(75, 76);  break;
  }
  if ((millis() - blinkerButtonTime) > debounceDelay)
  {
    if (button == 4 )
    {
      counter++;
      if (counter > 12)counter = 1;
    }
    blinkerButtonTime = millis();
  }
}

Thanks all for the kind co-operation