
' **********************************************************************
' * File:   presepemax_2012_def.bas   - Progetto Del Del 04.05.12 By Max B : *
' * Scheda: ARDUINO UNO       con Bascom AVR   IDE
' *  ver. n del 10.12.12
' **********************************************************************
'
'****************** Direttive del compilatore **************************
'
$regfile = "m328pdef.dat"
$crystal = 16000000
$baud = 19200
Config Clockdiv = 2
 $timeout = 300000
'$sim
Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portc.2 , Db5 = Portc.3 , Db6 = Portc.4 , Db7 = Portc.5 , E = Portc.1 , Rs = Portc.0
Config Timer2 = Pwm , Prescale = 8 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down
Config Adc = Single , Prescaler = Auto , Reference = Avcc       'configura ADC

'****************** Dichiarazione delle variabili **********************
Dim N As Byte
Dim Var As String * 1
Dim Su As Byte
Dim Giu As Byte
Dim Tempo As Word
Dim Dist As Word
Dim X As String * 2
Dim V As Word
Dim V1 As Word
Dim Temp As Single
Dim S5 As String * 5

Declare Sub Inizializ()
'************************* Programma main ******************************
Mai:
   Call Inizializ()
     Portb.0 = 1 : Portb.1 = 1 : Portb.2 = 1 : Portb.4 = 1 : Portb.5 = 1
     Portd.4 = 1 : Portd.6 = 1 : Portd.7 = 1
    Pwm2a = 0
    Cls : Cursor Off                              'Inizializza LCD
    Lcd "  BUON NATALE  "
   Lowerline : Lcd "Filippo e Carlo"
   Print "BUON NATALE da Filippo e Carlo   "
   Wait 2
   Cls
Avvio:
   Cls
   Print "Avvia il Presepio "
   Upperline
   Lcd "Avvia  Presepio"

 Via:
   Input , X Noecho
      If X = "q" Then : Goto Programma
      Elseif X = "s" Then : Goto Day
      Elseif X = "u" Then : Goto Resetprog
      Elseif X = "t" Then : Goto Test
      Elseif X = "r" Then : Goto Temperatura
      Elseif X = "w" Then : Goto Resetprog
      Elseif X = "y" Then : Goto Avvio

     Elseif X = "k" Then : Set Portb.0 : Cls : Locate 2 , 1 : Lcd " fuoco on" : Print "fuoco on"
     Elseif X = "l" Then : Reset Portb.0 : Cls : Locate 2 , 1 : Lcd " fuoco off" : Print "fuoco off "
     Elseif X = "i" Then : Set Portb.1 : Cls : Locate 2 , 1 : Lcd "stelle on" : Print "stelle on "
     Elseif X = "j" Then : Reset Portb.1 : Cls : Locate 2 , 1 : Lcd " stelle off" : Print "stelle off "
     Elseif X = "g" Then : Set Portb.2 : Cls : Locate 2 , 1 : Lcd " fontana on" : Print "fontana on"
     Elseif X = "h" Then : Reset Portb.2 : Cls : Locate 2 , 1 : Lcd " fontana off" : Print "fontana off "
     Elseif X = "e" Then : Pwm2a = 0 : Cls : Locate 2 , 1 : Lcd "luci giorno on" : Print " luci giorno on "
     Elseif X = "f" Then : Pwm2a = 255 : Cls : Locate 2 , 1 : Lcd " luci giorno off" : Print "luci giorno  off "
     Elseif X = "c" Then : Set Portb.4 : Cls : Locate 2 , 1 : Lcd " luci case on" : Print "luci case on"
     Elseif X = "d" Then : Reset Portb.4 : Cls : Locate 2 , 1 : Lcd " luci case off" : Print "luci case off "
     Elseif X = "a" Then : Set Portb.5 : Cls : Locate 2 , 1 : Lcd " panni stesi on" : Print "panni stesi on "
     Elseif X = "b" Then : Reset Portb.5 : Cls : Locate 2 , 1 : Lcd "panni stesi  off" : Print "panni stesi off "
     Elseif X = "o" Then : Set Portd.4 : Cls : Locate 2 , 1 : Lcd " luci notte on" : Print "luci notte on"
     Elseif X = "p" Then : Reset Portd.4 : Cls : Locate 2 , 1 : Lcd " luci notte off" : Print "luci notte off "
      Elseif X = "+" Then : Set Portd.6 : Cls : Locate 2 , 1 : Lcd " nonna on" : Print "nonna on"
     Elseif X = "-" Then : Reset Portd.6 : Cls : Locate 2 , 1 : Lcd " nonna  off" : Print "nonna off "
     Elseif X = "m" Then : Set Portd.7 : Cls : Locate 2 , 1 : Lcd "pastori on" : Print "pastori on "
     Elseif X = "n" Then : Reset Portd.7 : Cls : Locate 2 , 1 : Lcd " pastori off" : Print "pastori  off "
     Elseif X = "x" Then : Set Portd.5 : Waitms 500 : Reset Portd.5 : Cls : Upperline : Lcd " MP3 ON   " : Print " MP3 ON "
     Elseif X = "1" Then : Set Portd.5 : Waitms 500 : Reset Portd.5 : Cls : Upperline : Lcd " MUSICA ON   " : Print " MUSICA ON "
     Elseif X = "v" Then : Set Portd.5 : Cls : Upperline : Cls : Lcd " MP3 OFF   " : Print " MP3 OFF" : Wait 2 : Reset Portd.5 : Goto Avvio
  End If

