Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

a BUSY BOX FOR TODDLERS

Status
Not open for further replies.

MrDEB

Well-Known Member
Been hacking at this code with LOTS of help from Tumbleweed. Just need to place in enclosure and send to S. Cal
code is written in Swordfish basic
Thanks again Tumbleweed
Code:
{
*****************************************************************************
*  Name    : UNTITLED.BAS                                                   *
*  Author  : [select VIEW...EDITOR OPTIONS]                                 *
*  Notice  : Copyright (c) 2021 [select VIEW...EDITOR OPTIONS]              *
*          : All Rights Reserved                                            *
*  Date    : 8/15/2021                                                      *
*  Version : 1.0                                                            *
*  Notes   : grandson Drews busy box December 2021                                                               *
*          :                                                                *
*****************************************************************************
}
// RTTTL example (RingTone Text Transfer Language)
// uses tone module for generating notes
Program RTTTL_events_example

Device = 18F43K22
Clock = 16
// int osc and IO pin libraries
Include "intosc.bas"

#option DIGITALIO_INIT = true       // automatically call setalldigital
#option DIGITALIO_SLEWRATE = 1      // 1=slow (we don't need fast edges)
Include "setdigitalio.bas"

#option LCD_DATA = PORTD.4
#option LCD_RS = PORTD.2
#option LCD_EN = PORTD.3
Include "LCD.bas"
Include "convert.bas"
Include "utils.bas"



// tone library
// specify output pin
#option TONE_OUTPUT_PIN = PORTA.0
// wait flag setting (default = false)
#option TONE_WAIT = true
Include "tone.bas"

// RTTTL library (uses tone library)
Include "rtttl.bas"


// 8 leds... C, D, E, F, G, A, B, and spare
#option _LED_PORT = PORTC
#option _LED_PORT_TRIS = GetTris(_LED_PORT)

Dim LED_PORT As _LED_PORT,
    LED_PORT_TRIS As _LED_PORT_TRIS,
    LED_C As LED_PORT.0,     // led 1
    LED_D As LED_PORT.1,     // led 2
    LED_E As LED_PORT.2,     // led 3
    LED_F As LED_PORT.3,     // led 4
    LED_G As LED_PORT.4,     // led 5
    LED_A As LED_PORT.5,     // led 6
    LED_B As LED_PORT.6,     // led 7
    LED_8 As LED_PORT.7      // led 8 (outside RTTTL range)
 
 Dim counter0 As Byte
 Dim counter1 As Byte
 Dim counter2 As Byte   
 Dim counter3 As Byte
 Dim counter4 As Byte   
 Dim counter5 As Byte
 Dim counter6 As Byte   
 Dim counter7 As Byte
 dim counter8 as byte   

 dim PinIsHigh as PORTB.Booleans(0)

// tone events
// tone start - turn on leds
// there are 12 notes in a scale, but there are only 8 leds
// combine the notes and sharps together into a single led (ie, C or C# -> LED_C, G or G# -> LED_G, etc)
// since RTTTL supports 4 scales (C4 to C7) combine all the scales into one
// so that a C played in any scale turns on LED_C
// for any notes outside the 4 scales covered by RTTTL it turns on LED_8
Event tone_start_event()
    Dim freq As Word
    
    // get the current freq
    freq = tone.freq()

    // put all RTTTL supported notes into the same scale (scale 4)
    If (freq > NOTE_B6) Then
        freq = freq >> 3            // divide by 8... scale 7 -> 4
    ElseIf (freq > NOTE_B5) Then
        freq = freq >> 2            // divide by 4... scale 6 -> 4
    ElseIf (freq > NOTE_B4) Then
        freq = freq >> 1            // divide by 2... scale 5 -> 4
    EndIf

    // determine which led to turn on
    Select (freq)
        Case > NOTE_B4      // above B7... lite spare LED
            LED_8 = 1
        Case > NOTE_AS4     // B
            LED_B = 1
        Case > NOTE_GS4     // A and A#
            LED_A = 1
        Case > NOTE_FS4     // G and G#
            LED_G = 1                       
        Case > NOTE_E4      // F and F#
            LED_F = 1
        Case > NOTE_DS4     // E
            LED_E = 1
        Case > NOTE_CS4     // D and D#
            LED_D = 1
        Case > NOTE_B3      // C and C#
            LED_C = 1
        Else
            LED_8 = 1       // below C4/unknown... lite spare LED
    End Select                                                                   
