2012年4月9日 星期一

Arduino HW10


 This is a very simple example of showing the led in pattern with Arduino.
Below ís the source code:

int timerFast = 100;           // The higher the number, the slower the timing.
int timerSlow = 200;

void setup() {
  // use a for loop to initialize each pin as an output:
  for (int thisPin = 0; thisPin < 13; thisPin += 2)  {
    pinMode(thisPin, OUTPUT);      
  }
}

void loop() {
  //loop from both ends to the middle pin
  for (int thisPin = 0; thisPin < 6; thisPin += 2)
  {
    // turn the pins on:
    digitalWrite(thisPin, HIGH);   
    digitalWrite(12 - thisPin, HIGH);
    delay(timerFast);                  

    // turn the pins off
    digitalWrite(thisPin, LOW);    
    digitalWrite(12 - thisPin, LOW);
    delay(timerSlow);    
  }

  // turn the middle pin on
  digitalWrite(6, HIGH);
  delay(timerFast);
  //turn the middle pin off
  digitalWrite(6, LOW);
  delay(timerSlow);

  // turn all pins on and off for several times
  for(int i = 0; i < 3; i++)
  {
    // loop from the lowest pin to the highest:
    for (int thisPin = 0; thisPin < 13; thisPin += 2) { 
      // turn the pin on:
      digitalWrite(thisPin, HIGH);   
    }

    delay(timerFast);

    // loop from the lowest pin to the highest:
    for (int thisPin = 0; thisPin < 13; thisPin += 2) { 
      // turn the pin on:
      digitalWrite(thisPin, LOW);   
    }
    delay(timerFast);
  }
  delay(timerSlow);

  // turn the middle pin on
  digitalWrite(6, HIGH);
  delay(timerSlow);
  //turn the middle pin off
  digitalWrite(6, LOW);

  //loop from middle pin to both ends
  for (int thisPin = 6; thisPin >= 0; thisPin -= 2)
  {
    // turn the pins on:
    digitalWrite(thisPin, HIGH);   
    digitalWrite(12 - thisPin, HIGH);
    delay(timerSlow);                  

    // turn the pins off
    digitalWrite(thisPin, LOW);    
    digitalWrite(12 - thisPin, LOW);    
    delay(timerSlow);
  }

  // turn all pins on and off for several times
  for(int i = 0; i < 2; i++)
  {
    // loop from the lowest pin to the highest:
    for (int thisPin = 0; thisPin < 13; thisPin += 2) { 
      // turn the pin on:
      digitalWrite(thisPin, HIGH);   
    }

    delay(timerFast);

    // loop from the lowest pin to the highest:
    for (int thisPin = 0; thisPin < 13; thisPin += 2) { 
      // turn the pin on:
      digitalWrite(thisPin, LOW);   
    }
    delay(timerFast);
  }
  delay(timerSlow);
}





沒有留言:

張貼留言