Source Code

From Zedomax Wiki

Jump to: navigation, search

We didn't really have time to finish my project all the way but it works for now... We will have to add some more features next year.

Merry Christmas!

Const Device = CB280
'Define speed for songs
'Set to higher for slower song speed and lower for faster song speed

#define DELAYTIME 100
'Set delay time for Slow mode
#define sdelay 10
Dim num As Byte
	
'PWM3, Song and frequency declarations
Dim oct(9) As String *80
Const Single NOTES= (16.35,17.32,18.35,19.45,20.60,21.83,23.12, 24.50, 25.96, 27.50,29.14, 30.87)					'Octave 0
Const Single BLUES= (16.35,19.45,21.83,23.12,24.50,29.14)
'Jungle Bells Octave 5
		oct(0)="O5,E2,E2,E4,E2,E2,E4,E2,G2,C3,D1,E8,F2,F2,F3,F1!"
		oct(1)="O5,F2,E2,E2,E1,E1,E2,D2,D2,E2,D4,G4,E2,E2,E4,E2,E2,E4,E2,G2,C3,D1,E8!"
		oct(2)="O5,F2,F2,F3,F1,F2,E2,E2,E1,E1,G2,G2,F2,D2,C8!"			
'Jungle Bells Octave 6
		oct(3)="O6,E2,E2,E4,E2,E2,E4,E2,G2,C3,D1,E8,F2,F2,F3,F1!"
		oct(4)="O6,F2,E2,E2,E1,E1,E2,D2,D2,E2,D4,G4,E2,E2,E4,E2,E2,E4,E2,G2,C3,D1,E8!"
		oct(5)="O6,F2,F2,F3,F1,F2,E2,E2,E1,E1,G2,G2,F2,D2,C8!"
'Jungle Bells Octave 4		
		oct(6)="O4,E2,E2,E4,E2,E2,E4,E2,G2,C3,D1,E8,F2,F2,F3,F1!"
		oct(7)="O4,F2,E2,E2,E1,E1,E2,D2,D2,E2,D4,G4,E2,E2,E4,E2,E2,E4,E2,G2,C3,D1,E8!"
		oct(8)="O4,F2,F2,F3,F1,F2,E2,E2,E1,E1,G2,G2,F2,D2,C8!"

'Period for PWM
#define PeriodL 256

'Light RGB declarations
Dim ct As Integer, ct2 As Byte, ct3 As Byte
Dim status1 As Byte, status2 As Byte, status3 As Byte
Dim Arrow As Byte
Dim SoundMode As Byte
Dim AlarmColor As Byte
Dim AlarmPosition As Byte
Dim TimePosition As Byte

SoundMode=3
AlarmColor=0
Arrow=1
AlarmPosition=0
TimePosition=0

Dim sec As Byte, min As Byte, hr As Byte
Dim sec2 As Byte, min2 As Byte, hr2 As Byte
Dim AlarmHr As Byte, AlarmMin As Byte
'Set default alarm to 6 AM.
AlarmHr=6
AlarmMin=0

Dim SleepTime As Integer
Dim Sleep As Byte
Dim MenuMode As Byte
SleepTime= 60 * 30 'Set default Sleep time to 30 minutes it would be 60 * 30 seconds
MenuMode=0 'Main Menu
Sleep=0
'PWM0, PWM1, PWM2, and PWM3 correspond to P5, P6, P7, and P19 of CB280
Output 5
Output 6
Output 7
Output 19

'Pushbutton inputs P10 and P11, set them to inputs
Input 10
Input 11
	
'Reset Variables
ct=200
ct2=0
ct3=0

status1=0
status2=0
status3=0

Red 0
Green 0
Blue 0
	

	'	PlayString oct(2)
		Set Display 2,0,0,128
		Cls
		Delay 200
		Locate 0,0
		Print "Zedomax Light Orb"
		Locate 5,1
		Print "Alarm Clock"
		Delay 800
		MainMenu
		