End Event

// tone stop - turn off leds
Event tone_stop_event()
    LED_PORT = 0        // turn off all leds
End Event



// some example RTTTL tunes
//swt1
Public Const HappyBday = "HappyBday:d=4,o=5,b=125:8d.,16d,e,d,g,2f#,8p, 8d.,16d,e,d,a,2g,8p, 8d.,16d,d6,b,g,f#,2e,8p,8c6.,16c6,b,g,a,2g"
Public Const Simpsons = "The Simpsons:d=4,o=5,b=160:c.6,e6,f#6,8a6,g.6,e6,c6,8a,8f#,8f#,8f#,2g,8p,8p,8f#,8f#,8f#,8g,a#.,8c6,8c6,8c6,c6"
//swt2
Public Const IndianaJones = "IndianaJones:d=4,o=5,b=250:e,8p,8f,8g,8p,1c6,8p.,d,8p,8e,1f,p.,g,8p,8a,8b,8p,1f6,p,a,8p,8b,2c6,2d6,2e6,e,8p,8f,8g,8p,1c6,p,d6,8p,8e6,1f.6,g,8p,8g,e.6,8p,d6,8p,8g,e.6,8p,d6,8p,8g,f.6,8p,e6,8p,8d6,2c6"
Public Const Muppets = "Muppets:d=4,o=5,b=250:c6,c6,a,b,8a,b,g,p,c6,c6,a,8b,8a,8p,g.,p,e,e,g,f,8e,f,8c6,8c,8d,e,8e,8e,8p,8e,g,2p,c6,c6,a,b,8a,b,g,p,c6,c6,a,8b,a,g.,p,e,e,g,f,8e,f,8c6,8c,8d,e,8e,d,8d,c"
 //swt3
