Reputation: 1
I am trying to control the intensity of an LED using a PWM port on an Arduino, and capture with a camera the resulting brightness of the LED at a given setting. What I would expect is that at a setting of zero, the LED power is minimized, and at a setting of 1, power is maximized.
The issue is that I do not know how to wait for the Arduino to get written to before running the command to take an image capture, as seen in the plot below of the collected data:
Captured LED power (4 different LEDs) as a function of PWM input:
As seen above, there are spikes in the intensity near zero since the LED that was previously on hasn't yet shut off. Here is a snippet of the code being used:
# Iterate over each LED
for i in range(4):
board.pass_time(0.4)
time.sleep(0.4)
# Reset all pins to zero
for j in range(4):
led_pins[j].write(0)
# Select the relevant pin
pin = led_pins[i]
# Iterate over each data value
for j in range(N):
print(i, j)
# Set the power of the given LED
pin.write(vals[j])
board.pass_time(0.4)
time.sleep(0.4)
# os.wait()
# # Start a thread to take a capture
# thread = threading.Thread(target=take_capture_thread, args=(cam,))
# thread.start()
# # Wait to rejoin with thread
# thread.join()
# Take a capture
for k in range(10):
cam.issue_software_trigger()
frame = cam.get_pending_frame_or_null()
capture = frame.image_buffer
# Measure the average power at the center of the image
data[i, j] = np.average(capture[selection])
Even with longer durations inputted into board.pass_time() and time.sleep(), the issue is not resolved. I presume that the issue is that these commands are run before the code has finished writing to the Arduino, which is why they have no effect.
I'm wondering how I can make sure a command writing to the Arduino is completed before running a time.sleep() command to make sure that the camera is capturing the correct data, and is not delayed.
I have tried using time.sleep() and board.pass_time() to make sure that the Arduino has time to update before taking a capture, but even with long durations, the issue persists.
Upvotes: 0
Views: 40
Reputation: 1
The issue had to do with a previous line in my code:
# Arm the camera
cam.arm(frames_to_buffer=4)
Apparently, the consequence of this is that there is a buffer such that when I ran this snippet:
for k in range(10):
cam.issue_software_trigger()
frame = cam.get_pending_frame_or_null()
capture = frame.image_buffer
the capture that was grabbed was from three data points prior, even if with the for loop taking multiple captures. I suppose this is because the buffer only gets written to upon running:
frame = cam.get_pending_frame_or_null()
Upvotes: 0