On Timer(10) Gosub here

Do

'Alarm 1000
'If Arrow at Mode
	If SoundMode=0 Then
		Pwmoff 3
		Pwmoff 0
		Pwmoff 1
		Pwmoff 2
	Elseif SoundMode=1 Then
		'Play Jingle Bells
		For ct=0 To 8
			PlayString oct(ct)
		Next
	Elseif SoundMode=2 Then
		RunBluesRandom
	Elseif SoundMode=3 Then		
		Slow
	End If
	
	
	'	PlayString oct(1)
'		Next
'Slow


Loop

here:

PrintTime 0

If hr=AlarmHr And min=AlarmMin And sec=0 Then
	SoundMode=3
End If

	If Sleep=1 Then
			Locate 0,1
			Print "                   "
			Locate 0,1
			Print Dec SleepTime, " seconds left..."
	End If
	
   If In(10)=0 Then
		Debug "10!",Cr
		MaxBEEP	
		Delay 100
		
		If MenuMode=0 Then
			Locate 15,Arrow
			Print "  "
			Incr Arrow
			If Arrow=4 Then Arrow=1
			PrintArrow
		Elseif MenuMode=1 Then
				Locate 0,1
				Print "                   "
				Locate 0,1
				Print Dec SleepTime, " seconds left..."
				SoundMode=3
				Sleep=1
				MenuMode=2
				
		Elseif MenuMode=2 Then
	 			Sleep=0
				MenuMode=0
				SoundMode=0
				MainMenu
		Elseif MenuMode=3 Then
			
			If AlarmPosition =0 And TimePosition=0 Then
				Locate 15,Arrow
				Print "  "
				If Arrow=1 Then 
					Arrow=3
				Elseif Arrow=3 Then 
					Arrow=1
				End If
				PrintArrow
			Elseif AlarmPosition=1 Then
				Incr AlarmHr
				Locate 0,2
				Print Dp(AlarmHr,2,1)
			Elseif AlarmPosition=2 Then
				Incr AlarmMin
				Locate 3,2
				Print Dp(AlarmMin,2,1)
				
			Elseif TimePosition=1 Then
				Incr hr
				Timeset 12,hr
				PrintTime 1
			Elseif TimePosition=2 Then
				Incr min
				Timeset 11,min
				PrintTime 1
				
			End If
		End If
			
	
		
	Elseif In(11)=0 Then
		Debug "11!",Cr
		MaxBEEP
		Delay 100

		If MenuMode=0 Then
			If Arrow=1 Then
				Incr SoundMode
				If SoundMode=4 Then SoundMode=0
			'If Arrow at Sleep
			Elseif Arrow=2 Then
				Cls
				Csroff
				PrintTime 1
				Locate 0,1
				Print Dec SleepTime/60
				Arrow=1
				PrintArrow
				
				MenuMode=1
			Elseif Arrow=3 Then
				Cls
				Csroff
				PrintTime 1
				Locate 0,2
				Print Dp(AlarmHr,2,1),":",Dp(AlarmMin,2,1),":00"
				Locate 0,1
				Print "Set Time"
				Locate 0,3
				Print "Set Alarm"
				'Set MenuMode to Alarm and Time setting
				MenuMode=3	
				Arrow=1	
				PrintArrow
				
			End If
		Elseif MenuMode=1 Then
 			If Arrow=1 Then
			   Locate 0,1
				Print "        "
				Locate 0,1
				SleepTime=SleepTime+60
				Print Dec SleepTime/60
				
			End If
   	Elseif MenuMode=3 Then
			If Arrow=3 Then
				Incr AlarmPosition
				If AlarmPosition=1 Then
					Locate 0,3
					Print "                   "
					Locate 0,3
					Print "||"
				Elseif AlarmPosition=2 Then
					Locate 0,3
					Print "  "
					Locate 3,3
					Print "||"
				Elseif AlarmPosition=3 Then
					AlarmPosition=0
					MenuMode=0
					MainMenu
				End If
			Elseif Arrow=1 Then
				Incr TimePosition
				If TimePosition=1 Then
					Locate 0,1
					Print "                   "
					Locate 0,1
					Print "||"
				Elseif TimePosition=2 Then
					Locate 0,1
					Print "  "
					Locate 3,1
					Print "||"
				Elseif TimePosition=3 Then
					TimePosition=0
					MenuMode=0
					MainMenu
				End If			
			End If
		End If
		
	End If
	'Display current time only if seconds has changed
	
	
	
	
	
