Brighten, then pause, then brighten next set.

I hate to bother twice in a row but I'm having difficulty with finding the correct function.

What I want to do is 1) press button, when pressed LED1 brightens to full. PAUSE for a second or two, then 2) LED 2 brightens to full.
Right now I can get both to go on at the same time. I tried writing a delay in but it only causes the program to run slowly.

Pseudocode statement, unsure of syntax. Such as:

if J == 255
then
int J=0;
for (int j = 0; j <= fadeThreshold; j++)
analogWrite(led3,j)

The only problem is if I try something like that, it wipes out LED2 so do I need a new variable for this or is there a statement that would work?

if (buttonState2 == HIGH) {     
    // if the current state is LOW then the button has been pressed
    Serial.println("pressed 2");
 
    for (int j = 0; j <= fadeThreshold; j++) {
      //update led with new analog value (brightness)    
      analogWrite(led2, j);
      // HERE IS WHERE I WANT IT TO WAIT UNTIL led2 IS AT 255
      analogWrite(led3, j);
     // analogWrite(led4, HIGH);
      delay(fadeSpeed);
    }

    //jump to 255 (fully on)
   // analogWrite(led2, 255);
   // analogWrite(led3, 255);
 }

I looked in the examples and I saw a conditional which I think I might need. I tried adding below the //HERE note. Threshold was defined as 255

      counterbright ++;
      if (counterbright >threshold)
      Serial.println(counterbright, "hi");

to see if I could make it wait but...that failed. counterbright never increments.

Use CTRL T to format your code. Attach your ‘COMPLETE’ sketch between code tags, use the </> icon in the posting menu. [code]Paste your sketch here[/code]

Here is the whole code. Everything is working as I want except button 2, which is why I only posted it. I mean it does work, just there is no pause, everything brightens at once. There are abandoned variables probably but I cleaned it up a bit...

// this constant won't change:
const int  buttonPin = 2;    // connected to GND & D2
const int buttonPin2 = 4; //Added 4-12-2020
const int buttonPin3 = 7; //Added 4/18/2020
const int threshold = 255;
unsigned long interval = 500;
unsigned long previousMillis = 0;
const unsigned long intervalSlow = 250;
const unsigned long intervalFast = 750;

// Variables will change:
int buttonState = 0;         // current state of the 1st Button
int buttonState2 = 0;// current state of 2nd button
int buttonState3 = 0; //current state of 3rd button (4/18/2020)
int led = 9;           // the PWM pin the LED is attached to
int led2 = 10; //Added 4-12-2020
int led3 = 11;
int led4 = 6;
int fadeThreshold = 100; //127 originally
int fadeSpeed = 10;
int ledState = 150; //LOW
int flashstate = 255; //HIGH
int counter = 0;
int counterbright = 0;

void setup() {
  // initialize the button pin as a input:
  //pinMode(buttonPin, INPUT);
  //digitalWrite(buttonPin, HIGH);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP); //added 4/18/2020

  // initialize the LED as an output:
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT); //added 4-12-2020
  pinMode(led3, OUTPUT); //added 4/18/2020
  pinMode(led4, OUTPUT); //added 4/18/2020

  // initialize serial communication:
  Serial.begin(9600);
  counter = 0;
  counterbright = 0;
  interval = intervalSlow;
  Serial.println(counter);
  Serial.println(interval);

}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

  unsigned long currentMillis = millis();
  Serial.println(counter);
  delay(100);
  analogWrite(led2, 255);
  analogWrite(led3, 255);

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
      counter ++;
    }
    digitalWrite(led4, ledState);
  }

  if (counter == 2) {
    //reset counter and swap interval
    counter = 0;
    if ( interval == intervalSlow) {
      interval = intervalFast;
    }
    else {
      interval = intervalSlow;

    }

  }

  // if the state has changed, increment the counter
  if (buttonState == HIGH) {
    // if the current state is LOW then the button has been pressed
    Serial.println("pressed");

    for (int i = 0; i <= fadeThreshold; i++) {
      //update led with new analog value (brightness)
      analogWrite(led, i);
      analogWrite(led2, 255); //added 4-12-2020
      delay(fadeSpeed);
    }

    //jump to 255 (fully on)
    analogWrite(led, 255);

  } else {
    analogWrite(led, 0);
    //do nothing
  }

  // Delay a little bit to avoid bouncing
  delay(100);

  if (buttonState2 == HIGH) {
    // if the current state is LOW then the button has been pressed
    Serial.println("pressed 2");

    for (int j = 0; j <= fadeThreshold; j++) {
      //update led with new analog value (brightness)
      analogWrite(led2, j);
      counterbright ++;
      if (counterbright > threshold)
        Serial.println(counterbright, "hi");
      // HERE IS WHERE I WANT IT TO WAIT UNTIL led2 IS AT 255
      analogWrite(led3, j);
      analogWrite(led4, HIGH);
      delay(fadeSpeed);
    }
  }

  
  if (buttonState3 == HIGH) {
    // if the current state is LOW then the button has been pressed
    Serial.println("pressed 3");

    for (int k = 0; k <= 500; k++) {
      Serial.println(k);
      analogWrite(led2, random(0, 100));
      delay(random(50));
      analogWrite(led3, random(0, 100));
      delay(random(50));
      analogWrite(led4, random(0, 100));
      delay(random(50));
      if ( k == 250) {
        analogWrite (led2, 255);
        analogWrite (led3, 255);

      }
    }
  }
}

