Topic outline

  • General

  • Topic 1

    Introduction to programming

    The internet is awash with sites extolling the virtues of Arduinos and their role in STEM education. Arduino is a part of a worldwide Maker Movement where participants are encouraged to find "junk" then "hack" it to fullfill another, useful purpose.

    Sites like

    • youtube
    • instructables
    • thingaverse

    are full of examples.

    CH340 driver

    Like the word "church", Arduino can mean a prototyping board, a programming environmet or the community who wish to contribute to what's happening.

    A plethora of books, web sites and personal learning networks are available to help you along.

    The stated paradigm for learning Ardunino is to set up the board from a picture and download the supplied code.

    However, old school educators look for a slightly more formal approach. 

    • Topic 2

      A super crash course in programming (coding)

      Any book on programming generally follows a similar path, regardless of which language is used

      • introduction
      • data and data types
      • planning a program¬†
      • sequence and branching
      • iteration (loops)
      • decisions (forks)
      • modular programming (user defined functions and procedures)

      maybe it goes on with

      • arrays
      • files and file handling
      • interupts
      There are many things involved in these topics, and we will meet them along the journey
      • Topic 3

        What is programming a computer

         First some definitions.

        A computer can be thought of as an electronic device which is capable of receiving input in a particular form, can store and process this input a sequence of operations   to produce a result in the form of information or signals.

        A computer literate person is one who can take a suitable problem, then apply a solution using appropriate hardware, software and procedures to solve it.



        A computer program is a the series of instructions required to receive  input and process it to produce the required result of information or actions.



        At it's simplest level a computer program comprises three processes
        • sequence
        • iteration
        • decision


        Syntax is the set of rules that govern the way a line of code is constructed.



        • Topic 4

          Sub system : the Arduino board

          arduino board

          The Arduino road map.
          can you identify the following on your board
          • the ATMega328 chip
          • the 16Mhz clock
          • reset switch
          • the port you connect to your computer
          • digital i/o ports
          • analogue i/o ports
          • ports where power is available
          • external power port
          Smarty pants speedy kids might wish to get straight to the "good stuff". If you have an Arduino kit, here is a set of exercises for you to try.

          Sparkfun inventors' guide(PDF)

          Reading Assignment

          About the board from the Arduino web site

          • Topic 5

            Structural data types

            Data type

            A computer program must first define how data is to be stored into a computer's memory. Arduino programs can be divided in three main parts: structurevalues (variables and constants), and functions.


            A full list of data structures

            Structure 




            Control structures



            Arithmetic operators

            A computer program must first define how data is to be stored into a computer's memory. 



            Logical operators




            Boolean operators



            Compound operators

            A computer program must first define how data is to be stored into a computer's memory. 



            .








            .





            • Topic 6

              Variable data types

              Costants

              A computer program must first define how data is to be stored into a computer's memory. 


              Full list of variable data types here

              Data types




              Conversion


              Variable scope & qualifiers

              A computer program must first define how data is to be stored into a computer's memory. 



              Utilities








              .








              .








              .








              .





              • Topic 7

                Functions

                Digital


                Full list of functions here
                Analog


                Time


                • Topic 8

                  Sequence : Your first sketch

                  Arduino calls its applications or programs sketches. To get the feeling for structure, there are four main parts to an Arduino.

                  • Documentation
                  • Varible declaration
                  • Setup
                  • Body of program

                  Step 1 :

                  Write a simple sketch to do nothing using only the words

                  loop, setup, void, void

                  and

                  {, {

                  },  }

                  Required

                  • Computer
                  • Arduino
                  • USB cable

                  Load your sketch to your Arduino.

                  No one ever gets it right first time.

                  Check your cable is plugged in.

                  • The Arduino lights have lit up
                  • You have selected the correct Arduino board
                  • You have selected the correct port (Tools/port)
                  • There are no errors in your code

                  The next couple of examples deal with sketches running in sequence.

                  Vocabulary

                  loop, setup, void, void

                  • Topic 9

                    Sequence :  Making the Arduino blink

                    For newbies, there is a lot happening here.

                    We want a LED to blink, then hack the code.

                    The big danger here is we kill the LED by putting too much electricty through.

                    Here are some really basic lessons to be learnt, mainly how to set up a breadboard.

                    • This uses pins 13 and gnd
                    • a 220 ohm resistor (red red black) (the example uses brown black red)
                    • a LED
                    • a breadboard
                    The challenge is to figure out which way the tie points run and how to connect up the components.

                    The code

                    blink

                    // the setup function runs once when you press reset or power the board

                    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
                    }

                    • Topic 10

                      The IDE and programming

                      An integrated development environment (IDE) is a software application that provides an enviroment for Aduino users to write programs. An IDE normally consists of a source code editor, automatic build tools and a debugger.

                      Download your IDE from the Arduino site and install it on your machine. 





                      Download the IDE here.

                      https://www.arduino.cc/en/Main/Software

                      The Arduino Environment

                      Vocabulary

                      IDE, source code, syntax, run, verify, link, compile,  debug,  

                      Reading assignment

                      The built in sketches from the Arduinp web site

                      • Topic 11


                        • Topic 12

                          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 13

                            Sequence : Modularising the program and passing parameters

                            This is a really fun part of programming, modules and parameter passing.

                            You can read up on it, but Arduino allows for 11 seperate functions or procedures you can call from the main driver routine. 

                            You will see the advantages of this in the next part of the course.

                            Make the blink part of the program a separate module (function)

                            /* This example simply shows how to
                            * convert code to a module
                            *
                            * mrweber
                            * 080216
                            */
                            void setup() {
                            Serial.begin(9600);
                            }
                            void loop() {
                            printer(); // this calls the procedure
                            }
                            void printer(){
                            Serial.println( "hello world ...");
                            delay (1000);
                            }

                            Make a module out of your blink program the blink part of your p x

                            Modularising the program and passing in a parameter as a global value

                            A global variable is one which can be accessed in all parts of the code

                            /* This example simply shows how to
                            * pass a global value to a procedure
                            *
                            * mrweber
                            * 080216
                            */

                            int waitx = 1500;                              // declare a global variable

                            void setup() {
                            Serial.begin(9600);
                            }
                            void loop() {
                            printer(); // this calls the procedure
                            }
                            void printer(){
                            Serial.println( "hello world ...");
                            delay (waitx);                                  // global variable
                            }

                            Pass a global variable into the module and vary the time for it to blink

                            Modularising the program passing a parameter as a value

                            x

                            /* This example simply shows how to
                            * pass a parameter to a procedure
                            * as a value
                            *
                            * mrweber
                            * 080216
                            */

                            void setup() {
                            Serial.begin(9600);
                            }
                            void loop() {
                            printer(880); // this calls the procedure with a value
                            }
                            void printer(int x){ // x is declared locally
                            Serial.println( "hello world ...");
                            delay (x); // x can only be used locally
                            }

                            Again, vary the time of your blink program by passing a value into the orocedure x

                            Modularising the program  passing a parameter as a variable

                            x

                            /* This example simply shows how to
                            * create a variable using a function
                            * and pass it as a parameter to a procedure
                            *
                            * mrweber
                            * 080216
                            */

                            void setup() {
                            Serial.begin(9600);
                            }
                            void loop() {
                            int y = random(1500); // a function to produce a number between 0, 1500
                            printer(y); // this calls the procedure with a value y
                            }
                            void printer(int x){ // here we are using the same procedure in a different sketch
                            Serial.print( "hello world ... ");
                            Serial.println( x);
                            delay (x); // x can only be used locally
                            }

                            You can pass in a string of variables passing a paranmeter as a variable

                            Modularising the program creating  a function ; returning a value

                            x

                            /* This example simply shows how to
                            * create a function using return
                            * and pass it back as a parameter to the main
                            * loop
                            *
                            * mrweber
                            * 080216
                            */

                            void setup() {
                            Serial.begin(9600);
                            }
                            void loop() {
                            int y = random(15); // a function to produce a number between 0, 15
                            int z = dummy_fn(y); // the function to produce a value upto 1500
                            printer(z); // this calls the procedure with a value z
                            }

                            int dummy_fn(int w){ // declare the function as an integer
                            w = 100*w;
                            return(w);
                            }
                            void printer(int x){ // here we are using the same procedure in a different sketch
                            Serial.print( "hello world ... ");
                            Serial.println( x);
                            delay (x); // x can only be used locally
                            }

                            A function is when only one value is returned. Create a function to vary the time of youe blinker.

                            Modularising the program 

                            x
                            x

                            x

                            Modularising the program; using a sentinal to switch 

                            x

                            int sentinal;

                            void setup() {
                            // put your setup code here, to run once:
                            Serial.begin(9600);
                            sentinal = -1;
                            }

                            void loop() {
                            Serial.println(sentinal);
                            // put your main code here, to run repeatedly:
                            if (sentinal == 1)
                            {
                            one();
                            }
                            else if (sentinal == -1)
                            {
                            two();
                            }
                            delay(1000);
                            }

                            void one(){
                            Serial.println ("i am one");
                            sentinal = -1;
                            }
                            void two(){
                            Serial.writeln("i am two");
                            sentinal = 1;
                            }

                            This is a sneaky way to change between modules. It is best used to model arithmetis and geometric progressions. 

                            Increase a value until a maximum is reached. then decrease the value until it hits a minimum.

                            Repeat the process ad infinitum.

                            Modularising the program; the boolean operator

                            x

                            /* This example simply shows how to
                            * use a boolen operator to run a sketch
                            * once
                            * mrweber
                            * 080216
                            */

                            boolean run;
                            void setup() {
                            Serial.begin(9600);
                            run = true;
                            }
                            void loop() {
                            while (run == true){
                            Serial.println ("Hello world");
                            run = false;
                            }
                            }

                            • Topic 14

                              Variables as sentinal values

                              int sentinal;

                              void setup() {
                              // put your setup code here, to run once:
                              Serial.begin(9600);
                              sentinal = -1;
                              }

                              void loop() {
                              Serial.println(sentinal);
                              // put your main code here, to run repeatedly:
                              if (sentinal == 1)
                              {
                              one();
                              }
                              else if (sentinal == -1)
                              {
                              two();
                              }
                              delay(1000);
                              }

                              void one(){
                              Serial.println ("i am one");
                              sentinal = -1;
                              }
                              void two(){
                              Serial.writeln("i am two");
                              sentinal = 1;
                              }

                              • Topic 15

                                Iteration : fixed number of loops

                                • Topic 16

                                  Iteration : variable loops

                                  • Topic 17

                                    Iteration : repeat ... until



                                    • Topic 18

                                      Decision : if .. then .. else

                                      Code snipet to demonstrate the use of if the else statement for above program.

                                      /*
                                      HC-SR04 Ping distance sensor]

                                      */

                                      if (distance < 10) {      // This is where the LED On/Off happens
                                      digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
                                      digitalWrite(led2,LOW);
                                      }
                                      else {
                                      digitalWrite(led,LOW);
                                      digitalWrite(led2,HIGH);
                                      }
                                      if (distance >= 200 || distance <= 0){
                                      Serial.println("Out of range");
                                      }
                                      else {
                                      Serial.print(distance);
                                      Serial.println(" cm");
                                      }
                                      delay(500);
                                      }

                                      if then else

                                      Dont overthink it, dude.

                                      • Topic 19

                                        D

                                        • Topic 20

                                          Iteration : while ... do

                                          • Topic 21

                                            Decision : For

                                            • Topic 22

                                              Decision : Case

                                               



                                              • Topic 23

                                                Packed data type : arrays

                                                • Topic 24

                                                  Packed data types : records

                                                  • Topic 25

                                                    Packed data types : files

                                                    • Topic 26

                                                      Iteration

                                                      • Topic 27

                                                        File and file handling

                                                        • Topic 28

                                                          nnn

                                                          • Topic 29

                                                            nnn

                                                            • Topic 30

                                                              Line following code

                                                               

                                                              /*
                                                              marks little line follower

                                                              with a little bit of mucking around. this code will balance robot speed
                                                              and the speed of turn to follow a line.

                                                              the problem comes when it reaches the end and turns around.

                                                              the next step is to investigate Proportional-Integral-Derivative Control (PID).
                                                              PID is a feedback loop that adjest a process around a set condition.

                                                              */

                                                              int left_motor_1 = 5;
                                                              int left_motor_2 = 6; // these pins support pwm

                                                              int right_motor_1 = 9;
                                                              int right_motor_2 = 10;

                                                              int analogPinx = A0;
                                                              int digiPin = 13;
                                                              int val = 100; // variable to store the value read

                                                              int threshhold = 500; // between 0 - 1024

                                                              boolean run = true;


                                                              void setup(){
                                                              pinMode (right_motor_1,OUTPUT);
                                                              pinMode (right_motor_2,OUTPUT);
                                                              pinMode (left_motor_1,OUTPUT);
                                                              pinMode (left_motor_2,OUTPUT);

                                                              pinMode (digiPin,OUTPUT);

                                                              Serial.begin(9600); // setup serial

                                                              }

                                                              void loop()
                                                              {
                                                              val = analogRead(analogPinx); // read the input pin
                                                              Serial.println(val); // debug value
                                                              delay(1000);
                                                              digitalWrite(digiPin,HIGH);

                                                              forward();
                                                              delay(500);
                                                              while (run == true) // uses == the equality evaluator
                                                              { // run is never set to false so is infinite
                                                              if (val > threshhold) // wiggle left
                                                              {
                                                              left();
                                                              delay (20);
                                                              forward();
                                                              delay(20);
                                                              val = analogRead(analogPinx);
                                                              } // the use of 'x' in case
                                                              // analogPin is reserved word
                                                              else
                                                              {
                                                              right(); // wiggle right
                                                              delay (20);
                                                              forward();
                                                              delay(20);
                                                              val = analogRead(analogPinx);
                                                              }
                                                              }
                                                              }


                                                              void halt()
                                                              {
                                                              digitalWrite(left_motor_1,LOW);
                                                              digitalWrite(left_motor_2,LOW);

                                                              digitalWrite(right_motor_1,LOW);
                                                              digitalWrite(right_motor_2,LOW);

                                                              }

                                                              void back()
                                                              {

                                                              digitalWrite(left_motor_1,LOW);
                                                              digitalWrite(left_motor_2,HIGH);

                                                              digitalWrite(left_motor_1,LOW);
                                                              digitalWrite(right_motor_2,HIGH);

                                                              }

                                                              void left()
                                                              {

                                                              digitalWrite(left_motor_1,LOW);
                                                              analogWrite(left_motor_2,125); // uses an analog signal to control speed

                                                              digitalWrite(right_motor_1,HIGH);
                                                              analogWrite(right_motor_2,125); // better andled with a variable

                                                              }

                                                              void right()
                                                              {
                                                              analogWrite(left_motor_1,125);
                                                              digitalWrite(left_motor_2,LOW);

                                                              digitalWrite(right_motor_1,LOW);
                                                              analogWrite(right_motor_2,125);

                                                              }

                                                              void forward()
                                                              {
                                                              analogWrite(left_motor_1,125);
                                                              digitalWrite(left_motor_2,LOW);

                                                              analogWrite(right_motor_1,125);
                                                              digitalWrite(right_motor_2,LOW);

                                                              }

                                                              • Topic 31

                                                                • Topic 32

                                                                  • Topic 33

                                                                    • Topic 34

                                                                      • Topic 35

                                                                        • Topic 36

                                                                          • Topic 37

                                                                            • Topic 38

                                                                              • Topic 39

                                                                                • Topic 40