/*
   Flashing, winking, blinking, toggle ON/OFF & push-ON LEDs
   using millis function by Kidkara..

   Purple LED: flashing
   Blue LED: winking
   Red LED: blinking
   Yellow LED: push ON with a yellow pushbutton switch
   Green LED: toggle ON/OFF with green pushbutton switch 
*/

unsigned long curmil1;
unsigned long prevmil1;
unsigned long curmil2;
unsigned long prevmil2;
unsigned long curmil3;
unsigned long prevmil3;
unsigned long curmil4;
unsigned long prevmil4;

const unsigned long ONredduration = 100;
const unsigned long OFFredduration = 1000;
const unsigned long ONblueduration = 1000;
const unsigned long OFFblueduration = 200;
const unsigned long purpleduration = 800;

bool redled = LOW; // red LED state
bool blueled = LOW; // blue LED state
bool purpleled = LOW; // purple LED state
bool greenled = LOW; // green LED state

const int redpin =5;
const int bluepin = 6;
const int yellowpin = 4;
const int purplepin = 7;
const int greenpin = 3;
const int yellowpbpin = 11;
const int greenpbpin = 10;


void setup() {
  pinMode(yellowpin, OUTPUT);
  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(purplepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(greenpbpin, INPUT_PULLUP);
  pinMode(yellowpbpin, INPUT_PULLUP);
}

void loop() {
  curmil1 = millis(); // red LED
  curmil2 = millis(); // blue LED
  curmil3 = millis(); // yellow LED
  blinkred();
  winkblue();
  flashpurple();
  pushON();
  toggleOnOff();
}


void blinkred() {
  if (redled == LOW && curmil1 - prevmil1 >= ONredduration) 
  {
    digitalWrite(redpin, redled);
    prevmil1 = curmil1;
    redled = !redled;
  }

  if (redled == HIGH && curmil1 - prevmil1 >= OFFredduration) 
  {
    digitalWrite(redpin, redled);
    prevmil1 = curmil1;
    redled = !redled;
  }
}


void winkblue() {
  if (blueled == LOW && curmil2 - prevmil2 >= ONblueduration) 
  {
    digitalWrite(bluepin, blueled);
    prevmil2 = curmil2;
    blueled = !blueled;
  }

  if (blueled == HIGH && curmil2 - prevmil2 >= OFFblueduration) 
  {
    digitalWrite(bluepin, blueled);
    prevmil2 = curmil2;
    blueled = !blueled;
  }
}


void flashpurple() {
  if (purpleled == LOW && curmil3 - prevmil3 >= purpleduration) 
  {
    digitalWrite(purplepin, purpleled);
    prevmil3 = curmil3;
    purpleled = !purpleled;
  }

  if (purpleled == HIGH && curmil3 - prevmil3 >= purpleduration) 
  {
    digitalWrite(purplepin, purpleled);
    prevmil3 = curmil3;
    purpleled = !purpleled;
  }
}


void pushON() {
  if (digitalRead(yellowpbpin) == LOW) 
  {
    digitalWrite(yellowpin, HIGH);
  }

  else 
  {
    digitalWrite(yellowpin, LOW);
  }
}


void toggleOnOff() {
    if (digitalRead(greenpbpin) == LOW) 
    {
      greenled = !greenled;
      digitalWrite(greenpin, greenled);
      delay(170);
    }
    
}