Step 1 is not an issue for me, the problem is from step 2 onwards. I have coding to make the robot go forward and I have coding for the touch sensor so that it can be read in boolean mode.
Ive added an IF statement to the function so that if the touch sensor reads 1 (which means that its hit an object) then the robot should turn slightly to the left, I added repeat function so you can go back to step 2.
So essentially you would have a robot that drives forward until it hits an object, once this happens it will slightly turn left and continue in this same method until the robot is fully free to drive forward.
Here is my coding so far. ( I don't know if this is correct or not)
Function wander()
until( 8 == nxt.ButtonRead() )
end
-- Read touch sensor until the orange button is pressed - note that it's a
-- lot easier to simply read the sensor in boolean mode...
function TouchChange(port, thresh)
nxt.InputSetType(port,0)
local oldState,newState
repeat
if( thresh < nxt.InputGetStatus( port ) ) then
newState = 0
else
newState = 1
end
if newState ~= oldState then
print( nxt.TimerRead(), newState )
oldState = newState
end
-- And using the function - press the orange button on the NXT to stop it
TouchChange(1,500)
-- Sync Motors B (I) and C (II) - the speed is s and the difference is t
function MotorSync(s,t)
nxt.OutputResetTacho(2,1,1,1)
nxt.OutputResetTacho(3,1,1,1)
nxt.OutputSetRegulation(2,2,1)
nxt.OutputSetRegulation(3,2,1)
nxt.DisableNXT( 1 );
nxt.OutputSetSpeed(2,0x20,s, 0, t )
nxt.OutputSetSpeed(3,0x20,s, 0, t )
nxt.DisableNXT( 0 );
repeat
until( 8 == nxt.ButtonRead() )
nxt.OutputSetSpeed(2)
nxt.OutputSetSpeed(3)
end
repeat
if newState = 0 then
MotorSync(75,0)
else newState = 1
MotorSync(75,20)
When i use this coding all i get is the robot driving forward, the touch does not respond when it hits and object, so I dont know if my touch function is wrong or the IF statement
Thanks
Rajni