Intermixing BWD and delay() is a very bad idea.

You should look at the “State Change Detection (Edge Detection)” example in the IDE. This is superior than looking at a switch state.

Have you heard of the “State Machine” programming technique ?

“ What I want to do is 1) press button, when pressed LED1 brightens to full. PAUSE for a second or two, then 2) LED 2 brightens to full.”

Sketch to be tested:

//Version 1.02

#define PUSHED                   LOW

const byte buttonPin1            = 2;    //connected to GND & D2
const byte buttonPin2            = 4;
const byte buttonPin3            = 7;

const byte led1                  = 9;    //PWM pin the LED is attached to
const byte led2                  = 10;   //PWM pin the LED is attached to
const byte led3                  = 11;   //PWM pin the LED is attached to
const byte led4                  = 6;

const byte threshold             = 255;

const unsigned long intervalSlow = 250;
const unsigned long intervalFast = 750;
unsigned long interval           = intervalSlow;

unsigned long switchMillis;
unsigned long currentMillis;
unsigned long previousMillis;
unsigned long fadeMillis;

byte buttonState1;
byte lastButtonState1;

byte buttonState2;
byte lastButtonState2;

byte buttonState3;
byte lastButtonState3;

unsigned int fadeCounter;
unsigned int fadeThreshold      = 255;
unsigned int fadeSpeed          = 10;
unsigned int ledState           = 0;
unsigned int counter            = 0;

enum STATES {STARTUP, STATE1, STATE2, STATE3};
STATES mState = STARTUP;


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

  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);

} //END of setup()

//****************************************************************
void loop()
{
  //save the current Arduino time
  currentMillis = millis();

  //****************************************
  checkSwitches();

  //****************************************
  stateMachine();

  //****************************************
  //toggle led4 stuff
  if (currentMillis - previousMillis >= interval)
  {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
    {
      ledState = HIGH;
    }
    else
    {
      ledState = LOW;
      counter ++;
    }
    digitalWrite(led4, ledState);
  }

  if (counter == 2)
  {
    //reset counter and swap interval
    counter = 0;
    if ( interval == intervalSlow)
    {
      interval = intervalFast;
    }

    else
    {
      interval = intervalSlow;
    }
  }

  //****************************************
  // Other non blocking code goes here
  //****************************************


} //END of loop()

//****************************************************************
void stateMachine()
{
  switch (mState)
  {
    //**************
    case STARTUP:
      {

      }
      break;

    //**************
    case STATE1:
      {
        //time to fade LED1 ?
        if (currentMillis - fadeMillis >= fadeSpeed )
        {
          //restart the TIMER
          fadeMillis = currentMillis;


          if (fadeCounter <= fadeThreshold)
          {
            //update led with new analog value (brightness)
            analogWrite(led1, fadeCounter);
            fadeCounter++;
          }

          else
          {
            mState = STATE2;

            //restart the TIMER
            fadeMillis = currentMillis;
          }

        }
      }
      break;

    //**************
    case STATE2:
      {
        //wait here for 2 seconds
        if (currentMillis - fadeMillis >= 2000ul)
        {
          mState = STATE3;
          
          fadeCounter = 0;

          //restart the TIMER
          fadeMillis = currentMillis;
        }
      }
      break;

    //**************
    case STATE3:
      {
        //time to fade LED2 ?
        if (currentMillis - fadeMillis >= fadeSpeed )
        {
          //restart the TIMER
          fadeMillis = currentMillis;

          if (fadeCounter <= fadeThreshold)
          {
            //update led with new analog value (brightness)
            analogWrite(led2, fadeCounter);
            fadeCounter++;
          }
        }
      }
      break;

      //**************


  } //END of switch/case

} //END of stateMachine()


