Topic outline

  • Topic 1

    Introduction

    This section looks at using just 10 LEDS to teach a variety of concepts.

    It is assumed you have been through the previous module where you have built a circuit, the uploaded a sketch called "blink"

    Of course, it is expected you have hacked it to change the time it is off and on.

    • Topic 2

      The bare minimum

      Here is the bare minimum to run code. It does nothing, but lay out the minimum requirement for code to compile on the Arduino IDE.

      void setup() {
      // put your setup code here, to run once:

      }

      void loop() {
      // put your main code here, to run repeatedly:

      }

      • Topic 3

        Deconstructing blink

        This is the sketch as it appears in the Arduino examples.

        pinMode is a reserved word that tells the Aruino what the pin is supposed to do. It can either INPUT or OUTPUT a signal.

        delay(1000); halts the code for 1 second

        The Arduino runs on 5 volts. By default, all pins on the Arduino are set to LOW when it starts up.

        digitalWrite(13, HIGH) sets the 13 pin to +5 volts.
        digitalWrite(13, LOW) sets the 13 pin to zero volts. 


        void setup() {
        // initialize digital pin 13 as an output.
        pinMode(13, OUTPUT);
        }

        // the loop function runs over and over again forever
        void loop() {
        digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
        delay(1000); // wait for a second
        digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
        delay(1000); // wait for a second
        }

        You may have noticed the particular way ledPin13 is written. It is called "camel case" and makes long names especially file names, easier to read.

        #define ledPin13  13 // This loads the value 13 into memory as a constant value assigned the name ledPin13.

        void setup() {
        // initialize digital pin 13 as an output.
        pinMode(ledPin13, OUTPUT);
        }


        void loop() {
        digitalWrite(ledPin13, HIGH); // turn the LED on (HIGH is the voltage level)
        delay(1000); // wait for a second
        digitalWrite(ledPin13, LOW); // turn the LED off by making the voltage LOW
        delay(1000); // wait for a second
        }

        • Topic 4

          Two leds

          This sketch has been created using the copy and paste functions of the keyboard to quickly create this sketch

          #define ledPin13 13
          #define ledPin12 12
          void setup() {

          pinMode(ledPin13, OUTPUT);
          pinMode(ledPin12, OUTPUT);
          }


          void loop() {
          digitalWrite(ledPin13, HIGH);
          delay(1000);
          digitalWrite(ledPin13, LOW);
          delay(1000);
          digitalWrite(ledPin12, HIGH);
          delay(1000);
          digitalWrite(ledPin12, LOW);
          delay(1000);
          }

          • Topic 5

            Chasers

            Increase the number of LEDs from 2 to 8. 

            Make your LEDs

            Start at ledPin1 and light up in order to ledPin8 
            Start in the middle and move out
            Now make as many patterns you like

            • Topic 6

              Two LEDs and a subroutine

              Arduino can break the code up into little chunks called subroutines or procedures

              #define ledPin13 13
              #define ledPin12 12
              void setup() {

              pinMode(ledPin13, OUTPUT);
              pinMode(ledPin12, OUTPUT);
              }


              void loop() {
              flasher();    // this calls a subroutine
              }


              void flasher() // this is the subroutine
              {
              digitalWrite(ledPin13, HIGH);
              delay(1000);
              digitalWrite(ledPin13, LOW);
              delay(1000);
              digitalWrite(ledPin12, HIGH);
              delay(1000);
              digitalWrite(ledPin12, LOW);
              delay(1000);
              }

              • Topic 7

                Passing a variable into a sub procedure

                This is really cool. 

                Remember when we used #define to declare a constant value. It is loaded into the Arduino's memory and can only have one value.

                But what if we wanted to change it?

                We would use a variable. We can declare a variable in our sub procedure header and change the value as it is passed from the drive routine.

                #define ledPin13 13
                #define ledPin12 12
                void setup() {

                pinMode(ledPin13, OUTPUT);
                pinMode(ledPin12, OUTPUT);
                }

                void loop() {
                flasher(ledPin13);
                flasher(ledPin12);
                }
                void flasher(int x ) // we only need 4 lines of code
                {
                digitalWrite(x, HIGH);
                delay(1000);
                digitalWrite(x, LOW);
                delay(1000);

                }

                • Topic 8

                  one LED analog OUT (PWM)

                  This is one way of turning a LED on and off using an analog command.

                  analogWrite is used on pins 3,5,6, 9, 10,11. They allow the value of the signal to appear to be somewhere between 0 and 255 which is converted to a voltage of between 0 - 5 volts.

                  It also demonstrates the use of a loop. Read 

                  for (int i=0; i <= 255; i++)

                  as for integer i becomes 0, test for i is less than max value, then i++ means add 1

                  arduino reference for loop

                  int PWMpin = 10; // LED in series with 220 ohm resistor on pin 10

                  void setup()
                  {
                  // no setup needed
                  }

                  void loop()
                  {
                  for (int i=0; i <= 255; i++){
                  analogWrite(PWMpin, i);
                  delay(10);
                  }
                  }

                  • Topic 9

                    Using the serial monitor : Hello world

                    No introduction to programming is complete without the old "Hello world " message. Legend has it, this was the first message ever printed out by a computer.

                    We will use the Serial library. This is some code that is declared in the setup, and is joined into the final program that is compiled and loaded onto the board.


                    Add to your code to reflect the following changes


                    /*

                    This sketch writes "hello world" to the serial monitor

                    */

                    void setup() {
                    Serial.begin(9600);
                    }

                    void loop() {
                    Serial.print( "hello world ...");
                    delay (1000);
                    }

                    In your IDE, you can see a small magnifying glass in the top RHS of the window.

                    Click on this

                    Change Serial.print to Serial.println

                    What changes does this make to your output?

                    Vocabulary

                    Library, serial, baud,

                    • Topic 10

                      Using a function : random()

                      The function randNumber = random(1,5); // generate random number between 1 & 5. We can view the result in the serial window.

                      This code 

                      int randNumber ;

                      void setup() {
                      Serial.begin(9600);
                      }

                      void loop() {
                      randNumber = random(1,5); // generate random number between 1 & 5
                      Serial.println( randNumber);
                      delay (1000);
                      }

                       

                      • Topic 11

                        Selecting LEDS if ... then ... else

                        /*

                        This sketch writes "hello world" to the serial monitor

                        */
                        int randNumber ;    // define variable

                        #define ledPin13 13
                        #define ledPin12 12
                        void setup() {

                        pinMode(ledPin13, OUTPUT);
                        pinMode(ledPin12, OUTPUT);

                        Serial.begin(9600); 
                        }

                        void loop() {
                        randNumber = random(1,5); // generate random number between 1 & 5
                        Serial.println( randNumber);
                        if (randNumber <= 3){       // test to see if value is lower
                        digitalWrite(ledPin13,HIGH);
                        delay(500);
                        digitalWrite(ledPin13,LOW);
                        delay(500);
                        }
                        else                              // if test fails do something else
                        {
                        digitalWrite(ledPin12,HIGH);
                        delay(500);
                        digitalWrite(ledPin12,LOW);
                        delay(500);
                        }


                        }

                        • Topic 12

                          nnn

                          • Topic 13

                            nnn

                            • Topic 14

                              nnn

                              • Topic 15

                                nnn

                                • Topic 16

                                  nnn

                                  • Topic 17

                                    nnn

                                    • Topic 18

                                      nnn

                                      • Topic 19

                                        nnn

                                        • Topic 20

                                          nnn