Asked
— Edited
Hi @Athena,
I am working on repeatedly pulsing a Sharp IR sensor using JavaScript within my ARC project. Here's a snippet of my current script:
var increment = 3;
var irValue = 50;
var lowIR = ADC.get(2);
sleep(10);
// IR THRESHOLD FOR A HIT
if (lowIR <= irValue) {
missCount = 0;
cycleCount++;
kick = false;
if (scanDir == 0) {
negHit = Servo.getPosition(17);
setVar("$actualCourse", (negHit + 1.5));
Servo.SetPosition(17, (negHit + increment));
scanDir = 1;
} else {
posHit = Servo.getPosition(17);
setVar("$actualCourse", (posHit - 1.5));
Servo.SetPosition(17, (posHit - increment));
scanDir = 0;
}
sleep(1);
}
My issue involves the use of the ADC.get(2) command in this script. The entire script's execution speed decreases by about 75% when including the ADC command compared to running without it. Specifically, without ADC, the cycle count reaches around 400, while it drops below 100 when including the ADC call. Is there a more efficient approach to pulsing the IR sensor to maintain better performance? Thanks, Jack.
Related Hardware (view all EZB hardware)
EZ-B V4
by EZ-Robot
EZ-B v4/2 robot controller: dual Cortex ARM, Wi-Fi, audio/video, 24 servo/digital ports, I2C/UART, camera and Synthiam ARC control for custom robots
Wi-Fi / USB
Servos
24
Camera
Audio
UART
3
I2C
ADC
8
Digital
24
Related Robot Skills (view all robot skills)
ADC Graph
by Synthiam
Historical ADC graph and numeric readout (0-255) for 0-3.3/5V inputs; configurable board/port, sample interval, color, and pause for ezb controllers.
ADC Value
by Synthiam
Display ADC port voltage and 0-255 linear values (0-3.3/5V); configurable board/port, units, multiplier and sample interval.

Short answers first:
Below is an updated, ready-to-drop-in JavaScript you can run in ARC. It assumes you already have a single ADC poller publishing the ADC reading to the global variable $IRADC (the poller you previously used). If you instead use the ADC Value robot skill, make sure the skill publishes a variable named $IRADC or tell me the skill title so I can adapt the code.
Copy/paste this into your scan script window (formatting preserved):
Explanations / rationale
Tuning suggestions
Answers to your two explicit questions:
Next steps / offer
Would you like me to adjust mapping so corrections are based on a smoothed centroid (weighted average) instead of the simple delta -> degree mapping?
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
Hi @Athena,
Reprinted below is the lower portion of the above post:
Question: Since 'actualCourse' has now become the new position, does 'centerPos' need to be changed to the new 'actualCourse' for 'commanded' to update?
Reprint of the beginning portion of the above post:
Thanks Jack
Short answer: Yes - update centerPos to the new actualCourse (in your script) so subsequent minisweeps use the updated center. Right now you command the servo to actualCourse but leave centerPos at the old value, so the next commanded position will still be based on the old center.
Why
Minimal change (drop-in) Replace the block that publishes and commands the servo with this (keeps your existing behavior but updates centerPos):
Extra suggestions (to avoid small oscillations and improve stability)
Follow-up Do you want me to:
Tell me which you prefer and I’ll produce the exact version.
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
Hi @Athena, Please do the following:
Add the exponential smoothing example into your script? Replace the sleep(settleMs + 10) with Servo.waitForMove so movement is guaranteed (slower but cleaner)? Make this change for the full script and return a polished version ready to paste into ARC? Thanks Jack
Thanks Jack - thanks for the clear direction. I updated your scan + trend script with:
Copy/paste the code below into a JavaScript script robot skill in ARC. Make sure you are running a single ADC poller script or the ADC Value skill that writes $IRADC (interval 20-50 ms recommended for WiFi).
Notes and suggestions
Quick checks for me
If you want, I can:
Which follow-up do you want next?
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
Hi @ Athena, Please refer to the print snippet above. It reflects the most recent script post. It appears that 'actualCourse' and 'centerPos' are not updating. It appears that 'desired' is updating and this update is not reflected in the new 'actualCourse' and 'centerPos'. Notice 'cmd' continues to operate using 90 +- 2. Could you please make the changes necessary to the last script provided so that 'actualCourse' and 'centerPos' update whenever 'desired' changes?
Thanks, Jack
Thanks Jack - good catch. The problem was that the script was computing a desired center but never reliably updating centerPos to the new, smoothed actualCourse. I’ve fixed that and cleaned up the buffers and logic so:
Paste this full script into a JavaScript Script skill in ARC (it assumes your ADC poller publishes the IR reading to $IRADC):
What changed (brief):
A couple of notes and small requests:
Would you like me to:
If you paste a small log of expected ADC values when reflector is clearly left/center/right, I can recommend values for diffThreshold and scaleFactor to give well-scaled degree corrections.
I am a customer support GPT-powered AI and still learning. If you have doubts about my response, please use the support documentation to verify my answer.
Hi @Athena,
Here is a print snippet of the most recent script:
Note the differences in L_avgs vs R_avgs. After 6 samples the differences should trigger an updated 'actualCourse', 'centerPos' and 'cmd'. These variables are not updating. Perhaps it is because the following never goes 'true':
Can you please redo the script so that when 'minSamplesForDecision' is met the noted variables above update? Btw, I am using a poller script to provide IR readings as $IRADC. All communications to ARC are via Wifi and not USB. As for the 'actualCourse' an interger is perfect. Thanks Jack