Thumbwheel as Input

Using the Thumbwheel as input for the Buzzer


When we finished learning about the Buzzer, we ended with how to play a song. The song had a set tempo (speed at which it plays), but what if you wanted to be able to change the tempo of the song without having to go back into the program and code a different tempo each time? We are going to use the thumbwheel as an input for the tempo, which will allow you to speed up the song, or slow it down, just by moving the thumbwheel.

Recall the program we wrote to play a song in the Introduction to the Buzzer tutorial:

 

import com.ridgesoft.intellibrain.*;

import com.ridgesoft.io.*;

import com.ridgesoft.robotics.*;



public class Advance_Buzzer {

private static Display screen = IntelliBrain.getLcdDisplay();

private static Speaker buzzer = IntelliBrain.getBuzzer();

//Music notes

public static final int C4 = 262;

public static final int C4_SHARP = 277;

public static final int D4 = 294;

public static final int D4_SHARP = 311;

public static final int E4 = 330;

public static final int F4 = 349;

public static final int F4_SHARP = 370;

public static final int G4 = 392;

public static final int G4_SHARP = 415;

public static final int A4 = 440;

public static final int A4_SHARP = 466;

public static final int B4 = 494;

public static final int C5 = 523;

public static final int C5_SHARP = 554;

public static final int D5 = 587;

public static final int D5_SHARP = 622;

public static final int E5 = 659;

public static final int F5 = 698;

public static final int F5_SHARP = 740;

public static final int G5 = 784;

public static final int G5_SHARP = 831;

public static final int A5 = 880;

public static final int A5_SHARP = 932;

public static final int B5 = 988;

//Tempo

public static int Whole = 1200;

public static void main(String[] args) {

              play_song();

}



	public static void play_song(){

		int Half = Whole / 2, Quarter = Whole / 4;

                             screen.print(0, "Little Lamb"); 

                             buzzer.play(B4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(G4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(B4, Quarter);

              buzzer.play(B4, Quarter);

              buzzer.play(B4, Half);

              buzzer.play(A4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(A4, Half);

              buzzer.play(B4, Quarter);

              buzzer.play(D5, Quarter);

              buzzer.play(D5, Half);

              buzzer.play(B4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(G4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(B4, Quarter);

              buzzer.play(B4, Quarter);

              buzzer.play(B4, Quarter);

              buzzer.play(B4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(B4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(G4, Whole);

}

}



Let's expand our exsiting code to let the user be able to change the tempo. First lets add the following code into the main method so that your main looks like this:


public static void main(String[] args) {

        while(true){

             tempo();

             play_song();

         }

    }



All we did was add an additional method and a while(true) loop


Above the main method add the following code:


private static AnalogInput thumbwheel = IntelliBrain.getThumbWheel();

• This calls in the thumbwheel to we can choose a tempo speed • You also want to change the whole note from equaling 1200 to equaling 0

In the new method we created called “tempo()” add the following code:

 

double start, end, time = 0;

int hold = 0;

start = System.currentTimeMillis();

while (time < 10) {

            screen.print(0, "Use Thumbwheel");

            hold = thumbwheel.sample();

            screen.print(1, "Tempo: " + hold);

            end = System.currentTimeMillis();

            time = ((int) (end - start) / 1000);

}



Whole = hold;



11. Once you have completed these changes run the code again it should run something like the video below. If for some reason it does not go back through your code and make sure you copied all the parts correctly • Note what happened when you changed the number from a low number to a high number • Try changing the values of the tempo with the thumb wheel and see what happens 12. You can change the songs played by the robot by finding music online and writing the song in the code like the example above

 

import com.ridgesoft.intellibrain.*;

import com.ridgesoft.io.*;

import com.ridgesoft.robotics.*;



public class Advance_Buzzer {

private static Display screen = IntelliBrain.getLcdDisplay();

private static Speaker buzzer = IntelliBrain.getBuzzer();

//Music notes

public static final int C4 = 262;

public static final int C4_SHARP = 277;

public static final int D4 = 294;

public static final int D4_SHARP = 311;

public static final int E4 = 330;

public static final int F4 = 349;

public static final int F4_SHARP = 370;

public static final int G4 = 392;

public static final int G4_SHARP = 415;

public static final int A4 = 440;

public static final int A4_SHARP = 466;

public static final int B4 = 494;

public static final int C5 = 523;

public static final int C5_SHARP = 554;

public static final int D5 = 587;

public static final int D5_SHARP = 622;

public static final int E5 = 659;

public static final int F5 = 698;

public static final int F5_SHARP = 740;

public static final int G5 = 784;

public static final int G5_SHARP = 831;

public static final int A5 = 880;

public static final int A5_SHARP = 932;

public static final int B5 = 988;

//Tempo

public static int Whole = 1200;

private static AnalogInput thumbwheel = IntelliBrain.getThumbWheel();



public static void main(String[] args) {

               while(true){

             tempo();

             play_song();

}



	public static void play_song(){

		int Half = Whole / 2, Quarter = Whole / 4;

                             screen.print(0, "Little Lamb"); 

                             buzzer.play(B4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(G4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(B4, Quarter);

              buzzer.play(B4, Quarter);

              buzzer.play(B4, Half);

              buzzer.play(A4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(A4, Half);

              buzzer.play(B4, Quarter);

              buzzer.play(D5, Quarter);

              buzzer.play(D5, Half);

              buzzer.play(B4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(G4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(B4, Quarter);

              buzzer.play(B4, Quarter);

              buzzer.play(B4, Quarter);

              buzzer.play(B4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(B4, Quarter);

              buzzer.play(A4, Quarter);

              buzzer.play(G4, Whole);

}



    public static void tempo() {



    double start, end, time = 0;

    int hold = 0;

    start = System.currentTimeMillis();



    while (time < 10) {

            screen.print(0, "Use Thumbwheel");

            hold = thumbwheel.sample();

            screen.print(1, "Tempo: " + hold);

            end = System.currentTimeMillis();

            time = ((int) (end - start) / 1000);

    }



Whole = hold;

}

}



More Using the Thumbwheel as input for the Buzzer


  1. What happens as you move the thumbwheel up and down?
  2. Can you make it perform in the opposite manner (slow down as the thumbwheel increases as opposed to getting faster)?
  3. Can you make it play a different song?

Using the Thumbwheel as input for the Motors


You do not have to predefine the values for the speed of the motors and hard code them when you initially write your code. You can use other input to set the speed of the motors as your program is working. In this example we will use the moving in a circle code to allow us to use a while(true) loop (minimizing the risk of running into anything as the program runs) and the thumbwheel to set the speed of the motor. Before you look at the code, how do you think we will accomplish this task?


//code  MoveInCircleWithThumbwheel


Definitely a little bit more involved code, right? However, all we did was use programming topics we had learned before and combined them in a new way. Remember, the program runs line by line (see Introduction to Basic Programming Tutorial link) and no matter how complex a problem seems, it can (and to program, it must) be broken down into a series of step by step instructions.


More Using the Thumbwheel as input for the Motors



  1. What was the difference between this code and the previous ones?

  2. What happens if you turn the thumbwheel in one direction? What about the other?

  3. Which direction is your robot turning? Can you make it turn the other way?

  4. Can you think of reasons being able to have the robot move in different ways according to an input be beneficial?