//****************************************************************
void checkSwitches()
{
  //is it time to look at the switches ?
  if (currentMillis - switchMillis < 50)
  {
    //not time yet
    return;
  }

  //restart the TIMER
  switchMillis = currentMillis;

  //*******************************                  b u t t o n P i n 1
  buttonState1 = digitalRead(buttonPin1);

  //switch changed state ?
  if (lastButtonState1 != buttonState1)
  {
    //update to the new state
    lastButtonState1 = buttonState1;

    if (buttonState1 == PUSHED)
    {
      //if the current state is LOW then the button has been pressed
      Serial.println("pressed");

      //enter STATE1
      mState = STATE1;

      //start 'led' at intensity of 0
      fadeCounter = 0;

      //restart the TIMER
      fadeMillis = currentMillis;
    }

    else
    {
      mState = STARTUP;

      //turn off leds when the 'buttonPin1' is released
      analogWrite(led1, 0);
      analogWrite(led2, 0);
    }

  } //END of this switch

  //*******************************

} //END of checkSwitches()

I will test this sketch tomorrow when I work on my models again. Will let you know how it runs. And I vaguely remember state machines from college but we are going back 16-20 years in C++. Plus I was bad at programming except in COBOL.

I am jamstraz. Somehow my profile is doubled and I don't know why.

See below for Arduino sketch

I vaguely remember state machines from college

State Machines are made out to be far more mysterious than they really are.

It's really just a variable like any other variable that happens to have "state" in the title more often than not, like currentState or systemState or whatever. If the value of that variable is 0 (where it would probably be at power up) do certain things and those things only. That means for example only waiting for a press of button 'A' to start a process, even if someone is clicking button 'B' like a maniac. Pressing A in state 0 might initiate certain things, and once those things are done, it will set the variable to 1, say.

In state 1, button A might still be valid, but this time it would initiate a shut down and return to state 0. Button B in state 1 might also be valid now, and it would do some stuff and set the state to 2. State 2 might be the only one where the temperature is read and if the temperature is too high turn on the fan but stay in the same state. Button C might be the only way out of state 2... unless it's a Thursday during full moon, in which case you have to press button D.

You can sometimes manage states with if(), but it can get a but cumbersome, and switch...case may make more sense.

This might help.

Disturbingly my code works but its FAR less elegant than what larryd posted. Honestly its comparing his Astin Martin to my Ford Pinto with duct tape.

//Starship Base Code
//Based on a myriad of sources, I take very little credit
//Ver 2.0b 12 April 2020
//Ver 3.0a 18 April 2020

//Torpedo pin numbers
const int  buttonPin = 2;  //One press to brighten, flash and turn off
//Power up sequence pin numbers
const int buttonPin2 = 4; // Press to power up the model
//Damage flicker button
const int buttonPin3 = 7; //random light flickers

const int threshold = 255;
unsigned long interval = 500;
unsigned long previousMillis = 0;
const unsigned long intervalSlow = 250;
const unsigned long intervalFast = 750;


//Set up variables for Torpedo
int buttonState = 0;         // current state of the 1st Button
int led = 9;                 // PWM for Torpedo

//Set up variables for startup sequence
int buttonState2 = 0;        // current state of 2nd button
int led2 = 10;               //PWM for interior lights
int led3 = 11;               //PWM for engines
int fadeThreshold = 100; //127 originally
int fadeSpeed = 10;

//Setup variables for damage
int buttonState3 = 0; //PWM for damage
int led4 = 6;
int ledState = 150; //LOW
int flashstate = 255; //HIGH
int counter = 0;
int counterbright = 0;

