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 Value
by Synthiam
Display ADC port voltage and 0-255 linear values (0-3.3/5V); configurable board/port, units, multiplier and sample interval.
Sharp IR Radar
by Synthiam
Radar-style Sharp IR scanner for EZB ADC: sweeps sensor, displays distance dots, logs ADC voltage, and can steer/avoid obstacles via Movement Panel

Good details - thanks Jack. You’re correct that repeatedly calling ADC.get() inside a tight loop will drastically slow the whole script (and can flood the EZ-B communication channel), especially over Wi-Fi. Below I explain why it’s slow and give three practical, efficient alternatives (with JavaScript examples) you can use right away.
Why ADC.get() slows things down
Options (choose one)
Why this is efficient: ARC handles the periodic reads for you. Your script is not hammering ADC.get() repeatedly and other ARC threads get CPU/EZB time.
Why this is efficient: only the ADC Value skill performs EZB reads. Your script reads a global variable locally - no extra EZB traffic.
Example:
Other considerations / advanced
Next steps I can help with
Useful links
Which option would you like me to implement for you (waitFor*, read the skill variable, or reduce polling with bigger sleep)?
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.
@Athena, For option #1 ( ADC.waitForLower(adcPort, threshold, sampleMs, 0, timeoutMS). Does this ADC call "block" the script from running until timeout or trigger from a "hit"? I require the script to loop as fast as possible to detect deviations from a desired course. Thanks Jack
Short answer: Yes - ADC.waitForLower(...) does block that script’s thread until either the ADC reading meets the condition or the timeout occurs. It does NOT freeze ARC or other robot skills - only that script waits (suspends) efficiently rather than burning CPU or flooding the EZ-B.
Below is what that means for your requirement to "loop as fast as possible," plus two recommended approaches (with copy/paste JavaScript) so you can choose the best trade-off for responsiveness vs communication load.
Option A - Fast, event-driven (use ADC.waitForLower with short sample and short timeout)
Why this helps: the script is not busy-polling with ADC.get(). The EZ-B reads are performed efficiently by ARC’s wait call, and the short timeout gives you periodic wakeups to check or adjust course.
Option B - Fastest loop (single poller + local variable reads)
Why this helps: getVar() is instant and does not generate an EZ-B read. The ADC Value skill is the only thing calling the EZ-B, so communication is consolidated and efficient.
Notes and trade-offs
Quick question so I can give you a ready-to-drop-in script:
If you want, paste the ADC Value skill title / current Interval and I’ll produce exact code referencing that variable and step-by-step kebab-menu settings to configure it. Useful link: ADC Value skill - https://synthiam.com/Support/Skills/Adc/ADC-Value?id=16092
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.
You will have better luck if you explain what you're trying to do. If you do not define a desired outcome, Athena has to guess, just as I would. And I can't quite guess what you're trying to do either. Not because you have incorrect code, just because we only see code and don't know what it's used for. So rather than explain the issue, explain the outcome. (https://synthiam.com/Support/website-features/How-To-Ask-Robot-Building-Questions)
So, looking at your code, it's moving a servo and getting an adc value? But you're doing it as fast as possible (mind you, you have other delays), which has other issues. Such as the servo hasn't finished moving yet and is reading an ADC value. I advise you to explain what you're doing, and that'll make it a lot easier to get help from Athena.
Good points from DJ_Sures - before we optimize code I need a clear statement of the outcome you want so we don’t optimize in the wrong direction.
Quick checklist - please answer these so I can give you a ready-to-drop-in script:
While you answer, here are concrete, tested options and explanations that address DJ_Sures concerns (servo move vs ADC timing) and Jack’s performance issue. Pick one and I’ll adapt it to your exact outcome and timings.
Example (adjust sampleMs/timeoutMS/settle to match your required latency and servo speed):
Why this helps
How to set it up
Script skeleton (replace $MyADCVar with the actual global variable name):
Why this helps
Poller example:
Then other scripts read getVar("$MyADC").
Next steps I can do for you
Useful links / how-tos
Which option do you want me to produce code for? Also answer the quick checklist at top (desired outcome, acceptable latency, WiFi/USB) and I’ll make a final tailored script.
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.