// /* STM32 Blue Pill programmed with Arduino core */
// /* NOTE: Code is heavily commented, serves as an over simplification
// for me to understand what is happening in each line of code. Helped to solidify
// my understanding on how to program microcontrollers for future projects */
// const float BETA = 3950; // should match the Beta Coefficient of the thermistor
// const int sensorPins[] = {A0, A1, A2, A3, A4};
// const int numSensors = 5;
// // Each temp sensor will read data at different rates
// const unsigned long samplingIntervals[] = {1000, 1500, 2000, 2500, 3000};
// // Track the last time each sensor was read
// unsigned long lastReadTimes[numSensors] = {0};
// void setup() {
// Serial.begin(115200);
// Serial.println("Hello, STM32!");
// for (int i = 0; i < numSensors; i++) { // Configure the 5 analog pins to READ data
// pinMode(sensorPins[i], INPUT);
// }
// }
// void loop() {
// //logs the total time the board has been running in ms
// unsigned long currentTime = millis();
// for (int i = 0; i < numSensors; i++) {
// // Check if it's time to read the sensor
// if (currentTime - lastReadTimes[i] >= samplingIntervals[i]) {
// lastReadTimes[i] = currentTime;
// int analogValue = analogRead(sensorPins[i]);
// Serial.print("Sensor ");
// Serial.print(i);
// Serial.print(" - Raw Analog Value: ");
// Serial.println(analogValue);
// if (analogValue > 0 && analogValue < 1023) { // Ensure valid ADC range
// float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
// if (!isnan(celsius) && celsius > -50 && celsius < 150) { // Ensure valid temperature
// Serial.print("Temperature: ");
// Serial.print(celsius);
// Serial.println(" ℃");
// } else {
// Serial.println("Error: Invalid temperature reading");
// }
// } else {
// Serial.println("Error: Invalid analogValue for temperature calculation");
// }
// Serial.println(); // Add spacing for readability
// }
// }
// }
// STM32 Blue Pill - Reading Temperature Sensors with Unique Sampling Rates
#include <Arduino.h>
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
// Define analog pins for sensors
const int SENSOR_PINS[] = {A0, A1, A2, A3, A4}; // Array of sensor pins
const int NUM_SENSORS = 5; // Total number of sensors
// Define sampling rates in milliseconds
const int SAMPLING_RATES[] = {1000, 2000, 3000, 4000, 5000}; // Sampling rates for sensors
// Variables to store sensor values
int sensorValues[NUM_SENSORS] = {0};
// Timers for sampling
unsigned long lastSampleTime[NUM_SENSORS] = {0};
void setup() {
// Initialize serial monitor for debugging
Serial.begin(115200);
// ADC pins are automatically configured as inputs in Arduino
for (int i = 0; i < NUM_SENSORS; i++) {
pinMode(SENSOR_PINS[i], INPUT);
}
Serial.println("STM32 Blue Pill Temperature Sensor Reader Started...");
}
void loop() {
unsigned long currentTime = millis();
// Loop through all sensors
for (int i = 0; i < NUM_SENSORS; i++) {
if (currentTime - lastSampleTime[i] >= SAMPLING_RATES[i]) {
lastSampleTime[i] = currentTime;
Serial.print("Data Logged at: ");
Serial.println(lastSampleTime[i]);
// Read analog value from the sensor
sensorValues[i] = analogRead(SENSOR_PINS[i]);
// Print sensor data
Serial.print("Sensor ");
Serial.print(i + 1);
Serial.print(": ");
Serial.println(sensorValues[i]);
float celsius = 1 / (log(1 / (1023. / sensorValues[i] - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.println(" ℃");
Serial.println();
}
}
// Add a short delay to avoid spamming the Serial Monitor
}