Asked
— Edited
Another script I've been writing is for the IR object avoidance as the built in control for IR doesn't work correctly with my IR sensor. I'm also trying to get to grips with EZ-Script so playing around is always a great idea in my book.
Adjust and/or comment on as you see fit. I've written it so it can be easily adjusted to suit your settings (and to show how variables can be used to make for easy adjustment over different projects/people)
# IR Detector Script
# Object avoidance using IR sensor on ADC port
# Adjust values below for configuration
$iradcport = ADC6 # Change for ADC Port with sensor attached
$maxirdistance = 35 # Change for maximum distance from object before avoiding in units
$irreverseturn = 0 # Reverse before turn? 0 = no, 1 = yes
$irturndirection = 0 # Change 0 = left or 1 = right
$irturnamount = 500 # Change for how long to turn for in ms
$irreverseamount = 500 # Change for how long to reverse for in ms (if applicable)
$irmovementspeed = 255 # Change for movement speed
# -- Do not modify below this line --
Goto(detect)
:avoid
IF ($irreverseturn = 1)
reverse($irmovementspeed,$irreverseamount)
ELSEIF ($irturndirection = 0)
Left($irmovementspeed,$irturnamount)
ELSE
Right($irmovementspeed,$irturnamount)
ENDIF
Return()
:detect
$currirdistance = GetADC($iradcport)
IF ($currirdistance >= $maxirdistance)
Goto(avoid)
ENDIF
Sleep (50)
Goto(detect)
The :Detect should be a label called detect but I guess the forum thought putting the d next to a : meant I was grinning
Hahahaha! That's funny... And fixed
Now i have to fix the real smilies
Edit: there good to go!
DJ always on the ball even with the smallest of issues. Although I kinda liked the
in the code, it gave character.
Updated the code (although not the EZ-Cloud version yet as I am at work... don't tell the boss), it had problems with the reverse before turn, as in I had an ELSEIF after it not an ENDIF, so it would back up when detecting an object but didn't turn.
This is why I like sections and posts specifically for scripting, hopefully this will help show others how things work
Anyway, updated code is as follows (changed in original post also changed back in original post so others can follow the changes);
Nice
I would recommend putting the :Detect code above the :Avoid... That way it jumps right into :Detect and you won't need the Goto(Detect) anymore. Your way works, of course... It's just one less command
For the life of me I don't know why I structured it around that way, I can only think I was tired when I wrote it or had other intentions (very possibly as part of my roaming script I plan to write that this will be a part of). Although it does demonstrate the Goto and Return commands.
I'll reorganise it later I expect as I want to add in the option to only check when moving forwards, which I think should be a simple IF command using the $Direction global variable.
I may also add in options for additional detectors, servo mounting, capturing images and video (and anything else anyone can think of to throw in the mix)... more for educational purposes than function - it makes it fun learning that way
version 1.4 Added in option to only check while moving forwards using the global variable $Direction. Restructured. Commands in CAPS, variables and labels in lowercase.
Updated on the cloud too
I'm not sure on the best way around for the GOTO(detect), ENDIF after SLEEP(50) in the detectstart script, as far as I can tell it doesn't matter but would be interested in knowing which way around is considered the best practice, if there is such a thing...
Rich, is it much work to make this script for two IR, one for each side, on a 45 facing in for a full view? Thanks. I have been trying. and im still new. lol, Thanks
Here's an example for 3 IR units and a front bump switch with thanks from Rich: