The BX code is quite simple, and it's sending, when required, the two potentiometer values as
bytes and the state of the switch:
Option Explicit
dim adc1Var as byte
dim adc2Var as byte
dim adc3Var as byte
dim inByte as byte
dim gotaByte as boolean
dim inputBuffer(1 To 13) As Byte
dim outputBuffer(1 To 10) As Byte
'this is the other
dim switchState as byte
sub main ()
' set up serial port:
call defineCom3(12,11,bx1000_1000)
call openQueue(inputBuffer, 13)
call openQueue(outputBuffer, 10)
call openCom(3,9600,inputBuffer, outputBuffer)
do
' read sensors:
adc1Var = cByte(getADC(13)\4)
adc2Var = cByte(getADC(14)\4)
' adc3Var = cByte(getADC(15)\4)
switchState = getpin(16)
' debug.print "out we go"
debug.print "the switch " & cstr(switchState)
debug.print "pot in pin 13 = " & cstr(adc1Var)
debug.print "pot in pin 14 = " & cstr(adc2Var)
' delay i find useful for debugging in bx
' call delay(0.5)
' read serial data in:
if statusQueue(inputBuffer) = true then
call getQueue(inputBuffer, inByte, 1)
' if you got the message from director, send out data:
if inByte = 65 then
call putQueue(OutputBuffer, adc1Var, 1)
call putQueue(OutputBuffer, adc2Var, 1)
' call putQueue(OutputBuffer, adc3Var, 1)
call putQueue(OutputBuffer,switchState,1)
end if
end if
loop
end sub
The greatest feature was the reinvention of the Joystick: this is what we could call the PROTOTIPE:
On the other side, in director the code going on is way more complex:
(exit frame; behavior script)
global bouncing
on exitFrame
doSerial
go the frame
end
(comunicating with mr Bx; movie script)
global serialObject
global myVar1, myVar2
global switchState
global switchVar
global shootingSprite,displaySprite,Iam
global didUserShoot
global bouncing
--xtra "SerialXtra","js1800@nyu.edu" , "xxxx-xxxx-xxxx-xxxx",90
on startMovie
clearglobals
--------------set variables
displaySprite=1
shootingSprite=3
sprite(displaySprite).loch = 500
sprite(displaySprite).locv = 300
sprite(shootingSprite).loch = 50
sprite(shootingSprite).locv = 50
Iam=["red","orange","green","ghost"]
bouncing=true
-- variables for setting up the xtra.
-- Fill in your own values from the email
-- that you get from the licence registrar:
-- fill in your email address below (see physicalbits.com for details)
programmerName = "js1800@nyu.edu"
serialnum = "YJI2-4BJF-QP2I-LPF4" -- physicalbits.com will mail you this number
licenseLength = 90
-- fill in the name of your serial port below (e.g. COM1 on Windows):
mySerialPort = "COM1"
-- make a new instance of the xtra (mac OSX)
--openxlib the pathname & "serialXtra.osx"
-- use this line instead for Windows:
openxlib the pathname & "serialXtra.xtr"
serialObject = new (xtra "SerialXtra","js1800@nyu.edu" , "YJI2-4BJF-QP2I-LPF4",90 )
-- check that it has been created correctly
if objectP( serialObject ) then
put serialObject
else
alert("Instance not valid")
end if
-- open the serial port:
serialObject.openPort(mySerialPort)
-- set the data rate, start bits, etc:
serialObject.setProtocol(9600, "n", 8, 1)
end
on stopMovie
-- dispose of the xtra and close the xtra file
serialObject.closePort()
set serialObject to 0
closeXlib
end
on doSerial
-- see if the port’s been opened:
If serialObject.isPortOpen() then
-- put "serialObject.isportopen"
If serialObject.charsavailable() = 0 then
-- put "serialObjectCharsavailahle is 0"
--send a byte to ask for data
SerialObject.writeChar("A")
-- put "a was sent"
End if
If SerialObject.charsAvailable() >=3 then
-- put "cahrsavailable>=3"
--first byte sent by microcontroller:
MyVar1 = SerialObject.readNumber()
-- put Myvar1
--next byte sent by microcontroller:
MyVar2 = SerialObject.readNumber()
-- put myvar2
--third one sent by microcontroller:
switchVar = SerialObject.readNumber()
-- put switchState
end if
end if
end
(targetSprite; behavior script attached to the targets)
property displaySprite
property Iam
property hDir,vDir,mida,maximwidth,minimwidth
global bouncing
on startmovie me
end
on beginSprite me
displaySprite = 1
Iam=["red","orange","green","ghost"]
hDir=2
vDir=2
maximwidth=120
minimwidth=30
mida=1
end
on exitframe me
if bouncing=true then
sprite(displaysprite).loch = sprite(displaysprite).loch + hDir
sprite(displaysprite).locv = sprite(displaysprite).locv + vDir
sprite(displaysprite).width = sprite(displaysprite).width + mida
sprite(displaysprite).height = sprite(displaysprite).height + mida
if sprite(displaysprite).width > maximwidth then
mida = -mida
else if sprite(displaysprite).width < minimwidth then
mida = -mida
end if
if sprite(displaysprite).loch > 590 then
hDir = -hDir
end if
if sprite(displaysprite).loch < 50 then
hDir = -hDir
end if
if sprite(displaysprite).locv > 430 then
vDir = -vDir
end if
if sprite(displaysprite).locv < 50 then
vDir = -vDir
end if
end if
end
(shooting; behavior script attached to the pointer)
property lastShootVarState
property ShootingSprite,targetSprite
global MyVar1,MyVar2
global displaysprite
global Iam
global switchVar
global bouncing
on beginsprite me
clearGlobals
shootingSprite=3
targetSprite=1
lastSwitchVar=switchVar
end
on exitFrame me
sprite(shootingSprite).loch = Myvar1* 2.4 + 15
sprite(shootingSprite).locv = Myvar2* 1.75 + 20
updatestage
if SwitchVar = lastShootVarState then
nothing
else
shoot()
lastShootVarState = switchVar
end if
end
on shoot()
put "shooting"
if sprite(shootingSprite).intersects(sprite(targetSprite)) then
put "touche"
touche()
else
nontouchepas()
end if
end
on nontouchepas()
puppetSound "gunfails"
end
on touche()
put "touche"
if sprite(displaysprite).member = member(Iam[4]) then
put "all dead"
else
bouncing=false
starttimer
put "touche"
puppetSound "tocat"
repeat with i=1 to Iam.count-1
if sprite(displaysprite).member = member(Iam[i]) then
sprite(displaysprite).member = member(Iam[i+1])
exit repeat
end if
end repeat
bouncing=true
end if
The only problem with this project was the inconsistence of the shooting device.
I had to make it a switch from pin to ground, which as far as I know is weird, since the BX code is
doing a GET PIN to send the switch state. The switch should be from 5v to pin, and then when the switch
is open (there's no contact between the wires) the getpin would be 0, and 1 on the other case.
But surprisingly this setup would not work. So I had to make the switch from pin to ground. This made the
shooting device go a little wild once in a while, making the gun a really dangerous weapon...
HIPOTHESIS: maybe a resistor would help.
Hipothesis tested: It seems to be better, but not 100%. Anyway,
if I were to go on with this, I should get the bx to sent the shooting
bit everytime the user presses the switch, one shot every time. That would involve
a little change in the bx code in order to make sure it works correctly.
|