// Pin numbers
const int buttonPin = 2;
const int ledPin1 = 13;
const int ledPin2 = 12;
const int motorPin1 = 10;
const int motorPin2 = 11;
int m = 0;
// Variables
int buttonState = 0;
int buttonState_last = 0;
unsigned long debounceDelay = 20;
unsigned long lastDebounceTime = 0;
unsigned long buttonTime = 0;
unsigned long modeTime = 0;
unsigned long presstime = 0;
const long interval = 1000;           // interval at which to blink (milliseconds)

const long presslimit = 10000;
int ledState = HIGH;    
int keyswitch =0;
int count = 0;
int press = 0;



void setup() 
{
  pinMode(buttonPin, INPUT);   // Set the button pin as INPUT
  pinMode(ledPin1, OUTPUT);     // Set the LED pin as OUTPUT
  pinMode(ledPin2, OUTPUT);
  pinMode(motorPin1, OUTPUT);     // Set the LED pin as OUTPUT
  pinMode(motorPin2, OUTPUT);
}

void loop() 
{
  lastDebounceTime = buttonTime;
  modeTime = millis();
  buttonState_last = buttonState;
  if(keyswitch == 0)
  buttonState = digitalRead(buttonPin);   // Read the state of the button
    
  
  //Check the button
  if (buttonState ==HIGH&& buttonState_last ==LOW) 
  {
    buttonTime = millis();
    ledState = HIGH;  
    count=0;
    if (buttonTime - lastDebounceTime >= debounceDelay) 
    {
      m++;
      if(m==8)
      m=0;
    }
  }

  //Light up the corresponding LED
  switch(m)
  {
    case 0 :
    {
      press=0;
      digitalWrite(ledPin1, LOW);    // Turn off the LED
      digitalWrite(ledPin2, LOW);
    }break;
    case 2 :
    {
      press = 0;
      digitalWrite(ledPin1, ledState);
      digitalWrite(ledPin2, ledState);   // Turn on the LED
      if (modeTime  - buttonTime >= interval)  // save the last time you blinked the LED
        {
          buttonTime = modeTime;  // if the LED is off turn it on and vice-versa:
          digitalWrite(ledPin1, ledState);
          digitalWrite(ledPin2, ledState); // set the LED with the ledState of the variable:
          if (ledState == HIGH) 
          {
            count++;
            ledState = LOW;
          } 
          else 
          ledState = HIGH;
          // set the LED with the ledState of the variable:
        } 
        if(count==3)
        {
          count=0;
          m=0;
        }
      //digitalWrite(ledPin1, LOW);
      //digitalWrite(ledPin2, LOW);
    }break;

    case 4 :
    {
      press = 1; 
      digitalWrite(ledPin1, ledState);   // Turn on the LED
      if (modeTime  - buttonTime >= interval)  // save the last time you blinked the LED
        {
          buttonTime = modeTime;  // if the LED is off turn it on and vice-versa:
          digitalWrite(ledPin1, ledState); // set the LED with the ledState of the variable:
          if (ledState == HIGH) 
          {
            count++;
            ledState = LOW;
          } 
          else 
          ledState = HIGH;
          // set the LED with the ledState of the variable:
        } 
      digitalWrite(ledPin2, LOW);
      if(count==3)
      {
        count=0;
        digitalWrite(motorPin1, HIGH);
        presstime =  millis();
        
        //delay(10000);

        //digitalWrite(motorPin1, LOW);
        m=0;
      }
    }break;
    case 6 :
    {
      press = 1;
      digitalWrite(ledPin2, ledState);   // Turn on the LED
      if (modeTime  - buttonTime >= interval)  // save the last time you blinked the LED
        {
          buttonTime = modeTime;  // if the LED is off turn it on and vice-versa:
          digitalWrite(ledPin2, ledState); // set the LED with the ledState of the variable:
          if (ledState == HIGH) 
          {
            count++;
            ledState = LOW;
          } 
          else 
          ledState = HIGH;
          // set the LED with the ledState of the variable:
        } 
      digitalWrite(ledPin1, LOW);
      if(count==2)
      {
        count=0;
        digitalWrite(motorPin2, HIGH);
        
        digitalWrite(motorPin2, LOW);
        m=0;
      }
    }break; 
  }
  if (modeTime - presstime >= presslimit)
        {
          m=0;
          digitalWrite(motorPin2, LOW);
        
          digitalWrite(motorPin2, LOW);
        }
}