Return

Do

If status1=0 And ct<255 Or ct<50Then
   status1=0
	Incr ct
Else
   status1=1
	Decr ct
End If

If status2=0 And ct2<255 Or ct2<50 Then
	status2=0
	Incr ct2
Else
	status2=1
	Decr ct2
End If

If status3=0 And ct3<255 Or ct3<50 Then
   status3=0
	Incr ct3
Else 
	status3=1
	Decr ct3
End If


Red ct
Blue ct2
Green ct3




Loop


End

Sub MainMenu()
		Cls
		  Csroff
		PrintTime 1
		Locate 0,1
		Print "Mode"
		Locate 0,2
		Print "Sleep"
		Locate 0,3
		Print "Set Alarm/Time"
		
		Locate 15,Arrow
		Print "<-"
End Sub

Sub PrintArrow()
	Locate 15,Arrow
	Print "<-"
End Sub
		
Sub PrintTime(pmode As Byte)
	sec=Time(10)
	min=Time(11)
	hr=Time(12)
	If hr=24 Then 
		Timeset 12,0
		hr=0
	End If
	
	If pmode=0 Then
		
		If hr<>hr2 Then
			Locate 0,0
			Print Dp(hr,2,1), ":"
		Elseif min <> min2 Then
			Locate 3,0
			Print Dp(min,2,1), ":"
		Elseif sec2 <> sec Then
			If Sleep=1 Then
				If SleepTime=0 Then 
					Sleep=0
					SoundMode=0
				End If
				If SleepTime>0 Then Decr SleepTime
			End If 
			Locate 6,0
			Print Dp(sec,2,1)
		End If
	
	Else 
		Locate 0,0
		Print Dp(hr,2,1), ":",Dp(min,2,1), ":",Dp(sec,2,1)
	End If
	hr2=hr
	min2=min
	sec2=sec
	
End Sub



Sub Blue(DutyValue As Integer)
	'Debug "Blue DutyValue: ",  Dec DutyValue,Cr
	Pwm 0,DutyValue,PeriodL
End Sub

Sub Green(DutyValue As Integer)
	'Debug "Green DutyValue: ",  Dec DutyValue,Cr
	Pwm 1,DutyValue,PeriodL
	
End Sub

Sub Red(DutyValue As Integer)
'	Debug "Red DutyValue: ",  Dec DutyValue,Cr
	Pwm 2,DutyValue,PeriodL
	
End Sub


Sub RGB()
For ct=0 To 1024
	Red ct
Next
For ct=1024 To 1 Step -1
	Red ct
Next

For ct=0 To 1024
	Blue ct
Next
For ct=1024 To 1 Step -1
	Blue ct
Next


For ct=0 To 1024
	Green ct
Next
For ct=1024 To 1 Step -1
	Green ct
Next
End Sub

Sub Alarm(mode As Byte)

Select Case mode
	Case 0
	Green 0
	Blue 255

	Case 1
	Blue 0
   Red 255
	
	Case 2
	Red 0
	Green 255
	
	Case 3
	Blue 255
	Red 255
	Green 0

	Case 4
	Green 255
	Red 255
	Blue 0

   Case 5
	Green 255
	Blue 255
	Red 0

End Select

End Sub


Sub Slow()


