// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0,abcMillis=0; // will store last time LED was updated
unsigned long button=0;
int buttonflag=1;
int count=0,longflag=0;
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 500,interval1=200; // interval at which to blink (milliseconds)
const int buttonPin = 2; // the number of the pushbutton pin
int buttonState = 0;
void setup() {
Serial.begin(9600);
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop()
{
unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
//digitalWrite(ledPin, HIGH);
Serial.println("buttonenable");
if(buttonflag==1) {
button=millis();
buttonflag=0;
count=count+1;
abcMillis = millis();
Serial.println("count+1");
}
if(currentMillis-button>3000){
longflag=1;
Serial.println("longflag=1");
}else {
longflag=0;
}
}
else {
// turn LED off:
//digitalWrite(ledPin, LOW);
if(longflag) {
digitalWrite(ledPin, LOW);
count=0;
//button=0;
Serial.println("longbutton");
longflag=0;
}else {
if(currentMillis - abcMillis > interval1) {
//abcMillis = currentMillis;
Serial.println(count);
if(count==1) {
Serial.println("count=1");
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
//count=0;
}
}
if(count==2) {
//count=0;
Serial.println("count=2");
for(int i=0;i<3;i++){
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
for(int i=0;i<3;i++){
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
delay(1000);
}
for(int i=0;i<3;i++){
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
}
}
}
}
buttonflag=1;
}
}
====================================
沒有留言:
張貼留言
文章有誤或有問題麻煩您留言告知! 謝謝您~~