Servo myservo; 
int pos = 0;   
int led1 = D1;
int led2 = D2;
int led3 = D3;
int one = 0;
int button1 = D4;
int bs1 = 0;
int button2 = D5;
int bs2 = 0;
int button3 = D6;
int bs3 = 0;
int button4 = D7;
int bs4 = 0;
int chooser = 3;

void setup()
{
    Particle.function("gong", gong);
    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);
    pinMode(led3, OUTPUT);
    pinMode(button1, INPUT);
    pinMode(button2, INPUT);
    pinMode(button3, INPUT);
    pinMode(button4, INPUT);
    myservo.attach(D0);    
    pinMode(D7, OUTPUT);  
    Serial.begin(9600);
}

int gong(String command)   
{                         
    if(command == "now")  
    {                            
        myservo.write(0);       
        digitalWrite(led1, HIGH);
        digitalWrite(led2, HIGH); 
        digitalWrite(led3, HIGH); 
        delay(1000);             
        myservo.write(200);     
        digitalWrite(led1, LOW);
        digitalWrite(led2, LOW); 
        digitalWrite(led3, LOW);
        one = !one;
        return 1;               
    }
    else if(command == "alarm")   
    {                               
        for (int i = 0; i < 3; i++) 
        {
            myservo.write(0);
            digitalWrite(D7, HIGH);
            delay(100);
            myservo.write(25);
            digitalWrite(D7, LOW);
            delay(1000);
        }
        return 2;                
    }
    else if(command == "way1")   
    {                               
        chooser = 0;
        return 3;                
    }
    else if(command == "way2")   
    {                               
        chooser = 1;
        return 4;                
    }
    else if(command == "way3")   
    {                               
        chooser = 2;
        return 5;                
    }
    else if(command == "reset")   
    {                               
        chooser = 3;
        return 6;                
    }
}

void loop()
{
    bs1 = digitalRead(button1);
    bs2 = digitalRead(button2);
    bs3 = digitalRead(button3);
    bs4 = digitalRead(button4);
    
    if (chooser == 0)
    {
        if (bs1 == LOW)
        {
            delay(1000);
            for (int i = 0; i < 1000; i++) 
            {
                bs1 = digitalRead(button1);
                bs2 = digitalRead(button2);
                bs3 = digitalRead(button3);
                bs4 = digitalRead(button4);
                digitalWrite(D1, LOW);
                digitalWrite(D2, HIGH);
                digitalWrite(D3, LOW);
                if (bs2 == LOW)
                {
                    delay(1000);
                    for (int i = 0; i < 1000; i++) 
                    {
                        bs1 = digitalRead(button1);
                        bs2 = digitalRead(button2);
                        bs3 = digitalRead(button3);
                        bs4 = digitalRead(button4);
                        digitalWrite(D1, LOW);
                        digitalWrite(D2, HIGH);
                        digitalWrite(D3, LOW);
                        if (bs3 == LOW)
                        {
                            delay(1000);
                            for (int i = 0; i < 1000; i++) 
                            {
                                bs1 = digitalRead(button1);
                                bs2 = digitalRead(button2);
                                bs3 = digitalRead(button3);
                                bs4 = digitalRead(button4);
                                digitalWrite(D1, LOW);
                                digitalWrite(D2, HIGH);
                                digitalWrite(D3, LOW);
                                if (bs4 == LOW)
                                {
                                    delay(1000);
                                    myservo.write(120);
                                    digitalWrite(D1, LOW);
                                    digitalWrite(D2, LOW);
                                    digitalWrite(D3, HIGH);
                                    delay(1000);
                                    i = 999;
                                }
                                /*else if (bs1 == LOW || bs3 == LOW || bs2 == LOW)
                                {
                                    digitalWrite(D1, HIGH);
                                    digitalWrite(D2, LOW);
                                    digitalWrite(D3, LOW);
                                    i = 999;
                                }*/
                                delay(50);
                            }
                            i = 999;
                        }
                        /*else if (bs1 == LOW || bs2 == LOW || bs4 == LOW)
                        {
                            digitalWrite(D1, HIGH);
                            digitalWrite(D2, LOW);
                            digitalWrite(D3, LOW);
                            i = 999;
                        }*/
                        delay(50);
                    }
                    i = 999;
                }
                /*else if (bs1 == LOW || bs3 == LOW || bs4 == LOW)
                {
                    digitalWrite(D1, HIGH);
                    digitalWrite(D2, LOW);
                    digitalWrite(D3, LOW);
                    i = 999;
                }*/
                delay(50);
            }
        }
    }
    else if (chooser == 2)
    {
        myservo.write(120);
        digitalWrite(D1, LOW);
        digitalWrite(D2, LOW);
        digitalWrite(D3, HIGH);
    }
    else if (chooser == 1)
    {
        if (bs1 == LOW)
        {
            delay(1000);
            for (int i = 0; i < 1000; i++) 
            {
                bs1 = digitalRead(button1);
                bs2 = digitalRead(button2);
                bs3 = digitalRead(button3);
                bs4 = digitalRead(button4);
                digitalWrite(D1, LOW);
                digitalWrite(D2, HIGH);
                digitalWrite(D3, LOW);
                if (bs1 == LOW)
                {
                    delay(1000);
                    for (int i = 0; i < 1000; i++) 
                    {
                        bs1 = digitalRead(button1);
                        bs2 = digitalRead(button2);
                        bs3 = digitalRead(button3);
                        bs4 = digitalRead(button4);
                        digitalWrite(D1, LOW);
                        digitalWrite(D2, HIGH);
                        digitalWrite(D3, LOW);
                        if (bs1 == LOW)
                        {
                            delay(1000);
                            for (int i = 0; i < 1000; i++) 
                            {
                                bs1 = digitalRead(button1);
                                bs2 = digitalRead(button2);
                                bs3 = digitalRead(button3);
                                bs4 = digitalRead(button4);
                                digitalWrite(D1, LOW);
                                digitalWrite(D2, HIGH);
                                digitalWrite(D3, LOW);
                                if (bs1 == LOW)
                                {
                                    delay(1000);
                                    myservo.write(120);
                                    digitalWrite(D1, LOW);
                                    digitalWrite(D2, LOW);
                                    digitalWrite(D3, HIGH);
                                    delay(1000);
                                    i = 999;
                                }
                                delay(50);
                            }
                            i = 999;
                        }
                        delay(50);
                    }
                    i = 999;
                }
                delay(50);
            }
        }
    }
    else if (chooser == 3)
    {
        myservo.write(10);
        digitalWrite(D1, HIGH);
        digitalWrite(D2, LOW);
        digitalWrite(D3, LOW);
        delay(1000);
    }
}