For ct=0 To 255
	Blue ct
	Wait sdelay
	If SoundMode=0 Then Exit For
Next


For ct=0 To 255
	Red ct
	Wait sdelay
	If SoundMode=0 Then Exit For
Next

For ct=255 To 10 Step -1
	Blue ct
	Wait sdelay
	If SoundMode=0 Then Exit For
Next


For ct=0 To 255
	Green ct
	Wait sdelay
	If SoundMode=0 Then Exit For
Next

For ct=255 To 10 Step -1
   Red ct
	Wait sdelay
	If SoundMode=0 Then Exit For
Next

For ct=0 To 255
	Blue ct
	Wait sdelay
	If SoundMode=0 Then Exit For
Next
For ct=0 To 255
	Red ct
	Wait sdelay
	If SoundMode=0 Then Exit For
Next

For ct=255 To 10 Step -1
	Blue ct
	Wait sdelay
	If SoundMode=0 Then Exit For
Next

For ct=255 To 10 Step -1
	Red ct
	Wait sdelay
	If SoundMode=0 Then Exit For
Next

For ct=255 To 10 Step -1
	Green ct
	Delay sdelay
	If SoundMode=0 Then Exit For
Next


End Sub


'Parse music notes and play them on piezo

Sub PlayString(PlayNotes As String * 100)
Dim a As Byte
Dim k As Integer
Dim CurrentOctave As Byte


CurrentOctave=0

a=0

	k=PlayNotes_A(0)

	
	Do Until k=33
		If SoundMode <> 1 Then Exit Do
		
		If(k=111 Or k=79) Then			'if O or o for octave Change
			Incr a
			k=PlayNotes_A(a)
			Debug "OCTAVE CHANGED TO: ", k, Cr
			k=k-48
			CurrentOctave=k

		Elseif(k=67 Or k=99) Then			'if C
			Debug k
			Incr a
			k=PlayNotes_A(a)
			If(k=35) Then	'#
				Debug "#"
				Incr a
				k=PlayNotes_A(a)

				If(k>=48 And k<=57) Then		' Between 0 and 9
					Debug k
					k= k-48
					PlayNote 1, k * DELAYTIME,CurrentOctave
				End If
			Elseif (k>=48 And k<=57) Then	' if k between 0 and 9
					Debug k
					k= k-48
					PlayNote 0, k* DELAYTIME, CurrentOctave
			End If

		Elseif(k=68 Or k=100) Then
			Debug "D"
			Incr a
			k=PlayNotes_A(a)

			If(k=35) Then	'#
				Debug "#"
				Incr a
				k=PlayNotes_A(a)

				If(k>=48 And k<=57) Then		' Between 0 and 9
					Debug k
					k=k-48
					PlayNote 3, k * DELAYTIME,CurrentOctave
				End If
			Elseif (k>=48 And k<=57) Then	' if k between 0 and 9
				Debug k
				k= k-48
				PlayNote 2, k* DELAYTIME, CurrentOctave
			End If

		Elseif(k=69 Or k=101) Then
			Debug "E"
			Incr a
			k=PlayNotes_A(a)

				If(k>=48 And k<=57) Then		' Between 0 and 9
					Debug k
					k= k-48
					PlayNote 4, k * DELAYTIME,CurrentOctave
				End If

			End If

		Elseif(k=70 Or k=102) Then
			Debug "F"
			Incr a
			k=PlayNotes_A(a)

			If(k=35) Then	'# 
				Debug "#"
				Incr a
				k=PlayNotes_A(a)

				If(k>=48 And k<=57) Then		' Between 0 and 9
					Debug k
					k=k-48
					PlayNote 6, k * DELAYTIME,CurrentOctave
				End If
			Elseif (k>=48 And k<=57) Then	' if k between 0 and 9
				Debug k
				k= k-48
				PlayNote 5, k* DELAYTIME, CurrentOctave
			End If

		
		Elseif(k=71 Or k=103) Then
			Debug "G"
			Incr a
			k=PlayNotes_A(a)

			If(k=35) Then	'#
				Debug "#"
				Incr a
				k=PlayNotes_A(a)
				
				If(k>=48 And k<=57) Then		' Between 0 and 9
					Debug k
					k=k-48
					PlayNote 8, k * DELAYTIME,CurrentOctave
				End If
			Elseif (k>=48 And k<=57) Then	' if k between 0 and 9
				Debug k
				k= k-48
				PlayNote 7, k* DELAYTIME, CurrentOctave
			End If
		Elseif(k=65 Or k=97) Then
			Debug "A"
			Incr a
			k=PlayNotes_A(a)

			If(k=35) Then	'#
				Debug "#"
				Incr a
				k=PlayNotes_A(a)
				
				If(k>=48 And k<=57) Then		' Between 0 and 9
					Debug k
					k=k-48
					PlayNote 10, k * DELAYTIME,CurrentOctave
				End If
			Elseif (k>=48 And k<=57) Then	' if k between 0 and 9
				Debug k
				k= k-48
				PlayNote 9, k* DELAYTIME, CurrentOctave
			End If

		Elseif(k=66 Or k=98) Then
			Debug "B"
			Incr a
			k=PlayNotes_A(a)

			If (k>=48 And k<=57) Then	' if k between 0 and 9
				Debug k
				k= k-48
				PlayNote 11, k* DELAYTIME, CurrentOctave
			End If
		
		End If
		Incr a
		k=PlayNotes_A(a)
				
		If k=44 Then 'For commas
			Incr a
			k=PlayNotes_A(a)
		End If

	Loop

