#include <SoftwareSerial.h>
// Pins for RS232 communication
const int rs232TxPin = 10; // TX pin for RS232 communication
const int rs232RxPin = 11; // RX pin for RS232 communication
// Pins for toggle button and LED indicators
const int startButtonPin = 2; // Pin for toggle button
const int numIndicatorLEDs = 13; // Number of indicator LEDs
const int indicatorLEDs[] = {3, 4, 5, 6, 7, 8, 9, 12, 13, 14, 15, 16, 17}; // Pins for indicator LEDs
// Pins for relay control
const int relayPin = 18; // Pin for controlling the relay
// Threshold for short circuit detection
const int threshold = 100; // Adjust as needed
bool scanningStarted = false; // Flag to indicate if scanning has started
int currentPin = 0; // Current pin being checked
SoftwareSerial rs232(rs232RxPin, rs232TxPin); // Create a SoftwareSerial object
void setup() {
Serial.begin(9600); // Initialize serial communication for debugging
rs232.begin(9600); // Initialize RS232 communication with multimeter
pinMode(startButtonPin, INPUT_PULLUP); // Set start button as input with internal pull-up resistor
// Set indicator LED pins as outputs
for (int i = 0; i < numIndicatorLEDs; i++) {
pinMode(indicatorLEDs[i], OUTPUT);
digitalWrite(indicatorLEDs[i], LOW); // Turn off all LEDs initially
}
// Set relay pin as output
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Initially turn off relay
}
void loop() {
// Check if the start button is pressed to toggle scanning
if (digitalRead(startButtonPin) == LOW) {
scanningStarted = !scanningStarted; // Toggle scanning status
// Reset current pin index when scanning is started
if (scanningStarted) {
currentPin = 0;
}
delay(500); // Debouncing delay
}
// If scanning is started, proceed with short circuit check
if (scanningStarted) {
// Activate relay to measure resistance on current pin
digitalWrite(relayPin, HIGH);
// Wait for relay to settle
delay(100);
// Check if data is available from multimeter
if (rs232.available() > 0) {
// Read the value from multimeter
int value = rs232.parseInt();
// Check for short circuit
bool isShort = value < threshold;
// Update LED indicator based on short circuit status
digitalWrite(indicatorLEDs[currentPin], isShort ? HIGH : LOW);
// Move to the next pin for the next iteration
currentPin = (currentPin + 1) % numIndicatorLEDs;
// Print debug information
Serial.print("Reading from multimeter: ");
Serial.print(value);
Serial.print(" - Pin ");
Serial.print(currentPin);
Serial.println(isShort ? " - Short circuit detected!" : " - No short circuit detected.");
delay(1000); // Delay between readings
}
// Deactivate relay after measurement
digitalWrite(relayPin, LOW);
} else {
// Turn off all indicator LEDs if scanning is not started
for (int i = 0; i < numIndicatorLEDs; i++) {
digitalWrite(indicatorLEDs[i], LOW);
}
}
}