void setup() {
  // initialize the button pin as a input:
  //pinMode(buttonPin, INPUT);
  //digitalWrite(buttonPin, HIGH);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP); //added 4/18/2020

  // initialize the LED as an output:
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT); //added 4-12-2020
  pinMode(led3, OUTPUT); //added 4/18/2020
  pinMode(led4, OUTPUT);

  // initialize serial communication:
  Serial.begin(9600);
  counter = 0;
  counterbright = 0;
  interval = intervalSlow;
  Serial.println(counter);
  Serial.println(interval);

}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);

  unsigned long currentMillis = millis();
  Serial.println(counter);
  delay(100);
  //analogWrite(led2, 255);
  //analogWrite(led3, 255);

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
      counter ++;
    }
    digitalWrite(led4, ledState);
  }

  if (counter == 2) {
    //reset counter and swap interval
    counter = 0;
    if ( interval == intervalSlow) {
      interval = intervalFast;
    }
    else {
      interval = intervalSlow;

    }

  }

  // if the state has changed, increment the counter
  if (buttonState == HIGH) {
    // if the current state is LOW then the button has been pressed
    Serial.println("pressed");

    for (int i = 0; i <= fadeThreshold; i++) {
      //update led with new analog value (brightness)
      analogWrite(led, i);
      delay(fadeSpeed);
    }

    //jump to 255 (fully on)
    analogWrite(led, 255);

  } else {
    analogWrite(led, 0);
    //do nothing
  }

  // Delay a little bit to avoid bouncing
  delay(100);

  if (buttonState2 == HIGH) {
    analogWrite (led3, 0);
    analogWrite (led2, 0);
    delay (500);
    // if the current state is LOW then the button has been pressed

    for (int j = 0; j <= fadeThreshold; j++) {
      //update led with new analog value (brightness)
      analogWrite(led2, j);
      delay(fadeSpeed);
    }
    for (int l = 0; l <= fadeThreshold; l++) {
      analogWrite(led3, l);
      delay(fadeSpeed);
    }
    analogWrite(led4, HIGH);
    delay(fadeSpeed);
  }


  if (buttonState3 == HIGH) {
    // if the current state is LOW then the button has been pressed
    Serial.println("pressed 3");

    for (int k = 0; k <= 100; k++) {
      Serial.println(k);
      analogWrite(led2, random(0, 400));
      delay(random(50));
      analogWrite(led3, random(0, 400));
      delay(random(50));
      analogWrite(led4, random(0, 400));
      delay(random(50));
      if ( k == 50) {
        analogWrite (led2, HIGH);
        analogWrite (led3, HIGH);

      }
    }
  }
}

Your switches are wired so a closed gives a HIGH on your input pin.

Change
#define PUSHED LOW
To
#define PUSHED HIGH


LEDs require current limiting, add a series 220R resistor to every LED.


FYI


You show pin 5 as led4 but the code uses 6.


The 5v wire is going to GND !

I uploaded it but it killed the first button's function.

Button 1: Brighten LED1 to about 200, then flash to 255, then off.
Button 2: Brighten LED2, wait, Brighten LED3.
Button 3: Random flickering of LED2, 3 and 4.

LED 4 runs the fast blink, then slow.

per pins, I made this last night and was tired and failed to notice that. 6 is correct, 5 is not. I corrected it on my personal file.

I have 220R that came with my LEDs I will use but it dims them too much. Hoping the Darlington Arrays to power them without the Arduino and just control them that way will eliminate. I just wanted to make sure what I want to do is doable for a guy who's had an Arduino for 8 days :stuck_out_tongue:

larryd:
Your switches are wired so a closed gives a HIGH on your input pin.

Change
#define PUSHED LOW
To
#define PUSHED HIGH


LEDs require current limiting, add a series 220R resistor to every LED.


FYI


Your show pin 5 as led4 but the code uses 6.


The 5v wire is going to GND !

This is what happens when I do this in low light and document it lol. Pin 6 is correct, not 5 as I had it in the sketch. 5v is correct, not as ground where I had it in the sketch

" What I want to do is 1) press button, when pressed LED1 brightens to full. PAUSE for a second or two, then 2) LED 2 brightens to full."

The above was your criteria.

The code offered to you works as follows:

Push and hold buttonPin1 (pin 2).

led1 goes from 0-255, keep holding buttonPin1, 2 seconds delay, keep holding buttonPin1, led2 goes from 0-255, release buttonPin1, leds go off.

Does this not meet your criteria ?

led4 is your code which was left as is.

Ok I will implement that into button 2 and try to deconstruct and understand what you wrote to translate my mess into something resembling the state machine. Thank you for your assistance :slight_smile:

