Topic outline

  • General

    Mac Users

    The Arduino needs a driver to run the serial communications through the USB port.

    Search for the CH34X driver. Select the best one for your OS

  • Topic 1

    Lesson 1 : Getting started

    Major objective:

    • to build a basic robot with a view to improve on the build next year.

    Other objectives:

    • structured programming
    • basic circuitry
    • familiarity with the names of common components
    • gain proficiency with tools eg screwdriver

    deal ex

    Lesson

    • introduction
    • revise circuits
    • hand out arduino kits
    • revise parts to a sketch

    Task

    • load blink onto arduino
    • Revise "blink"
  • Topic 2

    Lesson 2

    Objectives

    revise the four main parts of an Arduino sketch

    build a circuit that operates 2 LEDs

    Task

    add a small motor so it can be controlled in both directions

    Demonstrate

    how a transistor can be used to switch a stronger voltage through a larger motor

    the gate on the transistor is controlled by the resistor. about 450 ohms with a 9volt battery should be ok.

    be warned, the transistor gets very hot.

    Image result for circuit for a transistor controlling motor
  • Topic 3

    Lesson 3 : Using a motor driver (ln 292)

    Objective

    to build a circuit that uses a motor driver

    Demonstrate

    • using a transistor to control a circuit so a different power source can be used
    • demonstrate something that only has power and cannot be controlled
    • pose the need for intelligence
    • introduce motor driver and h-bridge
    • show how rc car has a h-brige 

    Task

    • complete a circuit with arduino, power supply, ln 297, 2 motors and use following code to get them to run

    • Topic 4

      Next few lessons : Build a robot

      Assemble robot from supplied components

      • Topic 5

        Challenge 1 : Getting it to run and stop

        Copy and paste the following code into your Arduino IDE.

        The objective is to make sure the wheel motors are plugged into the motor driver so they propel the vehicle forward.

        Check int 1 is connected to pin 9 etc. If the robot does not go forward, reverse the leads on motor a and/or b.

        Note : it is not unusual for your robot not to travel in a straight line. You need to balance the load on both motors. There are several techniques, and we will cover one, pulse width modulation, later.


        /*
        Robot 1 : Documentation

        This is the first program for running the simple robot.
        We just want to make sure the motors spin the right way and the robot moves forward.

        Note

        the use of modular code
        use of if 
        timex as a global variable
        uses pins 5,6,9,10

        int defines an integer, int1 defines an interupt

        */


        int motor1 = 5;
        int motor2 = 6;

        int motor3 =9;
        int motor4 = 10;

        int timex = 3000; //runs for 3 seconds
        boolean run; // sets run to true

        void setup() {

        pinMode (motor1,OUTPUT);
        pinMode (motor2,OUTPUT);

        pinMode (motor3,OUTPUT);
        pinMode (motor4,OUTPUT);

        run = true;
        }

        void loop() {
        if (run)
        {
        forward(); // sets the robot forward.
        // check motors are spinning in same direction
        delay(timex);
        halt(); // stops the code running
        run = false; // sets run to false, exits the loop
        }
        }

        void forward() // forward sub proceedure
        {
        digitalWrite(motor1,HIGH);
        digitalWrite(motor2,LOW);

        digitalWrite(motor3,HIGH);
        digitalWrite(motor4,LOW);

        }

        void halt() // halt sub proceedure
        {

        digitalWrite(motor1,HIGH);
        digitalWrite(motor2,HIGH);

        digitalWrite(motor3,HIGH);
        digitalWrite(motor4,HIGH);

        }


        Solder a switch on the positive (red) wire, between the battery pack and the boards. drill and appropriately sized hole and screw the switch in. 

        This is a useful innovation for conrolling your device during testing.

        • Topic 6

          Forward, back, left and right 

          /*Robot 1

          This is the first program for running the simple robot.
          We just want to make sure the motors spin the right and left correctly and the robot moves forward .

          note the use of the locally declared variable, wait
          */


          int int1 = 5;
          int int2 = 6;

          int int3 =9;
          int int4 = 10;

          boolean run;

          void setup() {

          pinMode (int1,OUTPUT);
          pinMode (int2,OUTPUT);

          pinMode (int3,OUTPUT);
          pinMode (int4,OUTPUT);

          run = true;
          }

          void loop() {
          if (run)
          { // parameter passed as a value

          forward(3000); // sets the robot forward.
          // check motors are spinning in same direction;
          halt(); // stops motors running

          left(3000);
          halt(); // stops motors running

          right(3000);
          halt(); // stops motors running

          reverse(3000);
          halt(); // stops the motors running

          run = false; // halts the code
          }
          }

          void reverse(int wait){ // reverse sub procedure

          digitalWrite(int1,LOW);
          digitalWrite(int2,HIGH);

          digitalWrite(int3,LOW);
          digitalWrite(int4,HIGH);

          delay(wait);
          }

          void forward(int wait){ // forward sub procedure

          digitalWrite(int1,HIGH);
          digitalWrite(int2,LOW);

          digitalWrite(int3,HIGH);
          digitalWrite(int4,LOW);

          delay(wait);
          }

          void halt() // halt sub proceedure
          {

          digitalWrite(motor1,HIGH);
          digitalWrite(motor2,HIGH);

          digitalWrite(motor3,HIGH);
          digitalWrite(motor4,HIGH);

          }

          void right(int wait){

          digitalWrite(int1,LOW);
          digitalWrite(int2,HIGH);

          digitalWrite(int3,HIGH);
          digitalWrite(int4,LOW);

          delay(wait);
          }

          void left(int wait){

          digitalWrite(int1,HIGH);
          digitalWrite(int2,LOW);

          digitalWrite(int3,LOW);
          digitalWrite(int4,HIGH);

          delay(wait);
          }

          • Topic 7

            Lesson 6 : Going straight using parameters

            By declaring the special digital pins marked with a tilde (~) on the board, you can vary the output from 0 to 5v in 255 increments.

            No two motors ever are completely equal and so by running them at different power settings, you can make the robot "go straight".

            /*Controlling a robot using parameters
              All the code can be run using one simple procedure and calling it
              passing variables as parameters
              author mrweber
              date 22/04/2017
            */
            int motorPin1 = 5; // choose the pin for the motor
            int motorPin2 = 6; // choose the pin for the motor
            int motorPin3 = 9; // choose the pin for the motor
            int motorPin4 = 10; // choose the pin for the motor
            int delayTime = 1000;
            boolean runx = true;
            void setup() {
              pinMode(motorPin1, OUTPUT); // declare as output
              pinMode(motorPin2, OUTPUT); // declare as output
              pinMode(motorPin3, OUTPUT); // declare as output
              pinMode(motorPin4, OUTPUT); // declare as output
            }
            void loop() {
              // put your main code here, to run repeatedly:
              while (runx == true)
              {
             
                  turn(0, 0, 0, 0, 0);
                  delay(delayTime);
                  turn(0, 255, 0, 255, 2000);
                  delay(delayTime); 
                  turn(0, 0, 0, 0, 0); // a little breather
                 
                  runx = false;
                }
              }
            }
            void turn(int x1, int x2, int x3, int x4, int delayx)
            {
              analogWrite(motorPin1, x1);
              analogWrite(motorPin2, x2);
              analogWrite(motorPin3, x3);
              analogWrite(motorPin4, x4);
              delay (delayx);
            }
            • Topic 8

              Parameter passing using Boolean values

              /* this sets a pin to high then low by passing the value to the procedure blinker. A boolen value has only two sates, true or false */

              int led_1 = 8;
              boolean b_1 = LOW;

              void setup(){

              pinMode (led_1, OUTPUT);

              }

              void loop()

              blinker(HIGH);
              delay (1000);
              blinker(LOW);
              delay(1000);

              }

              void blinker(boolean x_1){

              digitalWrite(led_1,x_1);
              }

              The code could also (perhaps more correctly be written 

              void loop() {

              blinker(1);
              delay (1000);
              blinker(0);
              delay(1000);
              }

              void blinker(boolean x_1){

              digitalWrite(led_1,x_1);
              }

              or even like this, depending on your understanding of Boolean logic 

              void loop() {

              blinker(true);
              delay (1000);
              blinker(false);
              delay(1000);
              }

              void blinker(boolean x_1){

              digitalWrite(led_1,x_1);
              }

              • Topic 9

                Iteration (Repeating)

                It is said, if you need to do the same thing twice on a computer, you should use iteration.

                There are many ways to repeat code in programming and there are discussions on the Arduino web site.

                This will concern itself with the for loop

                the syntax is

                for (int i =1; i =5, i++)

                {

                do something 4 times

                }

                /* This demonstrate pwm
                */
                int index = 0;
                int pin_8 = 8;

                void setup()
                {
                pinMode(pin_8, OUTPUT);
                }
                void loop()
                {
                for (index = 0; index < 255; index++ )
                {
                analogWrite(pin_8, index);
                delay(10);
                }
                for (index = 255; index > 0; index-- )
                {
                analogWrite(pin_8, index);
                delay(10);
                }
                }

                Instructables pwm
                • Topic 10

                  https://www.arduino.cc/en/Tutorial/PWM

                  • Topic 11

                    Lesson 7 : A challenge

                    Make the robot run a course
                    • Topic 12

                      Further adventures

                      Once the robot is up and running, there are a number of things you can add.

                      • IR sensors for line following and control using tv remote
                      • sonic sensors to prevent collision

                      One of the great things about Arduinos is that students can use the board for all sorts of experiments

                      • measure temperature
                      • measure light intensity
                      • detect colour

                      Using these, we can log data and the graph it.

                      • Topic 13

                        Getting the robot to go straight : PWM

                        For various reasons, the voltage supplied to each motor is different. There is a way to vary the voltage using pulse width modulation. For a full explanation, check out youtube.

                        The key to understanding PWM is to understand the difference between a digital and analog signal. The commands for each are

                        digitalWrte(ledPin, HIGH);

                        analogWrite(ledPin,i);

                        where i changes from a value of 0 to 255

                        A digital signal in Arduino is either a 5v or 0v prot. An analog signal in Arduino is 5v spread between 0 - 255 steps. Analog can be reprogrammed in pins 3,5,6, 9, 10, 11 or accessed directly with pins A0 - A5.

                        Here is some code that demonstrates the use of PWM using an Arduino, 2 motors and an L298n motor driver.

                         

                        A techy introduction

                        Knowlege : the difference between analog and digital.

                        /* This is a first attempt to get my tank robot to go straight.
                        * It seems to meed more power on the starboard side than the port.
                        * It is usefull to define forward motion in terms of port (left)
                        * and starboard (right).
                        *
                        * This invlved pulse width modulation. not the difference between
                        * analogWrite() and digitalWrite() commands
                        *
                        * Note if the PWM is not high enough, there might not be enough power
                        * to turn over your motors
                        *
                        * mrweber
                        * 14/5/16
                        */


                        // int ena = 4;
                        int int1 = 5;
                        int int2 = 6;

                        // int enb = 8;
                        int int3 =9;
                        int int4 = 10;

                        int timex = 3000; //runs for 3 seconds
                        boolean run;

                        void setup() {
                        //pinMode (ena,OUTPUT);
                        pinMode (int1,OUTPUT);
                        pinMode (int2,OUTPUT);

                        //pinMode (enb,OUTPUT);
                        pinMode (int3,OUTPUT);
                        pinMode (int4,OUTPUT);

                        run = true;
                        }

                        void loop()
                        {
                        delay(3000);
                        if (run)
                        {
                        forward();
                        delay (4000);
                        halt();
                        run = false; // runs only one time
                        }
                        }

                        void forward() // forward sub proceedure
                        {
                        //starboard motor
                        analogWrite(int1,180); // <==== 100% 
                        digitalWrite(int2,LOW);

                        //port motor
                        analogWrite(int3,150); // < === 83% this balances the load
                        analogWrite(int4,LOW);

                        }
                        void halt() // halt sub proceedure
                        {
                        //digitalWrite(ena,HIGH);
                        digitalWrite(int1,HIGH);
                        digitalWrite(int2,HIGH);

                        //digitalWrite(enb,HIGH);
                        digitalWrite(int3,HIGH);
                        digitalWrite(int4,HIGH;

                        }

                        Sparkfun circ-01


                        Sparkfuncode01 

                        Skill : be able to write subroutines in C and call them from the main program.

                         

                        Sparkfun circ-03


                        Sparkfuncode03

                        Extra for experts : servo motors

                        circ-04 


                        Sparkfuncode04

                        The dont argue : relays

                        circ-11


                        Sparkfuncode11 

                        Skill : Be able to search out a datasheet on the web and deduce from information.
                        • Topic 14

                          What I really did


                          /*
                          * A chaser module to test all the outputs are
                          * working as they should. Tested against 4
                          * LEDs

                          sequence (100,0,0,0,500);
                          sequence (0,100,0,0, 500);
                          sequence (0,0,100,0, 500);
                          sequence (0,0,0,100, 500);

                          sequence (0,0,0,150, 3000); //goes right forward
                          sequence (0,0,150,0, 3000); //goes right reverse
                          sequence (0,150,0,0, 3000); //goes left forward
                          sequence (150,0,0,0, 3000); //goes left reverse

                          sequence (0,150,0,150, 3000); //goes all forward
                          sequence (150,0,150,0, 3000); //goes all reverse right
                          sequence (0,150,150,0, 3000); //goes left spin right reverse
                          sequence (150,0,0,150, 3000); //goes right forward left reverse

                          */

                          int int1 = 5;
                          int int2 = 6;

                          int int3 =9;
                          int int4 = 10;

                          int bias = .9; // this straightens the tank up

                          int timex = 2000 ;//runs for 3 seconds
                          boolean run;

                          void setup() {
                          pinMode (int1,OUTPUT);
                          pinMode (int2,OUTPUT);

                          pinMode (int3,OUTPUT);
                          pinMode (int4,OUTPUT);

                          run = true;
                          }

                          void loop() {
                          if (run)
                          {

                          sequence (0,150,0,135, 5000); //goes forward

                          sequence (0,0,0,0,5000);

                          run = false;
                          }

                          }

                          void sequence(int x1,int x2, int x3, int x4, int x5) // sequence sub proceedure
                          {
                          //digitalWrite(ena,HIGH);
                          analogWrite(int1,x1 );
                          analogWrite(int2,x2);

                          //digitalWrite(enb, HIGH);
                          analogWrite(int3,x3 );
                          analogWrite(int4,x4);

                          delay(x5);
                          }

                          void halt()                    // halt sub procedure
                          {
                          //digitalWrite(ena,HIGH);
                          digitalWrite(int1,LOW);
                          digitalWrite(int2,LOW);

                          //digitalWrite(enb,HIGH);
                          digitalWrite(int3,LOW);
                          digitalWrite(int4,LOW);

                          }

                          • Topic 15

                            • Topic 16

                              • Topic 17