Controller for an horse exercise yard

Horse exercise yard?? This is Sort of hamster wheel for horses. With an AC motor, a frequency converter and the welder of your trust is someting feasible. The problem now was the following:
The direction of rotation of the wheel should from time to time change, so the animals do not get dizziness, plus various speeds should be possible, depending on the fitness of the horses.
Because the number of different programs was open, I decided to use an infrared remote control. So the system can be selected on and off and so far 3 different interval patterns. In the download section a circuit diagram and the source code is provided.

Download

fuehranlage.bas

Sourcecode

'--------------------------------------------------------------
'                        F?hranlage f?r Pferde
'                  (c) 2007, Gerold Electronic
'                        Fileversion 1.0
'--------------------------------------------------------------
'
'Pin 5:  Eingang TSOP 1736 (IR-Empf?nger)
'Pin 11: Ausgang LED Programm 1
'Pin 12: Ausgang LED Programm 2
'Pin 13: Ausgang LED Programm 3
'Pin 14: Ausgang Piezo
'Pin 15: Ausgang Relais langsam,Zeit1-schnell,Zeit2 (Geschwindigkeitund Zeit)
'Pin 16: Ausgang Relais links-rechts (Drehrichtung)
'Pin 17: Ausgang Relais Steuerung ein
'Pin 23: Eingang Poti 1. Zeitintervall einstellen
'Pin 24: Eingang Poti 2. Zeitintervall einstellen

$regfile = "m8def.dat"
$crystal = 3686400
$baud = 9600
'Use byte library for smaller code
'$lib "mcsbyte.lbx"

'This example shows how to decode RC5 remote control signals
'The GETRC5 function uses TIMER0 and the TIMER0 interrupt.
'The TIMER0 settings are restored however so only the interrupt can not
'be used anymore for other tasks

Dim Address As Byte , Command As Byte
Dim Zeit1 As Word , Zeit2 As Word , Zeita As Word , Zeitb As Word
Dim I As Byte                                               'Zahl der Timerdurchl?ufe
Dim K As Byte                                               'Zahl der Wendungen





Ddrc = &B00000000                                           'Ausg?nge setzen
Ddrb = &B00001111                                           'Ausg?nge setzen
Ddrd = &B11100000



Config Rc5 = Pind.3                                         'Eingang f?r IR-Empfang, belegt Timer 0 !!
Config Timer1 = Timer , Prescale = 1024
Enable Timer1                                               'Overflow Interrupt ein
Enable Interrupts                                           'Interrupts werden f?r IR-Empfang ben?tigt

On Timer1 Timerfertig

Stop Timer1

Do
   Admux = &B01100000
   Adcsra = &B11000010
   Getrc5(address , Command)
 '  Print Address ; "--" ; Command
   If Command > 127 Then                                    'Toggle bit  enfernen
      Command = Command - 128
   End If
      Select Case Command
       Case 1 :                                             'Taste "1"
       If Portd.5 = 0 Then                                  ' verhindert einen erneuten Aufruf, wenn Taste 1 schon gedr?ckt wurde
         Portd.5 = 1
         Portd.6 = 0
         Portd.7 = 0
         Sound Portb.0 , 288 , 65
         'Die Zeit von den Potis holen...
         Zeita = Adch
         Zeitb = 0
         Zeit1 = Zeita \ 16
         I = 0
         K = 0
         Portb.1 = 0
         Portb.2 = 0
         Portb.3 = 1
         Timer1 = 0
         Start Timer1
         Print Zeit1
       End If
       Case 2 :                                             'Taste "2"
       If Portd.6 = 0 Then                                  ' verhindert einen erneuten Aufruf, wenn Taste 2 schon gedr?ckt wurde
         Portd.5 = 0
         Portd.6 = 1
         Portd.7 = 0
         Sound Portb.0 , 288 , 65
         Waitms 50
         Sound Portb.0 , 288 , 65
         Admux = &B01100001
         Adcsra = &B11000010
         Waitms 250
         Zeita = Adch
         Zeitb = 0
         Zeit1 = Zeita \ 16
         I = 0
         K = 0
         Portb.1 = 1
         Portb.2 = 0
         Portb.3 = 1
         Timer1 = 0
         Start Timer1
         Print Zeit1
       End If
       Case 3 :                                             'Taste "3"
       If Portd.7 = 0 Then                                  ' verhindert einen erneuten Aufruf, wenn Taste 3 schon gedr?ckt wurde
         Portd.5 = 0
         Portd.6 = 0
         Portd.7 = 1
         Sound Portb.0 , 288 , 65
         Waitms 50
         Sound Portb.0 , 288 , 65
         Waitms 50
         Sound Portb.0 , 288 , 65
         Zeita = Adch
         Admux = &B01100001
         Adcsra = &B11000010
         Waitms 250
         Zeitb = Adch \ 16
         If Zeitb = 0 Then Zeitb = 1
         Zeit1 = Zeita \ 16
         I = 0
         K = 0
         Portb.1 = 0
         Portb.2 = 0
         Portb.3 = 1
         Print Zeit1
         Print Zeitb
         Timer1 = 0
         Start Timer1
       End If
       Case 12 :                                            'Taste "Standby"
       If Portd.5 = 1 Or Portd.6 = 1 Or Portd.7 = 1 Then    ' verhindert einen erneuten Aufruf, wenn Taste Standby schon gedr?ckt wurde
         Stop Timer1
         Portd.5 = 0
         Portd.6 = 0
         Portd.7 = 0
         Portb.1 = 0
         Portb.2 = 0
         Portb.3 = 0
         Sound Portb.0 , 288 , 100

       End If
       End Select
Loop

Timerfertig:

   I = I + 1
   If I >= Zeit1 Then
      Portb.2 = Not Portb.2
      I = 0
      If K < 3 Then K = K + 1
      If Zeitb > 0 And K >= 2then                           'Programm 3 ist aktiv
         Portb.1 = Not Portb.1
         K = 0
         If Zeit1 = Zeita Then
            Zeit1 = Zeitb
         Else
            Zeit1 = Zeita
         End If
      End If
   End If

Return

End