Public Const LooneyTunes = "Looney:d=4,o=5,b=140:32p,c6,8f6,8e6,8d6,8c6,a.,8c6,8f6,8e6,8d6,8d#6,e.6,8e6,8e6,8c6,8d6,8c6,8e6,8c6,8d6,8a,8c6,8g,8a#,8a,8f"
Public Const StarWars = "StarWars:d=4,o=5,b=35:32p,32f#,32f#,32f#,8b.,8f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32e6,8c#.6,32f#,32f#,32f#,8b.,8f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32c#6,8b.6,16f#.6,32e6,32d#6,32e6,8c#6"
//swt4
Public Const ImperialMarch = "ImperialMarch:d=4,o=5,b=100:e,e,e,8c,16p,16g,e,8c,16p,16g,e,p,b,b,b,8c6,16p,16g,d#,8c,16p,16g,e,8p"
Public Const ATeam = "A-Team:d=8,o=5,b=125:4d#6,a#,2d#6,16p,g#,4a#,4d#.,p,16g,16a#,d#6,a#,f6,2d#6,16p,c#.6,16c6,16a#,g#.,2a#"
//swt5
Public Const Flintstones = "Flinstones:d=4,o=5,b=40:32p,16f6,16a#,16a#6,32g6,16f6,16a#.,16f6,32d#6,32d6,32d6,32d#6,32f6,16a#,16c6,d6,16f6,16a#.,16a#6,32g6,16f6,16a#.,32f6,32f6,32d#6,32d6,32d6,32d#6,32f6,16a#,16c6,a#,16a6,16d.6,16a#6,32a6,32a6,32g6,32f#6,32a6,8g6,16g6,16c.6,32a6,32a6,32g6,32g6,32f6,32e6,32g6,8f6,16f6,16a#.,16a#6,32g6,16f6,16a#.,16f6,32d#6,32d6,32d6,32d#6,32f6,16a#,16c.6,32d6,32d#6,32f6,16a#,16c.6,32d6,32d#6,32f6,16a#6,16c7,8a#.6"
Public Const Jeopardy = "Jeopardy:d=4,o=6,b=125:c,f,c,f5,c,f,2c,c,f,c,f,a.,8g,8f,8e,8d,8c#,c,f,c,f5,c,f,2c,f.,8d,c,a#5,a5,g5,f5,p,d#,g#,d#,g#5,d#,g#,2d#,d#,g#,d#,g#,c.7,8a#,8g#,8g,8f,8e,d#,g#,d#,g#5,d#,g#,2d#,g#.,8f,d#,c#,c,p,a#5,p,g#.5,d#,g#"
//swt6
Public Const Smurfs = "Smurfs:d=32,o=5,b=200:4c#6,16p,4f#6,p,16c#6,p,8d#6,p,8b,p,4g#,16p,4c#6,p,16a#,p,8f#,p,8a#,p,4g#,4p,g#,p,a#,p,b,p,c6,p,4c#6,16p,4f#6,p,16c#6,p,8d#6,p,8b,p,4g#,16p,4c#6,p,16a#,p,8b,p,8f,p,4f#"
Public Const MahnaMahna = "MahnaMahna:d=16,o=6,b=125:c#,c.,b5,8a#.5,8f.,4g#,a#,g.,4d#,8p,c#,c.,b5,8a#.5,8f.,g#.,8a#.,4g,8p,c#,c.,b5,8a#.5,8f.,4g#,f,g.,8d#.,f,g.,8d#.,f,8g,8d#.,f,8g,d#,8c,a#5,8d#.,8d#.,4d#,8d#."
// swt7
Public Const MissionImp = "MissionImp:d=16,o=6,b=95:32d,32d#,32d,32d#,32d,32d#,32d,32d#,32d,32d,32d#,32e,32f,32f#,32g,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,g,8p,g,8p,a#,p,c7,p,g,8p,g,8p,f,p,f#,p,a#,g,2d,32p,a#,g,2c#,32p,a#,g,2c,a#5,8c,2p,32p,a#5,g5,2f#,32p,a#5,g5,2f,32p,a#5,g5,2e,d#,8d"
Public Const Pacman = "Pacman:d=4,o=5,b=112:32b,32p,32b6,32p,32f#6,32p,32d#6,32p,32b6,32f#6,16p,16d#6,16p,32c6,32p,32c7,32p,32g6,32p,32e6,32p,32c7,32g6,16p,16e6,16p,32b,32p,32b6,32p,32f#6,32p,32d#6,32p,32b6,32f#6,16p,16d#6,16p,32d#6,32e6,32f6,32p,32f6,32f#6,32g6,32p,32g6,32g#6,32a6,32p,32b.6"
//swt8
Public Const OffToSeeWiz = "OffToSeeWiz:d=32,o=5,b=45:16g#.,16c#6,f#,16f,f#,g#,8c#6,g#,a#,g#,f#,f,f#,d#,8c#.,c#,16c#,c#,16c#6,c#6,c6,c6,c6,16a#,a#,d#6,d#6,d#6,16d#6,c6,8g#"
Public Const MontyPython = "MontyPython:d=4,o=5,b=200:32p,f6,8e6,d6,8c#6,c6,8b,a#,8a,8g,8a,8a#,a,8g,2c6,8p,8c6,8a,8p,8a,8a,8g#,8a,8f6,8p,8c6,8c6,8p,8a,8a#,8p,8a#,8a#,8p,8c6,2d6,8p,8a#,8g,8p,8g,8g,8f#,8g,8e6,8p,8d6,8d6,8p,8a#,8a,8p,8a,8a,8p,8a#,2c6,8p,8c6"


Const SONG_DELAY = 2000     // 1000ms delay between songs

//tune subroutines
Sub Tune1()
    WriteAt(1,1,"     HAPPY      ")
    WriteAt(2,1,"  BIRTHDAY DAY  ")
    RTTTL.play(@HappyBday)
    DelayMS(SONG_DELAY)
    counter0 = 1                 //counter0 = 1 TUNE HAS PLAYED ONCE ALREADY
    Cls
End Sub
//XXXXXXXXXXXXXXX   
Sub Tune2()
    WriteAt(1,1,"       THE      ")
    WriteAt(2,1,"     SIMPSONS     ")
    RTTTL.play(@Simpsons)
    DelayMS(SONG_DELAY)
    counter0 = 0                  //counter0=0 REVERT BACK TO TUNE1 UPON SECOND BUTTON PRESS
    Cls
