© Royalty Free/CORBIS
© Royalty Free/Corbis


Exercise 1
Exercise 2
Exercise 3




Exercise 1

pulldown.png
Code a program that will control: with button BTN1 on P7


Exercise 2

Modify the program from Exercise 4 so that when you press the button, LED1 blinks 5 times a second (100 milliseconds on and 100milliseconds off) when and is completely off when the button is released.

Exercise 3

Blinking a light without using delay():
If you are testing for a button press or some other event, a delay will interfere with your results. You can't use delay(), or you'd stop everything else in the program while the LED blinked. Here's some code that demonstrates how to blink the LED without using delay(). It keeps track of the last time it turned the LED on or off. Then, each time through loop() it checks if a sufficient interval has passed - if it has, it turns the LED off if it was on and vice-versa.

Copy code and modify it. Wire your board and upload the program
/* Blinking LED without using delay*/

// LED should be the pin connected to an LED	
int ledPin = ___;

// set value to the state of the LED before the program runs 
int value = ___; 

// store last time LED was updated
long previousMillis = 0; 

// interval at which to blink (milliseconds)
long interval = _____;           

void setup(){ 
// sets the digital pin as output
 _____________ 
}

void loop(){
  // check to see if the difference between the current time and 
  //last time we blinked the LED bigger than
  // the interval at which you want to blink the LED.
  if (millis() - previousMillis > interval) {
  
  // remember the last time you blinked the LED
  //by setting previousMillis to millis()
   _________________
    
    // if the LED is off turn it on and vice-versa.
    if (value == LOW){
      value = ___;
    }else{
      value = ____;
    }
    digitalWrite(ledPin, value);
  }
}