Posted by 상큼어린이
const int buttonPin1 = 2; // the number of the pushbutton pin const int buttonPin2 = 4; // the number of the pushbutton pin const int buttonPin3 = 7; // the number of the pushbutton pin const int ledPin[] = {3, 5, 6, 9, 10, 11}; // the number of the LED pin // variables will change: int buttonState1 = 0; // variable for reading the pushbutton status int buttonState2 = 0; // variable for reading the pushbutton status int buttonState3 = 0; // variable for reading the pushbutton status void setup() { // initialize the pushbutton pin as an input: pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT); } void loop(){ // read the state of the pushbutton value: buttonState1 = digitalRead(buttonPin1); buttonState2 = digitalRead(buttonPin2); buttonState3 = digitalRead(buttonPin3); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState1 == HIGH) { for(int i = 0; i < 6; i++){ if(checkSensor1() == HIGH) break; analogWrite(ledPin[i], 255); delay(100); analogWrite(ledPin[i], 0); delay(100); } for(int i = 4; i > 0; i--){ if(checkSensor1() == HIGH) break; analogWrite(ledPin[i], 255); delay(100); analogWrite(ledPin[i], 0); delay(100); } } else if(buttonState2 == HIGH) { for(int i = 0; i < 6; i++){ for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) { if(checkSensor2() == HIGH) break; analogWrite(ledPin[i], fadeValue); delay(15); } // fade out from max to min in increments of 5 points: for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { if(checkSensor2() == HIGH) break; analogWrite(ledPin[i], fadeValue); delay(15); } } } else if(buttonState3 == HIGH) { for(int i = 0; i < 6; i++){ analogWrite(ledPin[i], 255); } delay(100); for(int i = 0; i < 6; i++){ analogWrite(ledPin[i], 0); } delay(100); } else { for(int i = 0; i < 6; i++){ analogWrite(ledPin[i], 0); } } } int checkSensor1(){ buttonState1 = digitalRead(buttonPin1); if (buttonState1 == 0) { return 1; } else { return 0; } } int checkSensor2(){ buttonState2 = digitalRead(buttonPin2); if (buttonState2 == 0) { return 1; } else { return 0; } } int checkSensor3(){ buttonState3 = digitalRead(buttonPin3); if (buttonState3 == 0) { return 1; } else { return 0; } }