Pulseout Portd , 2 , 20
Portd.2 = 0
Bitwait Pind.3 , Reset
Pulsein Tempo , Pind , 3 , 1
Tempo = Tempo * 10
Dist = Tempo / 29
If Dist < 4 Then
Waitms 20
Gosub Temperatura
 End If
 If Dist > 8 And Dist < 12 Then
Gosub Programma
End If
Goto Via

Programma:
   Print "PROGRAMMA_ON"
   Cls
   Upperline : Lcd "PROGRAMMA _ON"
    Portd.5 = 0 : Waitms 300 : Portd.5 = 1        ' accende mp3
   Waitms 1500
    Portd.5 = 0 : Waitms 300 : Portd.5 = 1        ' avvia musica
     For Su = 1 To 240                            'Step 1
           Pwm2a = Su                             'accende gradatamente LUCI_GIORNO
            If Su = 100 Then Reset Portb.2        ' fontana - mulino ON
            If Su = 150 Then Reset Portd.7        ' pastori (treno)   ON
             If Su = 180 Then Reset Portd.6       ' nonna   ON
            If Su = 200 Then Reset Portb.5        ' panni stesi - ON
            Waitms 130
    Next
    Waitms 100
      For Giu = 255 To 0 Step -1
             Pwm2a = Giu                          'spegne  gradatamente LUCI_GIORNO
             If Giu = 230 Then Set Portd.7        '  pastori (treno)
             If Giu = 200 Then Set Portd.6        '  nonna  DOWN
             If Giu = 180 Then Set Portb.5        'panni stesi          - DOWN
             If Giu = 160 Then Set Portb.2        'fontana  DOWN
             If Giu = 50 Then Reset Portb.0       'fuoco ON
             Waitms 100
      Next
        Portb.1 = 0                               'stelle ON
        Lowerline : Lcd "STELLE  ON"
         Wait 7
         Cls
          Portb.4 = 0                             'luci case  ON
          Lowerline : Lcd "LUCI CASE  ON"
          Wait 8
          Cls
           Portd.4 = 0                            ' luce notte ON
          Lowerline : Lcd "LUCE NOTTE  ON"
          Wait 5
          Cls
           Portb.0 = 1                            'fuoco   DOWN
          Lowerline : Lcd " FUOCO SPENTO"
          Wait 5
           Cls
            Portb.4 = 1                           'luci case  DOWN
           Lowerline : Lcd "LUCI CASE SPENTE"
            Wait 5
            Cls
            Portb.1 = 1                           'stelle DOWN
            Lowerline : Lcd "STELLE SPENTE"
             Wait 6
             Cls
           Portd.4 = 1                            ' luce notte DOWN
            Lowerline : Lcd "LUCI NOTTE OFF "
            Wait 3
            Cls
             Portd.5 = 0                          ' musica OFF
            Lowerline : Lcd "MUSICA OFF "
            Wait 1
            Print "PROGRAMMA_OFF"
             Cls
             Lcd "PROGRAMMA _OFF"
              Wait 1 : Portd.5 = 0
              Print " " ;
              Wait 2
              Goto Avvio