Note: You are not limited in the number of ‘State Machines’ you can have, i.e. you can have one for each switch etc.


Use switch edge detection, not switch levels.


Always always always write your sketch using non blocking code.


Do not use delay() in your sketch unless you know the ramifications of doing so.


Use functions.

You wouldn't happen to know of any books or sources I can learn from? Any you recommend? I am basically cobbling together from other code and examples in the IDE.

Arduino links of interest.


How to use this forum:
https://forum.arduino.cc/index.php?topic=149014.0


Listing of downloadable 'Arduino PDFs' :
Either Google >>>- - - - > arduino filetype: pdf
Or
https://www.google.ca/search?q=arduino+filetype%3A+pdf&rlz=1C9BKJA_enCA739CA739&oq=arduino+filetype%3A+pdf&aqs=chrome..69i57j69i65.1385j0j7&hl=en-US&sourceid=chrome-mobile&ie=UTF-8


Listing of downloadable 'C++ PDFs' :
Either Google >>>- - - - > C++ filetype: pdf
Or
https://www.google.ca/search?q=c%2B%2B+filetype%3A+pdf&rlz=1C9BKJA_enCA739CA739&oq=c%2B%2B+filetype%3A+pdf&aqs=chrome..69i57.22790j0j7&hl=en-US&sourceid=chrome-mobile&ie=UTF-8


Arduino cheat sheet:


Watch these:
Arduino programming syntax:

Arduino arithmetic operators:

Arduino control flow:

Arduino data types:

Jeremy Blume:

Sparkfun External Interrupts

Sparkfun Timer1 Interrupts

Powering Your Projects


Understanding Destructive LC Voltage Spikes:

OR

Why MOSFET gate resistors:


Some things to read

LCD information:

OR

Reading a schematic:
https://learn.sparkfun.com/tutorials/how-to-read-a-schematic

Language Reference:

Foundations:


How and Why to avoid delay():
http://playground.arduino.cc/Code/AvoidDelay

Demonstration code for several things at the same time.
http://forum.arduino.cc/index.php?topic=223286.0


Multitasking:
Part 1:

Part 2:

Part 3:


Neopixels, Adafruit

Fastled


Sparkfun Tutorials:
https://learn.sparkfun.com/tutorials?page=all

Micro Controllers:

Useful links:
https://forum.arduino.cc/index.php?topic=384198.0

Arduino programming traps, tips and style guide:

Call for useful programming discussions
https://forum.arduino.cc/index.php?topic=383980.0

Arduino products:

Motors/MOSFETs

Switches:

A good book you might want to get:


Share tips you have come across, 500+ posts:
https://forum.arduino.cc/index.php?topic=445951.0


Debug discussion:
https://forum.arduino.cc/index.php?topic=215334.msg1575801#msg1575801

Frequently Asked Questions:
https://www.arduino.cc/en/main/FAQ#toc10


Number 'type's:

  • boolean (8 bit) - simple logical true/false, Arduino does not use single bits for bool
  • byte (8 bit) - unsigned number from 0-255
  • char (8 bit) - signed number from -128 to 127. The compiler will attempt to interpret this data type as a character in some circumstances, which may yield unexpected results
  • unsigned char (8 bit) - same as 'byte'; if this is what you're after, you should use 'byte' instead, for reasons of clarity
  • word (16 bit) - unsigned number from 0-65535
  • unsigned int (16 bit)- the same as 'word'. Use 'word' instead for clarity and brevity
  • int (16 bit) - signed number from -32768 to 32767. This is most commonly what you see used for general purpose variables in Arduino example code provided with the IDE
  • unsigned long (32 bit) - unsigned number from 0-4,294,967,295. The most common usage of this is to store the result of the millis() function, which returns the number of milliseconds the current code has been running
  • long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647
  • float (32 bit) - signed number from -3.4028235E38 to 3.4028235E38. Floating point on the Arduino is not native; the compiler has to jump through hoops to make it work. If you can avoid it, you should.

You should always select the 'data type' best suited for your variables.
ex:

  • your variable does not change and it defines a pin on the Arduino. const byte limitSwitchPin = 34;
  • since an analog variable can be 0 to 1023, a byte will not do, you can select 'int'. ex: int temperature;
  • if your variable needs to be within -64 to +64 a 'char' will do nicely. ex: char joystick;
  • if your variable is used for ASCII then you need type 'char', ex: char myText[] = {"Raspberry Pie Smells"};
  • if your variable enables some code then boolean can be used. ex: boolean enableFlag = false;
  • millis() returns the time in ms since rebooting, ex: unsigned long currentTime = millis();
    etc.

