This code was largely written by ChatGPT (version 3.5 was used). It took a few iterations and some testing and feedback from me, but for simple, straightforward effects in CircuitPython, ChatGPT is surprisingly easy to use. It's an amazing tool for folks who don't have a coding background, but have a clear vision of what the code should do. I first installed the Mu Editor to check each time I had changes.
I could copy and paste each iteration of ChatGPT's code into the Mu editor and load it onto my CircuitPlayground Bluefruit, testing and making requests until it did exactly what I wanted. ChatGPT made its share of mistakes, but when I told it what errors were being thrown, it came back every time with a corrected version.
Once I felt the code was finished, I ran it through Pylint to check for errors and it came back with a 100% perfect score the very first time.
Here is a transcription of the prompts I used and the testing process I went through.
Chat GPT Transcript
Erin: please write me some CircuitPython code that uses the sound sensor on the Circuit Playground Bluefruit. When the sound threshold is reached, I would like to sequentially light a strip of 20 NeoPixels soldered to pin A1 one at a time until they're all lit, then turn them off again all at once. Please have them turn on in a warm yellow color.
ChatGPT: Certainly! Below is a simple example of CircuitPython code that achieves the described functionality using the sound sensor and NeoPixels on the Circuit Playground Bluefruit:
(code sample using AudioIO library)
This code uses the Circuit Playground Bluefruit sound sensor to detect sound levels. When the sound level surpasses the defined threshold, it sequentially lights up the NeoPixels in a warm yellow color, then turns them off simultaneously. You can adjust the SOUND_THRESHOLD
value and the delays to suit your specific requirements.
Erin: can you please use the Circuit Playground library instead of Audioio
?
ChatGPT: Certainly! Below is an updated version of the CircuitPython code using the Circuit Playground library to interact with the sound sensor on the Circuit Playground Bluefruit:
(code sample using CircuitPlayground library)
In this code, I replaced the AudioIO
library with the Circuit Playground library's cp.sound_level
to read the sound level from the built-in microphone. The rest of the code remains similar, sequentially lighting up NeoPixels and turning them off when the sound threshold is reached. Adjust the SOUND_THRESHOLD
value and delays as needed for your specific use case.
Erin: can you please add some code so the pixels on the face of the board also light up sequentially alongside the strip on A1?
ChatGPT: Certainly! You can modify the code to include the NeoPixels on the face of the Circuit Playground Bluefruit. Here's an updated version that lights up both the NeoPixels on pin A1 and the ones on the face of the board sequentially when the sound threshold is reached:
(code with NeoPixel onboard lights added)
This code introduces a new set of NeoPixels (pixels_face
) for the face of the Circuit Playground Bluefruit. The code sequentially lights up both the NeoPixels on pin A1 and the face when the sound threshold is reached. Adjust the constants, colors, and delays according to your preferences.
Erin: great this is close. Can you please make it keep listening after turning the pixels off so it triggers each time the sound threshold is reached?
ChatGPT: Certainly! To keep the code continuously listening for sound and triggering the NeoPixels each time the sound threshold is reached, you can modify the structure of the code. Here's an updated version:
(code that keeps listening)
Now, the main loop keeps running continuously, allowing the code to keep listening for sound even after turning off the NeoPixels. The NeoPixels will trigger each time the sound threshold is reached. Adjust the delays and other parameters as needed for your specific use case.
Erin: This code throws an error in my serial monitor:
code.py output:
Traceback (most recent call last):
File "code.py", line 28, in
IndexError: index out of range
ChatGPT: It seems there might be an issue with the loop that sequentially lights up the NeoPixels on the face of the Circuit Playground Bluefruit. Let's make sure the loop indices are within the valid range. Here's a corrected version:
(corrected code)
This version includes checks (if i < len(pixels_strip) and if i < len(pixels_face))
to ensure that the indices are within the valid range of the NeoPixels array. Try uploading this corrected code to your Circuit Playground Bluefruit and see if the issue persists.
Erin: That fixed it. Can you add some debugging code so it prints out the sound level in the serial monitor?
ChatGPT: Certainly! You can use the print statement to output the sound level to the serial monitor for debugging. Here's the modified code with debugging statements:
(code with sound level printing in serial monitor)
Now, the sound level will be printed to the serial monitor, helping you debug and understand the values being read from the microphone. Upload this modified code to your Circuit Playground Bluefruit and check the serial monitor for the sound level information.
Erin: here is the new code with a few changes I made. Can you please modify this code so that the pixels stay on for 2 seconds after they're all lit before going off?
(pasted in my modified code)
ChatGPT: Certainly! I've modified your code to include a 2-second delay after lighting up all the pixels. Here's the updated code:
(my modified code with 2 second delay)
Erin: This is great. Can you make the 2 second delay a variable so I can modify it from the top of the code?
ChatGPT: Certainly! You can introduce a variable for the delay duration at the top of the code. Here's the modified code with a variable for the delay duration:
(code with delay variable)
Now, you can easily modify the DELAY_AFTER_LIGHT_UP
variable at the top of the code to adjust the delay duration after lighting up all pixels. Upload this modified code and experiment with different delay durations as needed.
Erin: Can we also make the color a variable so users can modify it together with the other variables at the top of the code?
ChatGPT: Certainly! You can introduce a variable for the color at the top of the code. Here's the modified code with a variable for the color:
(code with color variable added)
Erin: Thank you so much, this is perfect.
Text editor powered by tinymce.