Learn Arduino with Olympia Circuits
Learn Arduino
  • Home
    • Get Started
    • How to Use This Site
  • Electronics
    • The Basics
    • Electricity Flows like Water
    • Electronic Components
    • The Arno Board
  • Programming
    • The Basics
    • setup and loop Blocks
    • Variables and Arrays
    • Connecting with the Pins
    • Flow Control >
      • if Statement
      • Loops
      • Delays
    • Functions
    • Serial Communication
    • USB
    • Some Thoughts
  • Projects
    • Projects 1 >
      • 1.01: Blink
      • 1.02 Blink x2
      • 1.03 Blink Faster
      • 1.04 LED Chase!
      • 1.05 Wait To Blink
      • 1.06 Blink a Little Faster Now
      • 1.07 LED Fade
      • 1.08 RGB Blink
      • 1.09 Change RGB Color with SW1
      • 1.10 Fade RGB Colors
      • 1.11 Reaction Time Game
    • Projects 2 >
      • 2.01 Hello World
      • 2.02 Talk Back
      • 2.03 ASCII Values
      • 2.04 Ski Game
      • 2.05 Demonstration of the String Object
    • Projects 3 >
      • 3.01 Read the Potentiometer
      • 3.02 ASCIIbet Soup
      • 3.03 Potentiometer sets LED Brightness
      • 3.04 Potentiometer Sets Blink Rate
      • 3.05 LED Chase, Part II
    • Projects 4 >
      • 4.01 Bringing the Piezo to Life
      • 4.02 Controlling the Piezo with a Function
      • 4.03 Piezo C Major
      • 4.04 Piezo Greensleaves
      • 4.05 Piezo Metronome
      • 4.06 Piezo as an Input
      • 4.07 Piezo as an Input 2
      • 4.08 Metronome II
      • 4.09 Piezo Playback
      • 4.10 Piezo Fireworks
      • 4.11 Piezo Mosquito
    • Projects 5 >
      • 5.01 The Phototransistor
      • 5.02 Light and Sound
      • 5.03 Light and Sound II
    • Projects 6 >
      • 6.01 EEPROM
      • 6.02 I2C Address Scan
      • 6.03 Read the I2C Temperature Sensor
      • 6.04 High Temperature Alarm
    • Projects 7 >
      • 7.01 Arno Phone Home
      • 7.02 Keyboard Alphabet
      • 7.03 Move Mouse
      • 7.04 Draw Squares
    • Special Projects >
      • Bike Light Demo
  • References
    • Arno Pin Key
    • Arno Schematic
    • Project Index

Project 1.03 Blink Faster

 We’ll now make a small change to Project 1.01 by replacing the fixed delay with an int variable that is decreased by 25% after each blink.


Concepts: integer math, if statement

Circuits: 
  • Circuit 1


  • Select Sketch

    ///////////////////////////////////////////////////
    //Project 2 Blink Faster
    int LED1 = 13;
    int wait = 1000;
    void setup(){
    pinMode(LED1,OUTPUT);
    }
    void loop(){
    digitalWrite(LED1,HIGH);
    delay(wait);
    digitalWrite(LED1,LOW);
    delay(wait);
    wait = wait/4*3;
    if(wait < 5) wait = 1000;
    }
    ///////////////////////////////////////////////////

    Most of this sketch is the same as in Project 1.01. We add an addition int variable, wait, to keep track of the delay between blinks.  We set its initial value to 1000:

    int wait = 1000;

    After blinking LED2, we multiply the variable wait by 3 and divided by 4.  It would be logical to simply multiply by 0.75 but that would mix an int with a float value, which can have unexpected effects:

    wait = wait*3/4;

    The result of this math now replaces the previous value held by wait.

    In the first pass through the loop() block, wait is changed from 1000 to 750.  In the next pass, wait starts at 750 and is changed to 562.  It keeps getting smaller with every pass. Eventually, LED2 is switched on and off so fast that we can’t even tell its blinking.  We need to reset wait.  We use a conditional statement to change wait back to 1000 when it falls below 5:

    if(wait < 5) wait = 1000;

    }

    And the closing bracket } finishes the loop() block.


    Back to Projects 1

    Copyright Olympia Circuits LLC 2014. All Rights Reserved.