Topic outline

  • Topic 1

    Introduction

    • Topic 2

      Use an analog motor and a battery

      Use a simple toy motor and investigate how to get it to spin one way then the other.

      A breadboard might help.

      It is also useful to have some sort of protocol for wiring up the motor. One way is to lay so the contacts are visible then solder the red on the left, like in nautical terms.

      "Is there any port left in the bottle"

      Having a system is omportant then we come to wiring up the H bridge.

      Image result for battery and toy analog motor
      • Topic 3

        Sub system : Controlling motors : l29x

        A motor should be able to spin two ways. To accomplish this, there are a number of ways.

        • transistors
        • h briges
        • relays
        The l293 is a board that enables a motor to spin in two different directions, based on the commands from the Arduino.
        The motor driver is an important part of controlling a robot.
        Note : When we start to add components to the Arduino, we are actually creating systems.

         

        • Topic 4

          Subsystems : programming

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

          }

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

          }

          arduino kde
          • Topic 5

            Mr Weber's heads up : connect the motor driver

            OK

            This is the part where the taklking gets much less and the doing gets more.

            First thing is to get your robot to move in a predictable way.

            Program it to move 

            • forward
            • back
            • left (spin)
            • left (sweep)
            • right (spin)
            • right (sweep)
            • reverse
            • halt

            Later we will add a sonic sensor to see if it can avoid obstacles. 

            lln298nThe L297N H bridge

            Smarty pants kids will by now realise they can find examples on the web as well as code.

            Sites like

            • instructables
            • thingaverse
            • youtube
            will give you ideas.

            There are also
            • personal learning networks (PLN's)
            • forums
            • other kids
            Time to get cracking.



            The next part of the jouney is to go completely rogue and try to figure out how you can control a robot by

            • infra red
            • touch sensors
            • wireless
            • gps
            • anything

            Regard you robot not as a one off device but a starting point o investigate how all this fits together.

            • Topic 6

              Same code passing parameters

              /*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() {
                Serial.begin(9600);
                // put your setup code here, to run once:
                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)
                {
                  for (int count = 1; count < 7;  count++)
                  // make a square
                  {
                    turn(0, 0, 0, 0, 0);
                    delay(delayTime);
                    turn(0, 255, 0, 255, 800);
                    delay(delayTime); 
                    turn(0, 0, 0, 0, 0); // a little breather
                    delay(delayTime); 
                    turn(0, 200, 200, 0, 180);
                    delay(delayTime); 
                    turn(0, 0, 0, 0, 0);    // put on the brakes here
                    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 7

                • Topic 8

                  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 the ena, enb. In the old version of l298, there were 6 inputs. ena and enb
                  are set to HIGH. these can be set by hardware and hence are commented out.

                  Depending on your driver, you can compile these commands or not. Use find
                  and replace to remove the comment out directive if you wish.
                  */

                  // 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() {
                  if (run)
                  {
                  forward(); // sets the robot forward.
                  // check motors are spinning in same direction
                  delay(timex);
                  halt(); // stops the code running
                  run = false;
                  }
                  }

                  void forward() // forward sub proceedure
                  {
                  //digitalWrite(ena,HIGH);
                  digitalWrite(int1,HIGH);
                  digitalWrite(int2,LOW);

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

                  }

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

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

                  }

                  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 9

                    Controlling a robot using parameters

                    /*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() {
                      Serial.begin(9600);
                      // put your setup code here, to run once:
                      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)
                      {
                        for (int count = 1; count < 7;  count++)
                        // make a square
                        {
                          turn(0, 0, 0, 0, 0);
                          delay(delayTime);
                          turn(0, 255, 0, 255, 800);
                          delay(delayTime); 
                          turn(0, 0, 0, 0, 0); // a little breather
                          delay(delayTime); 
                          turn(0, 200, 200, 0, 180);
                          delay(delayTime); 
                          turn(0, 0, 0, 0, 0);    // put on the brakes here
                          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 (60/360*720);
                    }
                    • Topic 10

                      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 ena, enb. In the old version of L298, there were 6 inputs. ena and enb
                      are set to HIGH. these can be set by hardware and hence are commented out.

                      Depending on your driver, you can compile these commands or not. Use find
                      and replace to remove the comment out directive if you wish.
                      */

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

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

                      int timex = 3000; //global variable 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() {
                      if (run)
                      { // parameter passed as a value

                      forward(3000); // sets the robot forward.
                      // check motors are spinning in same direction
                      //delay(timex);
                      halt(); // stops motors running
                      left(3000); // sets the robot forward.
                      // check motors are spinning in same direction
                      //delay(timex);
                      halt(); // stops motors running
                      right(3000); // sets the robot forward.
                      // check motors are spinning in same direction
                      //delay(timex);
                      reverse(3000); // sets the robot forward.
                      // check motors are spinning in same direction
                      //delay(timex);
                      halt(); // stops the motors running
                      run = false; // halts the code
                      }
                      }

                      void reverse(int wait){ // reverse sub procedure
                      //digitalWrite(ena,HIGH);
                      digitalWrite(int1,LOW);
                      digitalWrite(int2,HIGH);

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

                      delay(wait);

                      }
                      void forward(int wait){ // forward sub procedure
                      //digitalWrite(ena,HIGH);
                      digitalWrite(int1,HIGH);
                      digitalWrite(int2,LOW);

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

                      delay(wait);

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

                      //digitalWrite(enb,HIGH);
                      digitalWrite(int3,LOW);
                      digitalWrite(int4,LOW);
                      }
                      void right(int wait){
                      //digitalWrite(ena, HIGH);
                      digitalWrite(int1,LOW);
                      digitalWrite(int2,HIGH);

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

                      delay(wait);
                      }

                      void left(int wait){
                      //digitalWrite(ena, HIGH);
                      digitalWrite(int1,HIGH);
                      digitalWrite(int2,LOW);

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

                      delay(wait);
                      }

                      • Topic 11

                        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,LOW);
                        digitalWrite(int2,LOW);

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

                        }

                        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 12

                          Touch : The touch sensor

                          Image result for arduino collision sensor
                          • Topic 13

                            Sonics : using the HC-SR04 sensor


                            hc-sr04

                            sketch

                            /*
                            HC-SR04 Ping distance sensor]
                            VCC to arduino 5v GND to arduino GND
                            Echo to Arduino pin 13 Trig to Arduino pin 12
                            */

                            #define trigPin 13
                            #define echoPin 12

                            int duration;
                            int distance;

                            void setup() {
                            Serial.begin (9600);
                            pinMode(trigPin, OUTPUT);
                            pinMode(echoPin, INPUT);
                            }

                            void loop() {
                            long duration, distance;
                            digitalWrite(trigPin, LOW);
                            delayMicroseconds(20);
                            digitalWrite(trigPin, HIGH);
                            delayMicroseconds(20);
                            digitalWrite(trigPin, LOW);
                            duration = pulseIn(echoPin, HIGH);
                            distance = (duration/2) / 29.1;

                            Serial.print(distance);
                            Serial.println(" cm");

                            delay(500);
                            }

                            • Topic 14

                              Sonics : Running a robot with sonic sensor

                              The hc-sr04 needs 5 volts to operate. Take the Vcc from the L297d motor driver NOT the Arduino board.

                              /*
                              Mr Weber's fifth robot program.

                              the robot goes forwards for 2 seconds then stops,
                              turns left 90 degrees,stops, then right 90 degrees, stops,
                              then back and stops.

                              hint : The hc-sr04 needs 5 volts to operate. Take the Vcc from the L297d motor driver NOT the Arduino board.

                              author mark weber
                              date 6/4/14
                              */

                              int ena = 3; // these relate to the h bridge (L293 chip)
                              int int1 = 4; // to drive the motors
                              int int2 = 5;

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

                              int trigPin = 12; // check the hc-sr04 sonic sensor
                              int echoPin = 11; // for the pinouts

                              long duration, distance;

                              long x = 0;

                              void setup() {

                              Serial.begin (9600);
                              pinMode(trigPin, OUTPUT);
                              pinMode(echoPin, INPUT);

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


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

                              }

                              void loop()
                              {
                              distance = sonic(x);
                              if (distance >= 200 || distance <= 0){ // testing
                              Serial.println("Out of range main");
                              }
                              else {
                              Serial.print(distance); // function working
                              Serial.println(" cm main");
                              }

                              if (distance > 10)
                              {

                              forward();
                              }
                              else
                              {
                              halt();
                              back();
                              halt();
                              left();
                              }
                              delay(500);
                              }

                              long sonic(long x) // calls the function sonic here
                              {

                              digitalWrite(trigPin, LOW); // Added this line
                              delayMicroseconds(2); // Added this line
                              digitalWrite(trigPin, HIGH);
                              delayMicroseconds(10); // Added this line
                              digitalWrite(trigPin, LOW);
                              duration = pulseIn(echoPin, HIGH);
                              distance = (duration/2) / 29.1;

                              if (distance >= 200 || distance <= 0){
                              Serial.println("Out of range");
                              }
                              else {
                              Serial.print(distance);
                              Serial.println(" cm");
                              }
                              delay(500);
                              return distance;
                              }


                              void halt()
                              {
                              digitalWrite(ena,HIGH);
                              digitalWrite(int1,LOW);
                              digitalWrite(int2,LOW);
                              digitalWrite(enb,HIGH);
                              digitalWrite(int3,LOW);
                              digitalWrite(int4,LOW);
                              delay(500);
                              }


                              void back()
                              {
                              digitalWrite(ena, HIGH);
                              digitalWrite(int1,LOW);
                              digitalWrite(int2,HIGH);
                              digitalWrite(enb,HIGH);
                              digitalWrite(int1,LOW);
                              digitalWrite(int4,HIGH);
                              delay(500);
                              }

                              void left()
                              // goes right
                              {
                              digitalWrite(ena, HIGH);
                              digitalWrite(int1,LOW);
                              digitalWrite(int2,HIGH);
                              digitalWrite(enb, HIGH);
                              digitalWrite(int3,HIGH);
                              digitalWrite(int4,LOW);
                              delay(500);
                              }

                              void right()
                              // goes backwards
                              {

                              digitalWrite(ena, HIGH);
                              digitalWrite(int1,HIGH);
                              digitalWrite(int2,LOW);
                              digitalWrite(enb, HIGH);
                              digitalWrite(int3,LOW);
                              digitalWrite(int4,HIGH);

                              }

                              void forward()
                              {
                              digitalWrite(ena, HIGH);
                              digitalWrite(int1,HIGH);
                              digitalWrite(int2,LOW);
                              digitalWrite(enb, HIGH);
                              digitalWrite(int3,HIGH);
                              digitalWrite(int4,LOW);
                              delay(500); // this is shorter
                              }

                              • Topic 15

                                Sonics :Putting it together : Obstacle avoiding code

                                One of the great strengths and weaknesses of the robot is that it only processes on series of commands at a time.

                                One way to get around this is to execute a series of commands very quickly to give the appearence of them working together. So by setting the motors forward ever 30/1000 of a second then moving the servo you can give the appearence of the system working harmonously.

                                Pulse width modulation works in this way. 

                                /*
                                This uses the above code to turn a servo through
                                65 - 114 degrees

                                this can now be put in as part of a loop to run in conjuction
                                with a motorised robot

                                mrweber
                                261115
                                */

                                #include <Servo.h>
                                Servo myservo;

                                int count = 45;
                                int sentinal =1;

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

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

                                #define trigPin 13
                                #define echoPin 12

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

                                pinMode(trigPin, OUTPUT);
                                pinMode(echoPin, INPUT);
                                myservo.attach(3);
                                }

                                void loop() {

                                forward (30);
                                sg90();
                                delay(20);
                                pinger();

                                }

                                void pinger() {
                                long duration, distance;
                                digitalWrite(trigPin, LOW); // Added this line
                                delayMicroseconds(2); // Added this line
                                digitalWrite(trigPin, HIGH);
                                delayMicroseconds(10);
                                digitalWrite(trigPin, LOW);
                                duration = pulseIn(echoPin, HIGH);
                                distance = (duration/2) / 29.1;  // there and back is 2*distance
                                Serial.println(distance);
                                if (distance < 10){
                                halt(100);
                                reverse(1000);
                                halt(100);
                                right(500);
                                halt(100);
                                }
                                }
                                void reverse(int wait){ // reverse sub procedure
                                //digitalWrite(ena,HIGH);
                                digitalWrite(int1,LOW);
                                digitalWrite(int2,HIGH);

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

                                delay(wait);
                                }

                                void forward(int wait){ // forward sub procedure
                                //digitalWrite(ena,HIGH);
                                digitalWrite(int1,HIGH);
                                digitalWrite(int2,LOW);

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

                                delay(wait);

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

                                //digitalWrite(enb,HIGH);
                                digitalWrite(int3,LOW);
                                digitalWrite(int4,LOW);
                                delay(wait);
                                }


                                void right(int wait){
                                //digitalWrite(ena, HIGH);
                                digitalWrite(int1,LOW);
                                digitalWrite(int2,HIGH);

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

                                delay(wait);
                                }

                                void left(int wait){
                                //digitalWrite(ena, HIGH);
                                digitalWrite(int1,HIGH);
                                digitalWrite(int2,LOW);

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

                                delay(wait);
                                }


                                void sg90(){
                                // here increases count
                                if ((sentinal == 1) && (count <=114)){
                                count = count + 1; // loop increments to 65
                                sentinal = 1; // sentinal
                                myservo.write(count);
                                }
                                else {
                                sentinal = -1;
                                }
                                // changes over and decreases count
                                if ((sentinal == -1) && (count >= 60) ){
                                count = count - 1;
                                sentinal = -1; // sentinal
                                myservo.write(count);
                                }
                                else {
                                sentinal = 1;
                                }
                                }

                              • Topic 16

                                IR detector

                                ir sensor

                                TCRT5000


                                tcrt5000

                                //this is a program to test if we can read a sensor
                                //we used a tcrt5000 sensor i bought off alibaba
                                //in a set with an ir led
                                //the key to the exercise was the balance between the two resistors in the circuit diagram

                                int sensor; // use an integer and read it continually

                                void setup()
                                {
                                Serial.begin(9600); //read serial port
                                }

                                void loop()
                                {
                                sensor=analogRead (A0); // note use of Arduino analog port
                                Serial.println(sensor);
                                delay(1000);

                                if (sensor < 500)
                                {
                                Serial.println("hello less than 500, it must be light");
                                }
                                else
                                {
                                Serial.println("we are in the dark, greater than 500");
                                }
                                }

                                Data sheet
                                • Topic 17

                                  Line following robot

                                   If you are using 1 sensor, then your code should be something like:
                                  Void loop {
                                      If(sensorvalue  < threshold){
                                          Turn left
                                      };
                                      If(sensorvalue > threshold){
                                          Turn right
                                      };
                                  }
                                  This will cause your robot to wiggle side to side while riding the edge of the line.
                                  Line following robot
                                  • Topic 18

                                    Sample code for line following robot

                                     

                                    /*
                                    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 20

                                      Line avoiding robot

                                       

                                      /*
                                      Robot 1

                                      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 the ena, enb. In the old version of l298, there were 6 inputs. ena and enb
                                      are set to HIGH. these can be set by hardware and hence are commented out.

                                      Depending on your driver, you can compile these commands or not. Use find
                                      and replace to remove the comment out directive if you wish.
                                      */

                                      // 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() {
                                      if (run)
                                      {
                                      forward(); // sets the robot forward. 
                                      // check motors are spinning in same direction
                                      delay(timex);
                                      halt(); // stops the code running
                                      run = false;

                                      }

                                      void forward() // forward sub proceedure
                                      {
                                      //digitalWrite(ena,HIGH);
                                      digitalWrite(int1,HIGH);
                                      digitalWrite(int2,LOW);

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

                                      }

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

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

                                      }

                                      • Topic 21

                                        Using an infra red remote

                                          Instructable IR Remote
                                        • Topic 22

                                          Code alert : global variables

                                           

                                          /*
                                          This piece of code demonstrates the use of global variables. Global variables maintain their assigned value anywhere in the code. If it changes in a function, it retains its value.

                                          Some things to be concidered

                                          1. arduino deals with functions not subprocedures
                                          2. a global variable is any variable declared *outside* a function
                                          3. a counter can increment to keep track of the number of times code runs.
                                          4. a sentinal value can be used to a switch between code fragments

                                          mrweber
                                          261115
                                          */

                                          int count = 0 ;  // declared outside function
                                          int sentinal = 1;

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

                                          void loop() {
                                          sg90();
                                          Serial.print(count);
                                          Serial.print(" ");
                                          Serial.println(sentinal);
                                          delay(2000);
                                          }

                                          void sg90(){
                                          count = count + 1; // increments here
                                          if (count <= 10){
                                          sentinal = 1; // sentinal
                                          }

                                          if (count > 10){
                                          sentinal = -1;// sentinal changes
                                          }

                                          if (count > 20){ // all is reset
                                          count = 0;
                                          sentinal = 1;
                                          }
                                          }

                                          • Topic 24

                                            Code alert : back and forward

                                             

                                            /*
                                            This piece of code demonstrates the use of global variables. Global variables
                                            maintain their assigned value anywhere in the code. If it changes in a function, it
                                            retains its value.

                                            Some things to be concidered

                                            1. arduino deals with functions not subprocedures
                                            2. a global variable is any variable declared *outside* a function
                                            3. a counter can increment to keep track of the number of times code runs.
                                            4. a sentinal value can be used to a switch between code fragments

                                            mrweber
                                            261115
                                            */

                                            int count = 45;
                                            int sentinal =1;

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

                                            void loop() {
                                            sg90();
                                            Serial.print(count);
                                            Serial.print(" ");
                                            Serial.println(sentinal);
                                            delay(1000);
                                            }

                                            void sg90(){
                                            // here increases count
                                            if ((sentinal == 1) && (count <=64)){
                                            count = count + 1; // loop increments to 65
                                            sentinal = 1; // sentinal
                                            }
                                            else {
                                            sentinal = -1;
                                            }
                                            // changes over and decreases count
                                            if ((sentinal == -1) && (count >= 46) ){
                                            count = count - 1;
                                            sentinal = -1; // sentinal
                                            }
                                            else {
                                            sentinal = 1;
                                            }
                                            }

                                            • Topic 25

                                              Running a servo motor

                                               Servo motors (or servos) are self-contained electric devices that rotate or push parts of a machine with great precision. Servos are found in many places: from toys to home electronics to cars and airplanes.

                                               It consists of a motor coupled to a sensor for position feedback.  It also requires a servo drive to complete the system. The drive uses the feedback sensor to precisely control the rotary position of the motor.

                                              The motor is run by the red and black/brown wires and the feedback sensor is the orange.

                                              Arduino tutorial

                                              /* Sweep
                                              modified 8 Nov 2013
                                              by Scott Fitzgerald
                                              http://www.arduino.cc/en/Tutorial/Sweep
                                              */

                                              #include <Servo.h>

                                              Servo myservo; // create servo object to control a servo
                                              // twelve servo objects can be created on most boards


                                              int servoPin = 3;

                                              int pos = 90; // variable to store the servo position

                                              void setup() {
                                              myservo.attach(servoPin); // attaches the servo pin to the servo object

                                              }

                                              void loop() {
                                              for (pos = 45; pos <= 135; pos += 1) { // goes from 0 degrees to 180 degrees
                                              // in steps of 1 degree
                                              myservo.write(pos); // tell servo to go to position in variable 'pos'
                                              delay(15); // waits 15ms for the servo to reach the position
                                              }
                                              for (pos = 135; pos >= 45; pos -= 1) { // goes from 180 degrees to 0 degrees
                                              myservo.write(pos); // tell servo to go to position in variable 'pos'
                                              delay(15); // waits 15ms for the servo to reach the position
                                              }
                                              }

                                              servo
                                              • Topic 26

                                                Code alert : turning a servo using counter and a sentinal values

                                                 

                                                /*
                                                This uses the above code to turn a servo through
                                                45 - 135 degrees

                                                this can now be put in as part of a loop to run in conjuction
                                                with a motorised robot

                                                mrweber
                                                261115
                                                */

                                                #include <Servo.h>
                                                Servo myservo;

                                                int count = 45;
                                                int sentinal =1;

                                                void setup() {
                                                // put your setup code here, to run once:
                                                Serial.begin(9600);
                                                myservo.attach(3);
                                                }
                                                void loop() {
                                                sg90();
                                                delay(50);
                                                }
                                                void sg90(){
                                                // here increases count
                                                if ((sentinal == 1) && (count <=134)){
                                                count = count + 1; // loop increments to 65
                                                sentinal = 1; // sentinal
                                                myservo.write(count);
                                                }
                                                else {
                                                sentinal = -1;
                                                }
                                                // changes over and decreases count
                                                if ((sentinal == -1) && (count >= 46) ){
                                                count = count - 1;
                                                sentinal = -1; // sentinal
                                                myservo.write(count);
                                                }
                                                else {
                                                sentinal = 1;
                                                }
                                                }

                                                • Topic 27

                                                  • Topic 28

                                                    • Topic 29