Oh, and have fun too :slight_smile: !

mind boggled at the amount of posts

...I'm struggling with trying to understand the code. Its my fault but I'm trying to translate my code to the state machine approach and it's not going well. The Button1 sequence should be...somewhat easily translated once I can write it out. (Button1 brightens an LED to around 200, flashes at 255 and then goes off). Button 3 is a whole other matter. I need to figure out how to make the LEDs randomly flash and flicker for like 7-10 seconds and then brighten back up to HIGH. In my bad code I did it with counters, random() and delays().

So this is where I'm flailing. Button 1. it turns the LED on all right, but no flash, no off and I can't figure out why.

//Version 1.02

#define PUSHED                   LOW

const byte buttonPin1            = 2;    //connected to GND & D2
const byte buttonPin2            = 4;
const byte buttonPin3            = 7;

const byte led1                  = 9;    //PWM pin the LED is attached to
const byte led2                  = 10;   //PWM pin the LED is attached to
const byte led3                  = 11;   //PWM pin the LED is attached to
const byte led4                  = 6;

const byte threshold             = 255;

const unsigned long intervalSlow = 250;
const unsigned long intervalFast = 750;
unsigned long interval           = intervalSlow;

unsigned long switchMillis;
unsigned long currentMillis;
unsigned long previousMillis;
unsigned long fadeMillis;

byte buttonState1;
byte lastButtonState1;

byte buttonState2;
byte lastButtonState2;

byte buttonState3;
byte lastButtonState3;

unsigned int fadeCounter;
unsigned int fadeThreshold      = 255;
unsigned int fadeSpeed          = 10;
unsigned int ledState           = 0;
unsigned int counter            = 0;

enum STATESTORP {TORPSEQUENCE, WARMUP, FLASHON, ENDSTATE};
STATESTORP mState1 = TORPSEQUENCE;

enum STATES {STARTUP, STATE1, STATE2, STATE3};
STATES mState2 = STARTUP;


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

  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);

} //END of setup()

//****************************************************************
void loop()
{
  //save the current Arduino time
  currentMillis = millis();

  //****************************************
  checkSwitches();

  //****************************************
  stateMachineStartup();
  stateMachineTorp();

  //****************************************
  //toggle led4 stuff
  if (currentMillis - previousMillis >= interval)
  {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
    {
      ledState = HIGH;
    }
    else
    {
      ledState = LOW;
      counter ++;
    }
    digitalWrite(led4, ledState);
  }

  if (counter == 2)
  {
    //reset counter and swap interval
    counter = 0;
    if ( interval == intervalSlow)
    {
      interval = intervalFast;
    }

    else
    {
      interval = intervalSlow;
    }
  }

  //****************************************
  // Other non blocking code goes here
  //****************************************


} //END of loop()

//****************************************************************
void stateMachineTorp()
{
  switch (mState1)
  {
    //**************
    case TORPSEQUENCE:
      {

      }
      break;

    //**************
    case WARMUP:
      {
        //time to fade LED1 ?
        if (currentMillis - fadeMillis >= fadeSpeed )
        {
          //restart the TIMER
          fadeMillis = currentMillis;


          if (fadeCounter <= fadeThreshold)
          {
            //update led with new analog value (brightness)
            analogWrite(led1, fadeCounter);
            fadeCounter++;
          }

          else
          {
            mState1 = FLASHON;

            //restart the TIMER
            fadeMillis = currentMillis;
          }

        }
      }
      break;

    //**************
    case FLASHON:
      {
        //wait here for 1 seconds
        if (currentMillis - fadeMillis >= 1000ul)
        {
          mState1 = ENDSTATE;
          analogWrite(led1, 255);
          fadeCounter = 0;

          //restart the TIMER
          fadeMillis = currentMillis;
        }
      }
      break;

    //**************
    case ENDSTATE:
      {
        {
          analogWrite(led1, 0);

        }
      }

      break;

      //**************


  } //END of switch/case
}

