From a79b373fe82b3a8f9c518944bd5c142ca923e19e Mon Sep 17 00:00:00 2001 From: Sean O'Connor Date: Tue, 28 Jan 2025 19:29:53 -0500 Subject: [PATCH] Initial commit --- .gitignore | 5 +++ .vscode/extensions.json | 10 +++++ platformio.ini | 17 ++++++++ src/main.cpp | 87 +++++++++++++++++++++++++++++++++++++++++ test/README | 11 ++++++ 5 files changed, 130 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 platformio.ini create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..8d0a408 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,17 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:uno_r4_minima] +platform = renesas-ra +board = uno_r4_minima +framework = arduino +lib_deps = + adafruit/Adafruit AS7341@^1.4.1 + adafruit/Adafruit BusIO@^1.16.2 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..0b82a0a --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,87 @@ +#include +#include +#include + +Adafruit_AS7341 as7341; + +int measurement = -1; +int baselineValue = 0; +bool reactionComplete = false; +unsigned long startTime = 0; +const int triggerPin = 2; +const int SIGNIFICANT_DECREASE = 5000; +bool triggerArmed = false; // Add this to wait for pin to stabilize + +void setup() { + Serial.begin(115200); + while (!Serial) delay(10); + + Wire.begin(); + delay(100); + + Serial.println("Looking for AS7341 sensor..."); + if (!as7341.begin()) { + Serial.println("Could not find AS7341 sensor. Check wiring!"); + while (1) delay(10); + } + + Serial.println("AS7341 sensor initialized!"); + + // Configure sensor + as7341.setATIME(100); + as7341.setASTEP(999); + as7341.setGain(AS7341_GAIN_256X); + as7341.enableLED(false); + + pinMode(triggerPin, INPUT_PULLUP); + + // Wait for trigger pin to stabilize to HIGH + delay(100); + if (digitalRead(triggerPin) == HIGH) { + triggerArmed = true; + // Serial.println("Trigger armed and ready!"); + } +} + +void loop() { + delay(50); + + if (!as7341.readAllChannels()) { + Serial.println("Error reading channels"); + return; + } + + measurement = as7341.getChannel(AS7341_CHANNEL_630nm_F7); + + // Print trigger state periodically + static unsigned long lastTriggerDebug = 0; + if (millis() - lastTriggerDebug > 500) { + // Serial.print("Trigger pin state: "); + // Serial.println(digitalRead(triggerPin)); + lastTriggerDebug = millis(); + } + + // Start timer when trigger is pulled (goes from HIGH to LOW) and take baseline + if (triggerArmed && !reactionComplete && digitalRead(triggerPin) == LOW && startTime == 0) { + baselineValue = measurement; + startTime = millis(); + Serial.println("Reaction started!"); + Serial.print("Baseline value: "); + Serial.println(baselineValue); + } + + // Check for completion if reaction has started + if (!reactionComplete && startTime > 0) { + if (baselineValue - measurement >= SIGNIFICANT_DECREASE) { + reactionComplete = true; + float duration = (millis() - startTime) / 1000.0; + Serial.print("Reaction complete! Duration: "); + Serial.print(duration); + Serial.println(" seconds"); + } + } + + Serial.print('>'); + Serial.print("630nm:"); + Serial.println(measurement); +} \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html