End Sub
 //XXXXXXXXXXXXXXXXX
            
    Sub Tune3()
        WriteAt(1,1,"     INDIANA    ")
        WriteAt(2,1,"      JONES     ")
        RTTTL.play(@IndianaJones)
        DelayMS(SONG_DELAY)
        Cls
        counter1=1
    End Sub       
     //XXXXXXXXXXXXXXXXXXXX           
    Sub Tune4()   
        WriteAt(1,1,"     MUPPETS    ")
        RTTTL.play(@Muppets)
        DelayMS(SONG_DELAY)
        Cls
        counter1=0
    End Sub
 //XXXXXXXXXXXXXXXXXXX       
        Sub Tune5()
            WriteAt(1,1,"     LOONEY     ")
            WriteAt(2,1,"     TUNES      ")
            RTTTL.play(@LooneyTunes)
            DelayMS(SONG_DELAY)
            Cls
            counter2=1
        End Sub
         //XXXXXXXXXXXXXXXXXXXXX               
        Sub TUNE6()
            WriteAt(1,1,"     STAR      ")
            WriteAt(2,1,"     WARS      ")
            RTTTL.play(@StarWars)
            DelayMS(SONG_DELAY)
            Cls
            counter2=0
        End Sub   
        //XXXXXXXXXXXXXXXXXXXXXXXXX                 
                Sub TUNE7()
                    WriteAt(1,1,"    THE    ")
                    WriteAt(2,1,"     FLINTSTONES     ")
                    RTTTL.play(@Flintstones)
                    DelayMS(SONG_DELAY)
                    Cls
                    counter3=1
                End Sub
                 //XXXXXXXXXXXXXXXXXXXXXXX               
                 Sub TUNE8()
                     WriteAt(1,1,"    THE WIZZARD   ")
                     WriteAt(2,1,"     OF OZ     ")
                     RTTTL.play(@ OffToSeeWiz )
                     DelayMS(SONG_DELAY)
                     Cls
                     counter3=0
                 End Sub
                 //XXXXXXXXXXXXXXXXXXXXXXXX
                     Sub Tune9()
                        WriteAt(1,1,"     IMPERIAL     ")
                        WriteAt(2,1,"  MARCH ")
                        RTTTL.play(@ImperialMarch)
                        DelayMS(SONG_DELAY)
                        counter5 = 1                 //counter0 = 1 TUNE HAS PLAYED ONCE ALREADY
                        Cls
                     End Sub
                      //XXXXXXXXXXXXXXXXXXXXXXXXX 
                    Sub Tune10()
                        WriteAt(1,1,"       THE      ")
                        WriteAt(2,1,"     A-TEAM     ")
                        RTTTL.play(@ATeam)
                        DelayMS(SONG_DELAY)
                        counter5 = 0                  //counter0=0 REVERT BACK TO TUNE1 UPON SECOND BUTTON PRESS
                        Cls
                    End Sub
                    //XXXXXXXXXXXXXXXXXXXXXXXX
        
                             Sub Tune11()
                                 WriteAt(1,1,"     INDIANA    ")
                                 WriteAt(2,1,"      JONES     ")
                                 RTTTL.play(@IndianaJones)
                                 DelayMS(SONG_DELAY)
                                 'Cls
                                 counter6=1
                             End Sub
                              //XXXXXXXXXXXXXXXXXXXXXXXX         
                                 Sub Tune12()   
                                     WriteAt(1,1,"     MUPPETS    ")
                                     RTTTL.play(@Muppets)
                                     DelayMS(SONG_DELAY)
                                     'Cls
                                     counter6=0
                                 End Sub
                                 //XXXXXXXXXXXXXXXXXXXXXX     
                                         Sub Tune13()
                                             WriteAt(1,1,"    JEOPARDY    ")
                                             RTTTL.play(@LooneyTunes)
                                             DelayMS(SONG_DELAY)
                                             'Cls
                                             counter7=1
                                         End Sub
                                         //XXXXXXXXXXXXXXXXXXXXX               
                                                 Sub TUNE14()
                                                     WriteAt(1,1,"     SMURFS      ")
                                                     RTTTL.play(@Smurfs)
                                                     DelayMS(SONG_DELAY)
                                                     'Cls
                                                     counter7=0
                                                 End Sub   
                                                 //XXXXXXXXXXXXXXXXXXXXX               
                                                     Sub TUNE15()
                                                         WriteAt(1,1,"MAHNAMAhna")
                                                         RTTTL.play(@ MahnaMahna)
                                                         DelayMS(SONG_DELAY)
                                                         'Cls
                                                         counter8=1
                                                     End Sub
                                                         //XXXXXXXXXXXXXXXXXX               
                                                             Sub TUNE16()
                                                                 WriteAt(1,1,"    THE WIZZARD   ")
                                                                 WriteAt(2,1,"     OF OZ     ")
                                                                 RTTTL.play(@ OffToSeeWiz )
                                                                 DelayMS(SONG_DELAY)
                                                                 'Cls
                                                                 counter8=0
                                                             End Sub
  //XXXXXXXXXXXXXXX                                   
  main:
    // set switch port to inputs and enable pullups
    TRISB = $ff                // set portB as inputs
    WPUB = $ff                // enable pullups on all PORTB pins
    INTCON2.bits(7) = 0        // set bit RBPU = 0 turns on pullups
    
    // set led pins as outputs and turn them off
    LED_PORT = 0
    LED_PORT_TRIS = 0
    // init tone library module
    tone.init()
    tone.setwait()              // set wait_flag true

    // set tone events
    tone.set_events(tone_start_event, tone_stop_event)
 //counters to track tune played   
  counter0 =0
  counter1 =0
  counter2 =0 
  counter3 =0
  counter4 =0   
  counter5 =0
  counter6 =0   
  counter7 =0   
  counter8 =0 
    
  