//****************************************************************
void stateMachineStartup()
{
  switch (mState2)
  {
    //**************
    case STARTUP:
      {

      }
      break;

    //**************
    case STATE1:
      {
        //time to fade LED2 ?
        if (currentMillis - fadeMillis >= fadeSpeed )
        {
          //restart the TIMER
          fadeMillis = currentMillis;


          if (fadeCounter <= fadeThreshold)
          {
            //update led with new analog value (brightness)
            analogWrite(led2, fadeCounter);
            fadeCounter++;
          }

          else
          {
            mState2 = STATE2;

            //restart the TIMER
            fadeMillis = currentMillis;
          }

        }
      }
      break;

    //**************
    case STATE2:
      {
        //wait here for 2 seconds
        if (currentMillis - fadeMillis >= 2000ul)
        {
          mState2 = STATE3;

          fadeCounter = 0;

          //restart the TIMER
          fadeMillis = currentMillis;
        }
      }
      break;

    //**************
    case STATE3:
      {
        //time to fade LED3 ?
        if (currentMillis - fadeMillis >= fadeSpeed )
        {
          //restart the TIMER
          fadeMillis = currentMillis;

          if (fadeCounter <= fadeThreshold)
          {
            //update led with new analog value (brightness)
            analogWrite(led3, fadeCounter);
            fadeCounter++;
          }
        }
      }
      break;

      //**************


  } //END of switch/case

} //END of stateMachine()


//****************************************************************
void checkSwitches()
{
  //is it time to look at the switches ?
  if (currentMillis - switchMillis < 50)
  {
    //not time yet
    return;
  }

  //restart the TIMER
  switchMillis = currentMillis;

  //*******************************                  b u t t o n P i n 1
  buttonState1 = digitalRead(buttonPin1);
  //switch changed state ?
  if (lastButtonState1 != buttonState1)
  {
    //update to the new state
    lastButtonState1 = buttonState1;

    if (buttonState1 == PUSHED)
    {
      //if the current state is LOW then the button has been pressed
      Serial.println("pressed 1");
    }
    //Enter Warmup
    mState1 = WARMUP;

    //start 'led' at intensity of 0
    //fadecounter = 0;

    //restart the TIMER
    fadeMillis = currentMillis;
  }

  else
  {
    mState1 = TORPSEQUENCE;
  }

  //*******************************                  b u t t o n P i n 2
  buttonState2 = digitalRead(buttonPin2);

  //switch changed state ?
  if (lastButtonState2 != buttonState2)
  {
    //update to the new state
    lastButtonState2 = buttonState2;

    if (buttonState2 == PUSHED)
    {
      //if the current state is LOW then the button has been pressed
      Serial.println("pressed 2");

      //enter STATE1
      mState2 = STATE1;

      //start 'led' at intensity of 0
      fadeCounter = 0;

      //restart the TIMER
      fadeMillis = currentMillis;
    }

    else
    {
      mState2 = STARTUP;

      //turn off leds when the 'buttonPin2' is released
      analogWrite(led2, 0);
      analogWrite(led3, 0);
    }

  } //END of this switch

  //*******************************

} //END of checkSwitches()

if (lastButtonState1 != buttonState1)
{
. . .
}
The above code checks to see if there was a change in state with buttonPin1, LOW to HIGH OR HIGH to LOW.

If there was change is state you then execute the code within the { & } braces


You have the the code below executing all the time when there is no change in state.

Is this what you intended ?
else
{
mState1 = TORPSEQUENCE;
}


"(Button1 brightens an LED to around 200, flashes at 255 and then goes off)"

You want led1 to start at 0 then fade up to 200 then wait 1 second ?

What does this mean ? flashes at 255


Do you want to lock out switch 2 and 3 when switch button1 is being held ?
Do you want to lock out switch 1 and 3 when switch button2 is being held ?
Do you want to lock out switch 1 and 2 when switch button3 is being held ?

No, that's not what I intend, so that's probably why I'm struggling. Button 1 is a press once and it's over button. When button 1 is pressed led1 is supposed to brighten to 200 and in an example
analogWrite (led1, 200)

but in this case a counter is pushing it to 200 for it's brightness. Once it reaches that number I want it to flash at full intensity, and then go off. It won't run again until button 1 is pressed again. Per locking out, it really doesn't matter. This code is a basis for some plastic models I'm building. Button 1 is a weapon fire (torpedo). Button 2 is a start up sequence for everything to brighten in a sequence and then stay on. Button 3 (if I can rework my code) will be a "damage" button, causing everything on the ship to blink and flicker at random. So I guess I don't really want any button locking anything out.