Incorporating the Buzzer

Incorporating the Buzzer

Another use of the Buzzer is to incorporate it with the other sensors to provide an audio cue. For example, you can have the robot beep if a sensor detects that an object is too close, or you can have it play a victory song when it finishes a task. We are now going to look into how we can incorporate the buzzer into your program to do just that sort of thing. Let's create a new project named "SonarBeep" and import the libraries we need.


import com.ridgesoft.intellibrain.*;

import com.ridgesoft.io.*;

import com.ridgesoft.robotics.*;



public class SonarBeep {



    public static void main(String[] args) {

   

        }

    }



Now let's create the display, buzzer, motors and associated variables we need to be able to use them in our program.


import com.ridgesoft.intellibrain.*;

import com.ridgesoft.io.*;

import com.ridgesoft.robotics.*;



public class SonarBeep {



    private static Display screen = IntelliBrain.getLcdDisplay();

    private static Speaker buzzer = IntelliBrain.getBuzzer();

    private static IntelliBrainDigitalIO pingPort = IntelliBrain.getDigitalIO(3);

    private static Motor rightMotor = new ContinuousRotationServo (IntelliBrain.getServo(2), true, 14);

    private static Motor leftMotor = new ContinuousRotationServo (IntelliBrain.getServo(1), false, 14);

    public static int SonarMeasurement = 0;

    public static boolean hold = false;



    public static void main(String[] args) {

    

    }

}

  1. Note that the first five have “private static” and that the last two have “public static” at the beginning for now it’s important to know that for the most part all predefined classes have “private static”

  2. A simple way to think of which type you should use (Public or Private) is simple if you are using more than one method and are planning to use the same variable in both methods or hand a number back and forth then use Public else use private

  3. You may notice something called a “boolean hold = false” this is another type used in programing it can hold ether true or false you will see how we use it in a bit

  4. Also notice that the integer and boolean value is set to something this is very important you should always set every variable equal to a value when you declare it

In the “Main Method” we will add code to allow us to perform the task we are wanting to do:


import com.ridgesoft.intellibrain.*;

import com.ridgesoft.io.*;

import com.ridgesoft.robotics.*;



public class SonarBeep {



    private static Display screen = IntelliBrain.getLcdDisplay();

    private static Speaker buzzer = IntelliBrain.getBuzzer();

    private static IntelliBrainDigitalIO pingPort = IntelliBrain.getDigitalIO(3);

    private static Motor rightMotor = new ContinuousRotationServo (IntelliBrain.getServo(2), true, 14);

    private static Motor leftMotor = new ContinuousRotationServo (IntelliBrain.getServo(1), false, 14);

    public static int SonarMeasurement = 0;

    public static boolean hold = false;



    public static void main(String[] args) {

        while (true) {

            sonarTest();

            if(hold == true){

                	rightMotor.setPower(0);

               	 leftMotor.setPower(0);

               	 buzzer.beep();

              	  	 screen.print(0, "Object in way");

                	screen.print(1, " " + SonarMeasurement);

            }else{

                	rightMotor.setPower(10);

                	leftMotor.setPower(10);

                	screen.print(0, "going forward");

                	screen.print(1, "dist.: " + SonarMeasurement);

            }

        }

    }



Right after the start of the while loop you see “sonarTest();” this is a method declaration that is referring to method we will create in the next step. Methods are good to use when it simplifies the code for the reader and also for debugging the code later (You can refer to the Introduction to Methods tutorial for more information on methods and how to create them). You can also see the boolean value, hold, in the if statement. We've touched on boolean values before, but if you need a refresher, go here (//link). Let's create our method SonarTest() now.


import com.ridgesoft.intellibrain.*;

import com.ridgesoft.io.*;

import com.ridgesoft.robotics.*;



public class Middle_Buzzer {



    private static Display screen = IntelliBrain.getLcdDisplay();

    private static Speaker buzzer = IntelliBrain.getBuzzer();

    private static IntelliBrainDigitalIO pingPort = IntelliBrain.getDigitalIO(3);

    private static Motor rightMotor = new ContinuousRotationServo (IntelliBrain.getServo(2), true, 14);

    private static Motor leftMotor = new ContinuousRotationServo (IntelliBrain.getServo(1), false, 14);

    public static int SonarMeasurement = 0;

    public static boolean hold = false;



    public static void main(String[] args) {

        while (true) {

            sonarTest();

            if(hold == true){

                	rightMotor.setPower(0);

               	 leftMotor.setPower(0);

               	 buzzer.beep();

              	  	 screen.print(0, "Object in way");

                	screen.print(1, " " + SonarMeasurement);

            }else{

                	rightMotor.setPower(10);

                	leftMotor.setPower(10);

                	screen.print(0, "going forward");

                	screen.print(1, "dist.: " + SonarMeasurement);

            }

        }

    }



    public static void sonarTest() {

        pingPort.enablePulseMeasurement(true);

        try {

            pingPort.pulse(20);

            Thread.sleep(50);

            SonarMeasurement = pingPort.readPulseDuration();

            if(SonarMeasurement >= 700)

                	hold = false;

            else

                	hold = true;

        } catch (Throwable t) {

            t.printStackTrace();

        }

    }

}

You may have noticed two blocks you may have not seen before, a try{ } block and a catch{ } block. Try/Catch blocks are used for exception handling in programs. Exceptions are essentially errors that occur while your program is running, however, you are often aware that particular issues may arise and you provide your program with a way to "handle" these "exceptional" situations (see what they did there?). How a try/catch works is the program "tries" the code in the try block, if no exception is raised it functions normally. If an exception does occur, it "throws" an exception which is then "caught" by the catch block and the code in the catch block is then executed, and the program will continue to run normally instead of crashing.

More Incorporating the Buzzer

  1. Does the robot stop if it detects an object in front?

  2. What happens if you change the sonar value from 700 to 500?

  3. What happens if you change the sonar value from 700 to 900?

  4. What would you do if you wanted to use an actual value for distance instead of 700?