Day:
     Print "PROGRAMMA_DAY"
     Cls
     Lcd "PROGRAMMA _DAY"
 Set Portb.0
 Set Portb.1
 Set Portb.2
  Set Portb.4
 Set Portb.5
 Set Portd.4
 Set Portd.6
 Set Portd.7
 Pwm2a = 0
 Wait 5
 Reset Portb.0
 Reset Portb.1
 Reset Portb.2
 Reset Portb.4
 Reset Portb.5
 Reset Portd.4
 Reset Portd.6
 Reset Portd.7
Pwm2a = 255
Cls
Lcd "PROGRAMMA  OFF"
Print "PROGRAMMA  OFF"
Waitms 300
Print " " ;
Wait 3
Goto Avvio

Test:
Cls
Lcd "Test comandi  ON"
Print "Test comandi  ON"
For N = 0 To 7
 Portb = 2 ^ N
 Waitms 200
 Next
 Set Portd.7
 Waitms 500
 Reset Portd.7
 Waitms 500
  Set Portd.6
 Waitms 500
 Reset Portd.6
 Waitms 500
 Set Portd.4
 Waitms 500
Reset Portd.4
Waitms 500
Reset Portb.0
 Pwm2a = 0
 Waitms 500
Pwm2a = 255
Cls
Waitms 400
Lcd "Test comandi OFF"
Print "Test comandi OFF"
Waitms 300
Print " " ;
Waitms 200
Goto Avvio

Resetprog:
             Reset Portb.0
             For N = 0 To 7
             Reset Portb. N
             Next
             Reset Portd.4
              Reset Portd.6
             Reset Portd.7
             Pwm2a = 255
             Cls : Lcd "RESET  "
             Lowerline : Lcd "    COMANDI "
             Print "RESET COMANDI"
             Wait 1
Goto Avvio

Temperatura:
Start Adc                                         'start ADC
 Ddrc.0 = 0                                       ' imposta il portc.0 come input
 Waitms 10
 V = Getadc(0) : V1 = 0                           'leggi dal canale (0)
 V1 = Getadc(0)
 V = V + V1 : V1 = 0                              ' 2 letture
Temp = 2026 - V
Temp = Temp / 5.1                                 'resistenz a +5V di 5760 ohm (4700+470) - termistore di 47Kohm
 S5 = Fusing(temp , "##.#")
 V = 0
 Ddrc.0 = 1
 Cls
 Upperline
 Lcd "Temp. Int.Quadro" : Print " Temperatura :"
 Locate 2 , 5
 Lcd S5 ; " " ; Chr(176) ; "C "
 Print S5 ; "  " ; "" ; "C"
 Wait 3
 Print " " ;
 Waitms 500
 Goto Avvio

  End

  Sub Inizializ()
     Disable Interrupts
     Ddrb = Ddrb Or &H3F                          ' Imposta PB0- PB1-PB2-PB3-PB4-PB5 come output
     Ddrd = Ddrd Or &HF4                          'Imposta PD2- PD4-PD5-PD6-PD7 come output
      Ddrd.3 = 0                                  'imposta PD3 come INPUT
     '  Ddrd.4 = 1                                 ' imposta PD4 come OUTPUT
     'Ddrc = Ddrc And Hc0                          'imposta PCO ,1,2,3,4,5 come INPUT
 End Sub