Topic outline

  • General

  • Introduction


     Here are some of the ideas for the ITS display. The plan is to decide which project is for you and what problem you would like to solve.
    • Code alert : Branching with if ... else


      /* this explores how to change the value of a variable
      * write a condition statement (if (condition) then change
      * the code processed.
      * Also note the use of Serial.print, Serial.println
      *
      * mark weber
      * 08/06/2016
      */

      int variable = 1;
      void setup() {

      Serial.begin(9600);
      }

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

      if (variable == 1)
      {
      Serial.print ("variable is one ==>");
      Serial.println(variable);
      variable = variable +1;
      delay(500);
      }

      else
      {
      Serial.print ("variable two is ==>");
      Serial.println(variable);
      variable = variable -1;
      delay(500);
      }
      }

      • 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);

        }

        • Obstacle avoiding robot

          This code is designed to demonstrate how a robot can be used using digital outputs

          See the next module for a much more elegant solution to the problem.

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


          #define echoPin 13
          #define trigPin 12

          // 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);

          Serial.begin (9600);

          pinMode(trigPin, OUTPUT);
          pinMode(echoPin, INPUT);

          }


          void loop() {
          // parameter passed as a value
          forward(100); // sets the robot forward.
          pinger(); // checks for obstacle
          }

          void pinger() {
          long duration, distance;
          digitalWrite(trigPin, LOW);
          delayMicroseconds(2);
          digitalWrite(trigPin, HIGH);
          delayMicroseconds(10);
          digitalWrite(trigPin, LOW);
          duration = pulseIn(echoPin, HIGH);
          distance = (duration/2) / 29.1; // converts pulse to cms
          Serial.println(distance);
          if (distance < 15)
          {
          halt(100);
          reverse(1000);
          halt(100);
          right(500);
          halt(100);
          }
          }

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

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

          delay(wait);
          }

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

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

          delay(wait);

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

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


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

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

          delay(wait);
          }

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

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

          delay(wait);
          }

          • Topic 5

            Obstacle avoiding robot

             With the use of parameter passing and the use of a function, the code above can be made much more elegant.

            Also, the use of analog write allow as us to vary the speed of the each wheel

            Note to the wise.

            When using batteries and an L297, you cn tap straight of the l297 vcc for the input for the Arduino.

            Sometimes, the 5 volt output is not enough to run the various inputs.


            /*
            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, 50 0);
            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

            */

            #define trigPin 13
            #define echoPin 12

            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

            int T =100;

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

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

            pinMode(trigPin, OUTPUT);
            pinMode(echoPin, INPUT);

            Serial.begin(9600);
            }

            void loop() {

            if (T > 10)
            {
            Serial.print("Forward");
            sequence (0, 150, 0, 130, 50);

            } //goes forward
            else
            {
            Serial.println("Halt");
            sequence (0, 0, 0, 0, 500);
            sequence (0, 0, 120, 10, 1000);
            }
            T = trigger();
            Serial.println(T);
            }

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

            analogWrite(int3, x3 );
            analogWrite(int4, x4);
            delay(x5);
            }

            int trigger()
            // reads hc-05 sonic sensor
            {
            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;
            int d1 = distance;
            return (d1);
            }

            • Topic 6

              Road signs

               Use either Arduino or Raspberry Pi to broadcast changes in road conditions.
              • Topic 7

                Controlling a robot using : Touch sensor

                 
                • Topic 8

                  Controlling a robot by : wireless

                  How long do you run the motors to get the robot to move 1 meter
                  How do you get the robot to turn left of right 90 degrees.
                  • Topic 9

                    Controlling a robot using : Bluetooth

                     Can you run a robot from an android phone
                    • Topic 10

                      Controlling a robot using : Xbee

                       Get the robot to run through an obstacle course.
                      • Topic 11

                        Pulse width modulation

                         Get your robot to speed up and slow down using PWM.
                        • Topic 12

                          Controlling a robot using : IR

                          • Topic 13

                            Controlling a robot using : keypad

                            • Topic 14

                              Controlling a robot by following a line

                              • Topic 15

                                IR sensor 2

                                 get your robot to speed up and slow down according to the shade of colour under your robot
                                • Topic 16

                                  Controlling a robot using : TV remote 

                                   Can you start your car with a TV remote?

                                  Can you drive it using a TV remote

                                  Can you run a robot from a computer keyboard.
                                  • Topic 17

                                    Controlling a robot using : Lazer beams

                                     Can you identify a robot from a bar code and an IR sensor
                                    Can you count the number of times a robot goes past,
                                    • Topic 18

                                      Sonic sensor

                                       Can you detect walls using a sonic sensor
                                      Can you use a robot with a sonic sensor and servo to detect other robots
                                      • Topic 19

                                        GPS

                                         Can you determine where a robot is and where it is going using GPS.
                                        • Topic 20

                                          Wifi

                                           Can two robots communicate via wifi.
                                          • Topic 21

                                            Turtle

                                             Get a robot to respond to a keypad and use a pen to draw lines.
                                            • Topic 22

                                              Libraries

                                              • Topic 23

                                                Robotic arm

                                                 Add a robotic arm to your robot
                                                • Topic 24

                                                  Speed camera

                                                   Determine the speed of a robot
                                                  • Topic 25

                                                    Stop lights

                                                     Can a robot recognise the colour on stop light and decide to stop or go.
                                                    • Topic 26

                                                      • Topic 27

                                                        • Topic 28

                                                          • Topic 29

                                                            Adding a sensor : IR reflective

                                                             
                                                            • Topic 30

                                                              Controlling robot speed

                                                              Web site