while PinIsHigh   
       WriteAt(1,1,"PRESS ANY")     //waiting for a button press
       WriteAt(2,1,"KEY DREW")
 //counter(x) is to keep track of which tune has played each port (2 tunes per)     
     // button 1
If PORTB.0 = 0 then        // button 1 pressed...
    if (counter0 = 0) then
        tune1()
        counter0 = 1
    elseif (counter0 = 1) then
        tune2()
        counter0 = 0
    endif
endif
 
 
     // button 2
If PORTB.1 = 0 then        // button 2 pressed...
    if (counter1 = 0) then
        tune3()
        counter1 = 1
    elseif (counter1 = 1) then
        tune4()
        counter1 = 0
    endif
endif

     // button 3
If PORTB.2 = 0 then        // button 3 pressed...
    if (counter2 = 0) then
        tune5()
        counter2 = 1
    elseif (counter0 = 1) then
        tune6()
        counter2 = 0
    endif
end if


    // button 4
If PORTB.3 = 0 then        // button 4 pressed...
    if (counter3 = 0) then
        tune7()
        counter3 = 1
    elseif (counter3 = 1) then
        tune8()
        counter3 = 0
    endif
endif
    

    // button 5
If PORTB.4 = 0 then        // button 5 pressed...
    if (counter4 = 0) then
        tune9()
        counter4 = 1
    elseif (counter4 = 1) then
        tune10()
        counter4 = 0
    endif
endif
    

     // button 6
If PORTB.5 = 0 then        // button 6 pressed...
    if (counter5 = 0) then
        tune11()
        counter0 = 1
    elseif (counter5 = 1) then
        tune12()
        counter5 = 0
    endif
endif
      

      // button 7
If PORTB.6 = 0 then        // button 7 pressed...
    if (counter6 = 0) then
        tune13()
        counter6 = 1
    elseif (counter6 = 1) then
        tune14()
        counter6 = 0
    endif
endif


     // button 8
If PORTB.7 = 0 then        // button 8 pressed...
    if (counter7 = 0) then
        tune15()
        counter0 = 1
    elseif (counter7 = 1) then
        tune16()
        counter7 = 0
    endif
endif

WEND

//End of Program
 
Since your button processing code in the main loop takes care of tracking the counter variables...
Code:
If PORTB.0 = 0 then        // button 1 pressed...
    if (counter0 = 0) then
        tune1()
        counter0 = 1            // <<<<<
    elseif (counter0 = 1) then
        tune2()
        counter0 = 0           //<<<<<<
    endif
endif

You can remove the 'counterN = ' statements from all the 'sub TuneN()' routines...
Code:
Sub Tune2()
    WriteAt(1,1,"       THE      ")
    WriteAt(2,1,"     SIMPSONS     ")
    RTTTL.play(@Simpsons)
    DelayMS(SONG_DELAY)
    counter0 = 0                  //counter0=0 REVERT BACK TO TUNE1 UPON SECOND BUTTON PRESS  <<<<<<<< THIS CAN BE REMOVED
    Cls
End Sub

It's just one more place for things to get confused.
 
Did you notice you have day twice?
Code:
    WriteAt(1,1,"     HAPPY      ")
    WriteAt(2,1,"  BIRTHDAY DAY  ")

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top