End Sub

Sub PlayNote(ThisNote As Byte,PlayTime As Integer, Octave As Byte)
Dim tsingle As Single
Dim period As Integer
Dim multiply As Byte


tsingle=NOTES(ThisNote)
Debug " Octave: ", Dec Octave
If Octave>0 Then
	For multiply=1 To Octave 
		tsingle=tsingle * 2
	Next
End If

Debug " Freq: ", Float tsingle
period =GetWidth(tsingle)
Debug " Period: ", Dec period,Cr

'----
Incr AlarmColor
If AlarmColor>5 Then AlarmColor=0
Alarm AlarmColor
'---

Pwm 3,period/2,period

Delay PlayTime
Pwmoff 3

End Sub




'Find (Width)Period of Frequency!

Function GetWidth(Frequency As Single) As Integer
Dim a As Single

a=4/(0.000001*1.735*Frequency)
a=a-1
GetWidth=Floor(a)

End Function



Sub RunBluesRandom()

Dim tsingle As Single					' Max's BLUES Solo!!!
Dim Period As Integer 
Dim bluesrand As Byte
num=Rnd(3) mod 6						'Get random values by using rnd()

Debug Dec num,Cr
tsingle=BLUES(num)

bluesrand=Rnd(0)
Debug "RANDDDD: ",Dec bluesrand,Cr

  If bluesrand <150 Then
	tsingle=tsingle * 16
Else
	tsingle=tsingle * 32
End If
Period=GetWidth(tsingle)
Debug "Result: ",Dec Period,Cr,Cr
'----
Incr AlarmColor
If AlarmColor>5 Then AlarmColor=0
Alarm AlarmColor
'---

Pwm 3,(Period/2),Period

If Rnd(0) <8000 Then	
	Delay 400
Else
	Delay 200
End If

End Sub

'Cool BEEP noise function for PWM3 Piezo
Sub MaxBEEP()
	Pwmoff 3 			'Turn off PWM3
	Dim SJ As Byte
	For SJ = 0 To 50
		Reverse 19
		Udelay 70
		Reverse 19		
	Next
	For SJ = 0 To 80
		Reverse 19
		Udelay 40
		Reverse 19		
	Next
End Sub
Personal tools