HELP! Blinking patterns with switch

Hi everyone!

I need help I am stuck and my project is due in 3 days.

I am trying to set something up that has one switch and 3 LEDS.

Each LED has it's own pattern (i.e. fade on for 4 counts, hold full brightness (255) for 3 counts, fade out for 2 counts, hold off for 2 counts, repeat)

The idea is that each LED is a different breathing exercise for the user to follow.

click once- pattern one click 2nd time - pattern 2 click 3rd time- pattern 3 4th click - pattern 0 (off)

If anyone can please direct me towards the proper coding i would be SO grateful. Thank you!

Forget the switch for now. Have you got any of the LED patterns working ? Do you know how to turn an LED on and off ? Do you know how to fade an LED up and down ? Do you know how to time events ?

mjmroy:
Hi everyone!

I need help I am stuck and my project is due in 3 days.

I am trying to set something up that has one switch and 3 LEDS.

Each LED has it’s own pattern (i.e. fade on for 4 counts, hold full brightness (255) for 3 counts, fade out for 2 counts, hold off for 2 counts, repeat)

The idea is that each LED is a different breathing exercise for the user to follow.

click once- pattern one
click 2nd time - pattern 2
click 3rd time- pattern 3
4th click - pattern 0 (off)

If anyone can please direct me towards the proper coding i would be SO grateful.
Thank you!

here is an example of one way to do it:

#include <math.h>

#define NUMBER_OF_ROUTINES 10
#define DEBUG

#ifdef DEBUG
#define DEBUG_PRINTLN(x) Serial.println(x)
#define DEBUG_PRINT(x)   Serial.print(x)
#else 
#define DEBUG_PRINTLN(x) 
#define DEBUG_PRINT(x) 
#endif

void (*ledFunction[NUMBER_OF_ROUTINES])(void)={ ledFunction0, ledFunction1, ledFunction2, ledFunction3, ledFunction4, ledFunction5, ledFunction6, ledFunction7, ledFunction8, ledFunction9 };

byte ledPin = 13;
byte buttonPin = 2;
byte lastPressed;
byte state = 0;
byte increment = 5;
byte briteness;
unsigned long startTime;
boolean printed = false;
//
void setup() 
{  
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP); 
}
//
void loop()
{
  int pressed = digitalRead(buttonPin); 
  if (pressed == LOW)
  {
    if (pressed != lastPressed)
    {
      state++;
      printed = false;
    }
  }
  lastPressed = pressed;
  if (Serial.available())
  {
    char myChar = Serial.read();
    {
      state++;
      printed = false;
    }
  }
  if (state >= NUMBER_OF_ROUTINES) state = 0;
  ledFunction[state]();
}

void ledFunction0()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("Math Fade"));
    printed = true;
  }
  float val = (exp(sin(millis()/2000.0*PI)) - 0.36787944)*108.0;
  analogWrite(ledPin, val);
}
//
void ledFunction1()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("Fast Flash"));
    printed = true;
  }
  if (millis() - startTime >= 100UL)
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    startTime += 100UL;
  }
}
//
void ledFunction2()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("Slow Flash"));
    printed = true;
  }
  if (millis() - startTime >= 500UL)
  {
    digitalWrite(ledPin, !digitalRead(ledPin));
    startTime += 500UL;
  }
}
//
void ledFunction3()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("2.5% Duty Cycle Flash"));
    printed = true;
  }
  unsigned long nowMillis = millis();
  if (nowMillis - startTime < 25UL)
  {
    digitalWrite(ledPin, HIGH);
  }
  if (nowMillis - startTime < 1000UL)
  {
    digitalWrite(ledPin, LOW);
  }
  else
  {
    startTime += 1000UL;
  }
}
//
void ledFunction4()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("Quick Fade"));
    printed = true;
  }
  if (millis() - startTime >= 10UL)
  {
    briteness += increment;
    if (briteness >= 255 || briteness <= 0) increment = - increment;
    analogWrite(ledPin, briteness);
    startTime += 10UL;
  }
}
//
void ledFunction5()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("Slow Fade"));
    printed = true;
  }
  if (millis() - startTime >= 35UL)
  {
    briteness += increment;
    if (briteness >= 255 || briteness <= 0) increment = - increment;
    analogWrite(ledPin, briteness);
    startTime += 35UL;
  }
}
//
void ledFunction6()
{
  static boolean fadeNow;
  if (!printed)
  {
    DEBUG_PRINTLN(F("Fade Down and Up with Pause"));
    printed = true;
  }
  if (millis() - startTime > 1000UL)
  {
    fadeNow = true;
    briteness = 255;
    increment = -5;
  }
  if (fadeNow)
  {
    for (int i = 0; i < 2; i++)
    {
      for (int j = 0; j <= 255; j += 5)
      {
        analogWrite(ledPin, briteness);
        briteness += increment;
        delay(25);
      }
      increment = - increment;
    }
    fadeNow = false;
  }
}
//
void ledFunction7()
{
  static boolean fadeNow;
  if (!printed)
  {
    DEBUG_PRINTLN(F("Fade Up then Down with Pause"));
    printed = true;
  }
  if (millis() - startTime > 1000UL)
  {
    fadeNow = true;
    briteness = 0;
    increment = 5;
  }
  if (fadeNow)
  {
    for (int i = 0; i < 2; i++)
    {
      for (int j = 0; j <= 255; j += 5)
      {
        analogWrite(ledPin, briteness);
        briteness += increment;
        delay(25);
      }
      increment = - increment;
    }
    fadeNow = false;
  }
}
//
void ledFunction8()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("Led On"));
    printed = true;
  }
  digitalWrite(ledPin, HIGH);
}
//
void ledFunction9()
{
  if (!printed)
  {
    DEBUG_PRINTLN(F("Led Off"));
    printed = true;
  }
  digitalWrite(ledPin, LOW);
}