Step 10Copy and paste the source code
Example for DIP32 (8mHz)
*/
dim M00 as new pwm(PP0)
dim M01 as new pwm(PP1)
dim M11 as new pwm(PP2)
dim M10 as new pwm(PP3)
dim IR1 as new ADC(PAD05) //ADC object for Sharp Sensor (Front)
dim IR1Result as new byte
dim IR2 as new ADC(PAD03) //ADC object for Sharp Sensor (Back)
dim IR2Result as new byte
dim myChar as new byte //Variable to store received characters
dim S as new SCI(PS0,PS1) //SCI object
dim SPK as new DIO(PM4) // Using Speaker on the SSIM
const ontime = 20
dim duration as new word
Const A2 = 2273 // Music notes
Const A3 = 1136 // Music notes
Const A4 = 568 // Music notes to make sound when robot sees something
dim WLED1 as new DIO(PM2) //LEDs on the wheels
dim WLED2 as new DIO(PM3) //LEDs on the wheels
dim loop as new byte
Const OFF = 0
Const ON = 1
Const FOREVER = 1
Const A = 200
Const B = 10
Const DEL_1MSEC = 1000
sub DelayMsec (in byte milliseconds)
while (milliseconds > 0)
System.Delay (DEL_1MSEC) //Delay 1000 microsecond to make 1 millisecond
milliseconds = milliseconds - 1
end while
end sub
sub stop() // to make motors stop
M00.PWM_Start(PWM_MAIN_CLK,0,250,250)
M01.PWM_Start(PWM_MAIN_CLK,0,250,250)
M10.PWM_Start(PWM_MAIN_CLK,0,250,250)
M11.PWM_Start(PWM_MAIN_CLK,0,250,250)
end sub
sub goback() // robot will go back
M00.PWM_Start(PWM_MAIN_CLK,0,250,180)
M01.PWM_Start(PWM_MAIN_CLK,0,250,250)
M10.PWM_Start(PWM_MAIN_CLK,0,250,180)
M11.PWM_Start(PWM_MAIN_CLK,0,250,250)
end sub
sub turnright() // turn the robot to the right
M00.PWM_Start(PWM_MAIN_CLK,0,250,250)
M01.PWM_Start(PWM_MAIN_CLK,0,250,180)
M10.PWM_Start(PWM_MAIN_CLK,0,250,250)
M11.PWM_Start(PWM_MAIN_CLK,0,250,250)
end sub
sub turnleft() // turn the robot to the left
M00.PWM_Start(PWM_MAIN_CLK,0,250,250)
M01.PWM_Start(PWM_MAIN_CLK,0,250,250)
M10.PWM_Start(PWM_MAIN_CLK,0,250,250)
M11.PWM_Start(PWM_MAIN_CLK,0,250,180)
end sub
sub goahead() // make the robot forward
M00.PWM_Start(PWM_MAIN_CLK,0,250,250)
M01.PWM_Start(PWM_MAIN_CLK,0,250,180) //left dc
M10.PWM_Start(PWM_MAIN_CLK,0,250,250)
M11.PWM_Start(PWM_MAIN_CLK,0,250,180) //right dc
end sub
sub wait3() // my own delays
DelayMsec(A)
DelayMsec(A)
DelayMsec(A)
end sub
sub wait4()
DelayMsec(A)
DelayMsec(A)
DelayMsec(A)
DelayMsec(A)
end sub
sub wait5()
DelayMsec(A)
DelayMsec(A)
DelayMsec(A)
DelayMsec(A)
DelayMsec(A)
end sub
sub wait10() //long delay
loop = 1
while(loop < 11)
DelayMsec(A)
loop =loop + 1
end while
end sub
sub playsound() // to play the notes
duration = ontime
while(duration > 0)
SPK.PIN_Out(PM4,ON)
system.Delay(A2)
SPK.PIN_Out(PM4,Off)
system.Delay(A2)
duration = duration - 1
end while
DelayMsec(B)
duration = ontime
while(duration > 0)
SPK.PIN_Out(PM4,ON)
system.Delay(A3)
SPK.PIN_Out(PM4,Off)
system.Delay(A3)
duration = duration - 1
end while
DelayMsec(B)
duration = ontime
while(duration > 0)
SPK.PIN_Out(PM4,ON)
system.Delay(A4)
SPK.PIN_Out(PM4,Off)
system.Delay(A4)
duration = duration - 1
end while
DelayMsec(B)
end sub
main
PWM.PWM_Res_PP0145 (TIMER_DIV_16, 0)
PWM.PWM_Res_PP23 (TIMER_DIV_16, 0)
S.SER_Setup(SER_BUFFER_4,BAUD9600) //Setup SCI and allow 4 characters to be buffered
System.INTS_On () //TURN ON INTERRUPTS!
S.SER_Put_string("This is a test ")
S.SER_Put_char ('\n')
S.SER_Put_char ('\r')
while (FOREVER)
IR1.ADC_Start (WAIT, ADC_MODE_8ONCE) // Read value from the front sharp sensor
IR1.ADC_Read (PAD05,IR1Result)
IR2.ADC_Start (WAIT, ADC_MODE_8ONCE) // Read value from the back sharp sensor
IR2.ADC_Read (PAD03,IR2Result)
S.SER_Put_decimal(IR2Result,FILLUP_SPACE) // send the value to the windows hyper terminal
S.SER_Put_char ('\n') // make a new line on the hyper terminal
S.SER_Put_char ('\r')
if ((IR1Result == 25) or (IR1Result > 25))
stop()
playsound()
wait5()
WLED1.PIN_Out(PM2,ON)
WLED2.PIN_Out(PM3,ON)
goback()
wait5()
if ((IR2Result == 25) or (IR2Result > 25))
stop()
playsound()
wait5()
turnleft()
wait3()
goahead()
end if
turnright()
wait3()
else
goahead()
end if
if ((IR2Result == 25) or (IR2Result > 25))
WLED1.PIN_Out(PM2,ON)
WLED2.PIN_Out(PM3,ON)
stop()
wait5()
turnright()
wait3()
WLED1.PIN_Out(PM2,OFF)
WLED2.PIN_Out(PM3,OFF)
goahead()
wait3()
else
goahead()
end if
end while
end main
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|













































