Quantcast
Channel: Seeedstudio Forum
Viewing all articles
Browse latest Browse all 6612

PIN for external device

$
0
0
Of course you can. See the following demo, press the button (high level) for more than 5s and the led will be turn on. if the pressing time is between 1s and 5s, the led will be turn off. If the time is less than 1s, nothing happen. Image

Code: unsigned long time;
unsigned long time1;
#define led 3
#define button 2
unsigned long sum;

void setup(){
  Serial.begin(9600);
  pinMode(3,OUTPUT);
  pinMode(2,INPUT);
  digitalWrite(3,LOW);
}
void loop(){
  int i=digitalRead(2);
  Serial.println(i);
  if(i==HIGH)
  {
    time = millis();
    while(digitalRead(2)==HIGH)
     {
     }
    time1=millis();
    sum=time1-time;
    if(sum>=5000)
     {
       digitalWrite(3,HIGH);
     }
     else if( (sum>=1000) && (sum<5000) )
     {
       digitalWrite(3,LOW);
     }
     else
     {
     }
  }   
}


Statistics : Posted by pizzic • on Tue Apr 07, 2015 1:12 am • Replies 1 • Views 34

Viewing all articles
Browse latest Browse all 6612

Trending Articles