  PROVE Funz.con sheda ARDUINO  e BascomAVR
' **********************************************************************
'
'****************** Direttive del compilatore **************************
'
$regfile = "m328pdef.dat"
$crystal = 16000000
$baud = 19200
Config Clockdiv = 2

Config Pind.3 = Input                                       ' echo
Config Portd.2 = Output                                     ' trigger
Config Portb = Output
Config Portd.7 = Output
Config Portd.4 = Output
Config Portd.5 = Output

Dim V As Word
Dim V1 As Word
Dim Temp As Single
Dim S5 As String * 5

'$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 Timer0 = Pwm , Prescale = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up
Config Timer2 = Pwm , Prescale = 8 , Compare A Pwm = Clear Up , Compare B Pwm = Clear Up
 $timeout = 300000

 Config Adc = Single , Prescaler = Auto , Reference = Avcc  'configura ADC
Start Adc                                                   'start ADC

'****************** Dichiarazione delle costanti ***********************
'
Const Mas = "  PRESEPE 2012  "
'Const Canc = "                 "                            ' x cancellare una sola riga

'
'****************** Dichiarazione delle variabili **********************
'
Dim N As Byte
Dim Var As String * 1
Dim Su As Byte , Giu As Byte
Dim Tempo As Word
Dim Dist As Word
Dim X As String * 2

'************************* Programma main ******************************

 Cls : Cursor Off                                           'Inizializza LCD
     Lcd "  BUON NATALE  "
     Lowerline : Lcd "Filippo e Carlo"
     Print "BUON NATALE da Filippo e Carlo   "
      Wait 2
Cls : 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 50
 Reset Portd.7
 Waitms 50
 Set Portd.4
 Waitms 50
Reset Portd.4
Waitms 50
Reset Portb.0
 Pwm2a = 0
 Waitms 50
Pwm2a = 255
Cls
Waitms 400
Lcd "Test comandi OFF"
Print "Test comandi OFF"
Print "Z"
Waitms 20

   Avvio:
Cls
 Print "Avvia  il Presepio "
 Upperline
 Lcd "Avvia il "                                            'display this at the top line
Lowerline                                                   'select the lower line
Lcd "presepio."                                             'display this at the lower line
'For A = 1 To 10
'Shiftlcd Right                                              'shift the text to the right
'Waitms 3                                                    'wait a moment
'Next
'for A = 1 To 10
'Shiftlcd Left                                               'shift the text to the left
'Waitms 3                                                    'wait a moment
'Next

   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 : Set Portb.3 : Cls : Locate 2 , 1 : Lcd "luci giorno on" : Print " luci giorno on "
     Elseif X = "f" Then : Reset Portb.3 : 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 = "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 : Cls : Upperline : Lcd " MP3 ON   " : Print " MP3 ON "
     Elseif X = "1" Then : Set Portd.5 : Cls : Upperline : Lcd " MUSICA ON   " : Print " MUSICA ON "
     Elseif X = "v" Then : Pulseout Portd , 5 , 20 : Upperline : Cls : Lcd " MP3 OFF   " : Print " MP3 OFF "
                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 > 6 And Dist < 10 Then
     Gosub Programma
    End If
Goto Via
 Waitms 10
 Cls
                       Programma:
Print "PROGRAMMA_ON"
Cls
Lcd "PROGRAMMA _ON"
     For Su = 255 To 0 Step -1
           Pwm2a = Su                                       'accende gradatamente LUCI_GIORNO
            If Su = 253 Then Set Portd.5                    ' accende mp3
            If Su = 252 Then Set Portd.5                    ' avvia musica
            If Su = 120 Then Set Portb.2                    ' fontana  UP
            If Su = 120 Then Set Portd.7                    ' pastori (treno)-nonna   UP
            If Su = 100 Then Set Portb.5                    ' panni stesi          - UP
            Waitms 180
    Next
    Waitms 100
      For Giu = 0 To 255
             Pwm2a = Giu                                    'spegne  gradatamente LUCI_GIORNO
             If Giu = 50 Then Reset Portd.7                 '  pastori (treno)-nonna  DOWN
             If Giu = 80 Then Reset Portb.5                 'panni stesi          - DOWN
             If Giu = 120 Then Reset Portb.2                'fontana  DOWN
             If Giu = 150 Then Set Portb.0                  'fuoco UP
             Waitms 180
      Next
       Portb.1 = 1                                          'stelle UP
        Wait 7
        Lowerline : Lcd "STELLE ACCESE"
        Set Portb.4                                         'luci case  UP
        Wait 8
        Lowerline : Lcd "LUCI CASE  ACCESE"
        Set Portd.4                                         ' luce notte UP
         Waitms 5
         Lowerline : Lcd "LUCE NOTTE ACCESE"
         Reset Portb.0                                      'fuoco   DOWN
         Wait 2
         Lowerline : Lcd " FUOCO SPENTO"
         Reset Portb.4                                      'luci case  DOWN
         Wait 2
          Lowerline : Lcd "LUCI CASE SPENTE"
          Reset Portb.1                                     'stelle DOWN
          Wait 2
          Lowerline : Lcd "STELLE SPENTE"
          Reset Portd.4                                     ' luce notte DOWN
          Lowerline : Lcd "Luci Notte Spente "

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

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

Temperatura:
 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
  Temp = 2220 - V
  Temp = Temp / 36
    S5 = Fusing(temp , "##.#")
   V = 0
   Ddrc.0 = 1
   Cls
  Upperline
   Lcd " TEMPERATURA =  " : Print " Temperatura :"
  Locate 2 , 5
  Lcd S5 ; " " ; Chr(176) ; "C "
   Print Temp ; "    " ; "" ; "C"
   ' Input , X Noecho
    '  If X = "y" Then
     '  Goto Avvio
     ' End If
      Wait 3
   Goto Avvio

   'Musica:
  ' Cls
  '  Lcd " MP3 ON   " : Print " MP3 ON "
   '   Set Portd.5
    '  Waitms 300
    '   Input , X Noecho
    '  If X = "1" Then : Set Portd.5
     '   Upperline
     ' Lcd " MUSICA ON   " : Print " MUSICA ON "
   '    End If
    '    Waitms 500
    '   If X = "v" Then
     '   Lcd " MP3 OFF   " : Print " MP3 OFF "
     '  Pulseout Portd , 5 , 20
      ' End If
      ' Goto Avvio

 End