7 Commits

53 changed files with 23486 additions and 99726 deletions

View File

@ -0,0 +1,37 @@
*
.subckt 0004-DenshaBekutoru_v0.2
.model __D1 D
.model __D2 D
J6 __J6
J5 __J5
D10 __D10
R3 Net-_A1-Vcc_ Net-_D10-A_ 220
R1 Net-_D2-A_ Net-_D1-K_ 1.8k
U2 __U2
D1 Net-_D1-A_ Net-_D1-K_ __D1
U1 __U1
C4 Net-_D3-K_ GND 4.7u
C5 Net-_D3-K_ GND 100n
D3 __D3
RV1 __RV1
D11 __D11
R4 Net-_D11-A_ Net-_A1-Vcc_ 220
J8 __J8
R9 GND Net-_J8-Pin_1_ 10k
R2 Net-_D1-A_ Net-_D2-K_ 1.8k
J1 __J1
D2 Net-_D2-A_ Net-_D2-K_ __D2
A1 __A1
R5 Net-_J3-Pin_2_ Net-_A1-PadD9_ 220
J7 __J7
J3 __J3
J2 __J2
J4 __J4
R6 Net-_J3-Pin_3_ Net-_A1-PadD9_ 220
R7 Net-_J3-Pin_4_ Net-_A1-D3_INT1_ 220
R8 Net-_J3-Pin_5_ Net-_A1-D3_INT1_ 220
.ends

View File

@ -0,0 +1,25 @@
"Reference","Value","Datasheet","Footprint","Qty","DNP"
"A1","Arduino_Pro_Mini_Socket_NoSPH_V2","https://docs.arduino.cc/retired/boards/arduino-pro-mini","arduino-library:Arduino_Pro_Mini_Socket_NoSPH_V2","1",""
"C4","4.7µF","~","Capacitor_THT:CP_Axial_L10.0mm_D6.0mm_P15.00mm_Horizontal","1",""
"C5","100nF","~","Capacitor_THT:C_Disc_D3.0mm_W1.6mm_P2.50mm","1",""
"D1,D2","1N4148","https://assets.nexperia.com/documents/data-sheet/1N4148_1N4448.pdf","Diode_THT:D_DO-35_SOD27_P7.62mm_Horizontal","2",""
"D3","1N5819","http://www.vishay.com/docs/88525/1n5817.pdf","Diode_THT:D_DO-41_SOD81_P10.16mm_Horizontal","1",""
"D4","LED Green","~","LED_THT:LED_D3.0mm","1",""
"D5","LED Yellow","~","LED_THT:LED_D3.0mm","1",""
"D10","LED: HL_A.1","","LED_THT:LED_D5.0mm_Clear","1",""
"D11","LED: HL_A.2","","LED_THT:LED_D5.0mm_Clear","1",""
"D12","LED: HL_B.1","","LED_THT:LED_D5.0mm_Clear","1",""
"D13","LED: HL_B.2","","LED_THT:LED_D5.0mm_Clear","1",""
"J1","from Lego","~","Connector_PinHeader_2.54mm:PinHeader_1x04_P2.54mm_Vertical","1",""
"J2","Headlight GND","~","Connector_PinSocket_2.54mm:PinSocket_1x04_P2.54mm_Vertical","1",""
"J3","Headlight GND; A1,2/B1,2","~","Connector_PinSocket_2.54mm:PinSocket_1x05_P2.54mm_Vertical","1",""
"J4","Headlight Test","~","Connector_PinHeader_2.54mm:PinHeader_1x05_P2.54mm_Horizontal","1",""
"J5","I2C","~","Connector_PinHeader_2.54mm:PinHeader_1x03_P2.54mm_Vertical","1",""
"J6","TX/RX","~","Connector_PinHeader_2.54mm:PinHeader_1x03_P2.54mm_Vertical","1",""
"J7","Add. Lights","~","Connector_PinSocket_2.54mm:PinSocket_1x04_P2.54mm_Vertical","1",""
"J8","LDR (~1MOhm) Option","~","Connector_PinHeader_2.54mm:PinHeader_1x02_P2.54mm_Vertical","1",""
"R1,R2","1.5k","~","Resistor_THT:R_Axial_DIN0204_L3.6mm_D1.6mm_P5.08mm_Horizontal","2",""
"R3,R4,R5,R6,R7,R8","220","~","Resistor_THT:R_Axial_DIN0204_L3.6mm_D1.6mm_P5.08mm_Horizontal","6",""
"R9","10k","~","Resistor_THT:R_Axial_DIN0204_L3.6mm_D1.6mm_P5.08mm_Horizontal","1",""
"RV1","1MOhm","~","Potentiometer_THT:Potentiometer_Piher_PT-10-V10_Vertical","1",""
"U1,U2","PC817","http://www.soselectronic.cz/a_info/resource/d/pc817.pdf","Package_DIP:DIP-4_W7.62mm","2",""
1 Reference Value Datasheet Footprint Qty DNP
2 A1 Arduino_Pro_Mini_Socket_NoSPH_V2 https://docs.arduino.cc/retired/boards/arduino-pro-mini arduino-library:Arduino_Pro_Mini_Socket_NoSPH_V2 1
3 C4 4.7µF ~ Capacitor_THT:CP_Axial_L10.0mm_D6.0mm_P15.00mm_Horizontal 1
4 C5 100nF ~ Capacitor_THT:C_Disc_D3.0mm_W1.6mm_P2.50mm 1
5 D1,D2 1N4148 https://assets.nexperia.com/documents/data-sheet/1N4148_1N4448.pdf Diode_THT:D_DO-35_SOD27_P7.62mm_Horizontal 2
6 D3 1N5819 http://www.vishay.com/docs/88525/1n5817.pdf Diode_THT:D_DO-41_SOD81_P10.16mm_Horizontal 1
7 D4 LED Green ~ LED_THT:LED_D3.0mm 1
8 D5 LED Yellow ~ LED_THT:LED_D3.0mm 1
9 D10 LED: HL_A.1 LED_THT:LED_D5.0mm_Clear 1
10 D11 LED: HL_A.2 LED_THT:LED_D5.0mm_Clear 1
11 D12 LED: HL_B.1 LED_THT:LED_D5.0mm_Clear 1
12 D13 LED: HL_B.2 LED_THT:LED_D5.0mm_Clear 1
13 J1 from Lego ~ Connector_PinHeader_2.54mm:PinHeader_1x04_P2.54mm_Vertical 1
14 J2 Headlight GND ~ Connector_PinSocket_2.54mm:PinSocket_1x04_P2.54mm_Vertical 1
15 J3 Headlight GND; A1,2/B1,2 ~ Connector_PinSocket_2.54mm:PinSocket_1x05_P2.54mm_Vertical 1
16 J4 Headlight Test ~ Connector_PinHeader_2.54mm:PinHeader_1x05_P2.54mm_Horizontal 1
17 J5 I2C ~ Connector_PinHeader_2.54mm:PinHeader_1x03_P2.54mm_Vertical 1
18 J6 TX/RX ~ Connector_PinHeader_2.54mm:PinHeader_1x03_P2.54mm_Vertical 1
19 J7 Add. Lights ~ Connector_PinSocket_2.54mm:PinSocket_1x04_P2.54mm_Vertical 1
20 J8 LDR (~1MOhm) Option ~ Connector_PinHeader_2.54mm:PinHeader_1x02_P2.54mm_Vertical 1
21 R1,R2 1.5k ~ Resistor_THT:R_Axial_DIN0204_L3.6mm_D1.6mm_P5.08mm_Horizontal 2
22 R3,R4,R5,R6,R7,R8 220 ~ Resistor_THT:R_Axial_DIN0204_L3.6mm_D1.6mm_P5.08mm_Horizontal 6
23 R9 10k ~ Resistor_THT:R_Axial_DIN0204_L3.6mm_D1.6mm_P5.08mm_Horizontal 1
24 RV1 1MOhm ~ Potentiometer_THT:Potentiometer_Piher_PT-10-V10_Vertical 1
25 U1,U2 PC817 http://www.soselectronic.cz/a_info/resource/d/pc817.pdf Package_DIP:DIP-4_W7.62mm 2

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
{ {
"board": { "board": {
"active_layer": 0, "active_layer": 0,
"active_layer_preset": "", "active_layer_preset": "All Layers",
"auto_track_width": true, "auto_track_width": true,
"hidden_netclasses": [], "hidden_netclasses": [],
"hidden_nets": [], "hidden_nets": [],

View File

@ -2,12 +2,224 @@
"board": { "board": {
"3dviewports": [], "3dviewports": [],
"design_settings": { "design_settings": {
"defaults": {}, "defaults": {
"diff_pair_dimensions": [], "apply_defaults_to_fp_fields": false,
"drc_exclusions": [], "apply_defaults_to_fp_shapes": false,
"rules": {}, "apply_defaults_to_fp_text": false,
"track_widths": [], "board_outline_line_width": 0.05,
"via_dimensions": [] "copper_line_width": 0.2,
"copper_text_italic": false,
"copper_text_size_h": 1.5,
"copper_text_size_v": 1.5,
"copper_text_thickness": 0.3,
"copper_text_upright": false,
"courtyard_line_width": 0.05,
"dimension_precision": 4,
"dimension_units": 3,
"dimensions": {
"arrow_length": 1270000,
"extension_offset": 500000,
"keep_text_aligned": true,
"suppress_zeroes": false,
"text_position": 0,
"units_format": 1
},
"fab_line_width": 0.1,
"fab_text_italic": false,
"fab_text_size_h": 1.0,
"fab_text_size_v": 1.0,
"fab_text_thickness": 0.15,
"fab_text_upright": false,
"other_line_width": 0.1,
"other_text_italic": false,
"other_text_size_h": 1.0,
"other_text_size_v": 1.0,
"other_text_thickness": 0.15,
"other_text_upright": false,
"pads": {
"drill": 0.762,
"height": 1.524,
"width": 1.524
},
"silk_line_width": 0.1,
"silk_text_italic": false,
"silk_text_size_h": 1.0,
"silk_text_size_v": 1.0,
"silk_text_thickness": 0.1,
"silk_text_upright": false,
"zones": {
"min_clearance": 0.5
}
},
"diff_pair_dimensions": [
{
"gap": 0.0,
"via_gap": 0.0,
"width": 0.0
}
],
"drc_exclusions": [
"courtyards_overlap|116847500|113255000|4b518c68-cacc-4ba0-bdd7-4ed203d87dd8|c5b5c5e2-5bf5-406f-bc2c-791c87e4bd5a",
"courtyards_overlap|116847500|116747500|4b518c68-cacc-4ba0-bdd7-4ed203d87dd8|ce87971b-d681-44d7-9fb7-23a15933148a",
"courtyards_overlap|126372500|116747500|1094a2e1-479d-4870-8b87-1411fc6da792|4b518c68-cacc-4ba0-bdd7-4ed203d87dd8",
"courtyards_overlap|130322874|113255000|4b518c68-cacc-4ba0-bdd7-4ed203d87dd8|ee659d06-72c1-451a-8938-35a721a086e2"
],
"meta": {
"version": 2
},
"rule_severities": {
"annular_width": "error",
"clearance": "error",
"connection_width": "warning",
"copper_edge_clearance": "error",
"copper_sliver": "warning",
"courtyards_overlap": "error",
"diff_pair_gap_out_of_range": "error",
"diff_pair_uncoupled_length_too_long": "error",
"drill_out_of_range": "error",
"duplicate_footprints": "warning",
"extra_footprint": "warning",
"footprint": "error",
"footprint_symbol_mismatch": "warning",
"footprint_type_mismatch": "ignore",
"hole_clearance": "error",
"hole_near_hole": "error",
"holes_co_located": "warning",
"invalid_outline": "error",
"isolated_copper": "warning",
"item_on_disabled_layer": "error",
"items_not_allowed": "error",
"length_out_of_range": "error",
"lib_footprint_issues": "warning",
"lib_footprint_mismatch": "warning",
"malformed_courtyard": "error",
"microvia_drill_out_of_range": "error",
"missing_courtyard": "ignore",
"missing_footprint": "warning",
"net_conflict": "warning",
"npth_inside_courtyard": "ignore",
"padstack": "warning",
"pth_inside_courtyard": "ignore",
"shorting_items": "error",
"silk_edge_clearance": "ignore",
"silk_over_copper": "warning",
"silk_overlap": "warning",
"skew_out_of_range": "error",
"solder_mask_bridge": "error",
"starved_thermal": "error",
"text_height": "warning",
"text_thickness": "warning",
"through_hole_pad_without_hole": "error",
"too_many_vias": "error",
"track_dangling": "warning",
"track_width": "error",
"tracks_crossing": "error",
"unconnected_items": "error",
"unresolved_variable": "error",
"via_dangling": "warning",
"zones_intersect": "error"
},
"rules": {
"max_error": 0.005,
"min_clearance": 0.0,
"min_connection": 0.0,
"min_copper_edge_clearance": 0.5,
"min_hole_clearance": 0.25,
"min_hole_to_hole": 0.25,
"min_microvia_diameter": 0.2,
"min_microvia_drill": 0.1,
"min_resolved_spokes": 2,
"min_silk_clearance": 0.0,
"min_text_height": 0.8,
"min_text_thickness": 0.08,
"min_through_hole_diameter": 0.3,
"min_track_width": 0.25,
"min_via_annular_width": 0.1,
"min_via_diameter": 0.5,
"solder_mask_to_copper_clearance": 0.0,
"use_height_for_length_calcs": true
},
"teardrop_options": [
{
"td_onpadsmd": true,
"td_onroundshapesonly": false,
"td_ontrackend": false,
"td_onviapad": true
}
],
"teardrop_parameters": [
{
"td_allow_use_two_tracks": true,
"td_curve_segcount": 0,
"td_height_ratio": 1.0,
"td_length_ratio": 0.5,
"td_maxheight": 2.0,
"td_maxlen": 1.0,
"td_on_pad_in_zone": false,
"td_target_name": "td_round_shape",
"td_width_to_size_filter_ratio": 0.9
},
{
"td_allow_use_two_tracks": true,
"td_curve_segcount": 0,
"td_height_ratio": 1.0,
"td_length_ratio": 0.5,
"td_maxheight": 2.0,
"td_maxlen": 1.0,
"td_on_pad_in_zone": false,
"td_target_name": "td_rect_shape",
"td_width_to_size_filter_ratio": 0.9
},
{
"td_allow_use_two_tracks": true,
"td_curve_segcount": 0,
"td_height_ratio": 1.0,
"td_length_ratio": 0.5,
"td_maxheight": 2.0,
"td_maxlen": 1.0,
"td_on_pad_in_zone": false,
"td_target_name": "td_track_end",
"td_width_to_size_filter_ratio": 0.9
}
],
"track_widths": [
0.0,
0.508,
1.27
],
"tuning_pattern_settings": {
"diff_pair_defaults": {
"corner_radius_percentage": 80,
"corner_style": 1,
"max_amplitude": 1.0,
"min_amplitude": 0.2,
"single_sided": false,
"spacing": 1.0
},
"diff_pair_skew_defaults": {
"corner_radius_percentage": 80,
"corner_style": 1,
"max_amplitude": 1.0,
"min_amplitude": 0.2,
"single_sided": false,
"spacing": 0.6
},
"single_track_defaults": {
"corner_radius_percentage": 80,
"corner_style": 1,
"max_amplitude": 1.0,
"min_amplitude": 0.2,
"single_sided": false,
"spacing": 0.6
}
},
"via_dimensions": [
{
"diameter": 0.0,
"drill": 0.0
}
],
"zones_allow_external_fillets": false
}, },
"ipc2581": { "ipc2581": {
"dist": "", "dist": "",
@ -283,8 +495,8 @@
"last_paths": { "last_paths": {
"gencad": "", "gencad": "",
"idf": "", "idf": "",
"netlist": "", "netlist": "../../../../../../",
"plot": "", "plot": "/home/tgohle/Desktop/git/ToGo-Lab/0004-DenshaBekutoru/KiCad/0004-DenshaBekutoru_PCB_v0.2/",
"pos_files": "", "pos_files": "",
"specctra_dsn": "", "specctra_dsn": "",
"step": "", "step": "",
@ -295,7 +507,7 @@
}, },
"schematic": { "schematic": {
"annotate_start_num": 0, "annotate_start_num": 0,
"bom_export_filename": "", "bom_export_filename": "0004-DenshaBekutoru_v0.2.csv",
"bom_fmt_presets": [], "bom_fmt_presets": [],
"bom_fmt_settings": { "bom_fmt_settings": {
"field_delimiter": ",", "field_delimiter": ",",
@ -345,11 +557,35 @@
"label": "DNP", "label": "DNP",
"name": "${DNP}", "name": "${DNP}",
"show": true "show": true
},
{
"group_by": false,
"label": "#",
"name": "${ITEM_NUMBER}",
"show": false
},
{
"group_by": false,
"label": "Sim.Device",
"name": "Sim.Device",
"show": false
},
{
"group_by": false,
"label": "Sim.Pins",
"name": "Sim.Pins",
"show": false
},
{
"group_by": false,
"label": "Description",
"name": "Description",
"show": false
} }
], ],
"filter_string": "", "filter_string": "",
"group_symbols": true, "group_symbols": true,
"name": "Grouped By Value", "name": "",
"sort_asc": true, "sort_asc": true,
"sort_field": "Reference" "sort_field": "Reference"
}, },

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 292 KiB

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,90 @@
# DenshaBekutoru - Model Train Direction Sensor # 電車ベクトル (DenshaBekutoru)
Get **direction** and **speed** from motor power signals. ## Model Train Direction Sensor
Target use case: **model trains**.
- Detects polarity (+/) to identify train direction DenshaBekutoru is a small controller board for brick-built model locomotives.
- Measures pulse width to calculate speed (% of maximum)
- Implemented with optocouplers and a state machine
- As small as possible, but I hope no SMD (beginner friendly)
Project is in an early stage. Currently running tests on target hardware. The target use case is: read the motor power signals, detect the current driving direction, and switch the headlights accordingly. The software is also prepared to derive speed information from the motor signal behaviour.
More information and documentation will follow.
This project is built around one practical problem: the motor is driven from an H-bridge, so the signal is noisy, polarity changes are not “clean logic”, and inductive spikes make direct evaluation difficult.
## What it does
- detects train direction from the motor power signal
- evaluates two processed motor-side signals via analogue inputs
- switches the front / rear lights according to direction
- keeps the last valid direction if the train stops or the signal becomes unclear
- is designed as small as possible for installation in brick-built locomotives
## Technical approach
The current version is built around:
- optocoupler-based input isolation and signal conditioning
- Arduino Pro Mini 5V as controller
- software averaging of analogue inputs
- startup calibration to adapt to different controllers and builds
- threshold + hysteresis logic
- simple state machine with direction memory
- PWM-capable output pins for possible later LED dimming
The main idea is not to read one raw signal and react immediately.
The idea is to turn a noisy motor signal into a stable direction decision.
## Current status
**Version 2 (beta) is built and working.**
Current state:
- [x] schematic finished
- [x] PCB finished
- [x] firmware finished for the current beta state
- [x] three PCBs built and tested
- [x] first boards handed over for real-life testing in a model railway club
- [ ] Feedback and what to improve
## Design goals
- as small as possible
- beginner-friendly where possible
- no SMD if it can reasonably be avoided
- easy to reproduce later as a DIY kit
- modular PCB concept with main section, future-option section, and test section
## Repository content
Typical content of this repository:
- `KiCad/` schematic and PCB files
- `firmware/` Arduino test and controller software
- documentation and pictures will be added step by step
## Project status note
This is still a beta project.
The current hardware and software already work, but testing on real locomotives is still ongoing.
The next version will depend on real-life feedback from actual use.
Possible next steps:
- verify behaviour with different motor controllers
- check robustness in real train builds
- improve wiring / connector handling
- finalise optional features like LED dimming
- prepare a first “official” DIY kit version
## Blog / documentation
- Project overview: https://togo-lab.io/?p=223
- Hardware description: https://togo-lab.io/?p=233
- Software description: https://togo-lab.io/?p=243
## Short summary
DenshaBekutoru is a small Arduino-based direction sensor for brick-built model trains.
It reads the noisy motor signal from an H-bridge-driven setup, isolates and evaluates it, and switches the headlights automatically.
The current beta version is working and now entering real-life testing.

View File

@ -0,0 +1,287 @@
/*
0004_DenshaBekutoru 1st Version PCB
PCB Version : 2026/02 (Order F016895)
Target dev board : Arduino Pro Mini 5V / 16 MHz (ATmega328P)
----------------------------------------------------------------------------
State machine (direction memory) + Hysteresis (Version A)
----------------------------------------------------------------------------
We use two thresholds:
T_hold (smaller): below -> treat as neutral and HOLD state
T_enter (larger) : above -> allow direction change (set by sign)
Behavior:
If |diff| <= T_hold : hold direction
If |diff| >= T_enter : set direction by sign
Else (between) : hold direction
Presets (implemented as multipliers of the calibrated VdiffThreshold):
T_hold = 1.0 * VdiffThreshold
T_enter = 2.0 * VdiffThreshold
Inputs derived from v1, v2: diff = v1 - v2
*/
const unsigned long SAMPLE_WINDOW_MS = 100; // averaging window per measurement
const unsigned long SAMPLE_DELAY_US = 500; // delay between ADC samples (~2 kHz)
const uint8_t ADC_PIN_1 = A0; // Pro Mini A0 -> (PCB dependend!)
const uint8_t ADC_PIN_2 = A1; // Pro Mini A1 -> (PCB dependend")
// Direction LEDs (avoid D0/D1 because you may want RX/TX later)
const uint8_t LED_DIR1_PIN = 3; // D3
const uint8_t LED_DIR2_PIN = 9; // D9
const unsigned int LED_BLINK_ON_MS = 150;
const unsigned int LED_BLINK_OFF_MS = 150;
// Serial output control (0 = no output, 1 = output)
bool SerialOutputAllow = true;
// ---- Calibration parameters ----
const uint16_t AverageRepeat = 100; // number of init measurements
const float VdiffThresholdMargin = 1.50f; // multiplier (e.g., 1.5 = +50% margin)
const float VdiffThresholdMinVolts = 0.03f; // floor in volts
// ---- Hysteresis presets (multipliers of VdiffThreshold) ----
const float THOLD_MULT = 1.0f; // T_hold = THOLD_MULT * VdiffThreshold
const float TENTER_MULT = 2.0f; // T_enter = TENTER_MULT * VdiffThreshold
// Globals: latest measured averaged voltages
float v1 = 0.0f;
float v2 = 0.0f;
// Calibration globals
float VdiffBaseline = 0.0f; // median(|V1-V2|) measured at boot (no movement)
float VdiffThreshold = 0.0f; // calibrated base threshold (used to derive T_hold/T_enter)
// Hysteresis thresholds (derived once after calibration)
float T_hold = 0.0f;
float T_enter = 0.0f;
// Direction encoding:
// 0 = NONE, 2 = DIR1, 3 = DIR2
byte direction = 0;
static inline float absf(float x) { return (x >= 0.0f) ? x : -x; }
static inline float maxf(float a, float b) { return (a > b) ? a : b; }
// -----------------------------------------------------------------------------
// LED helpers
// -----------------------------------------------------------------------------
static void blinkLED(uint8_t pin, uint8_t times)
{
for (uint8_t i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(LED_BLINK_ON_MS);
digitalWrite(pin, LOW);
delay(LED_BLINK_OFF_MS);
}
}
static void initBlinkSequence()
{
// LED at Output 2 blinks 2 times
blinkLED(LED_DIR1_PIN, 2);
// LED at Output 3 blinks 3 times
blinkLED(LED_DIR2_PIN, 3);
}
static void applyDirectionOutputs()
{
digitalWrite(LED_DIR1_PIN, LOW);
digitalWrite(LED_DIR2_PIN, LOW);
if (direction == 2) {
digitalWrite(LED_DIR1_PIN, HIGH);
} else if (direction == 3) {
digitalWrite(LED_DIR2_PIN, HIGH);
}
}
// -----------------------------------------------------------------------------
// Measurement
// -----------------------------------------------------------------------------
static void measureAveragedVoltages()
{
unsigned long startTime = millis();
unsigned long sum1 = 0;
unsigned long sum2 = 0;
unsigned long samples = 0;
while (millis() - startTime < SAMPLE_WINDOW_MS) {
sum1 += analogRead(ADC_PIN_1);
sum2 += analogRead(ADC_PIN_2);
samples++;
delayMicroseconds(SAMPLE_DELAY_US);
}
float avg1 = (float)sum1 / samples;
float avg2 = (float)sum2 / samples;
// Convert ADC value to voltage (default analog reference = Vcc ~ 5V)
v1 = avg1 * (5.0f / 1023.0f);
v2 = avg2 * (5.0f / 1023.0f);
}
// -----------------------------------------------------------------------------
// Median helpers (for calibration)
// -----------------------------------------------------------------------------
static void sortFloatArray(float *a, uint16_t n)
{
// Insertion sort (n=100 is small, OK)
for (uint16_t i = 1; i < n; i++) {
float key = a[i];
int16_t j = (int16_t)i - 1;
while (j >= 0 && a[j] > key) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}
}
static float medianOfSortedFloatArray(const float *a, uint16_t n)
{
if (n == 0) return 0.0f;
if (n & 1) {
return a[n / 2];
} else {
return (a[(n / 2) - 1] + a[n / 2]) * 0.5f;
}
}
// Calibrate baseline + VdiffThreshold once at boot/reset
static void calibrateVdiffThreshold(uint16_t averageRepeat)
{
if (averageRepeat < 1) averageRepeat = 1;
if (averageRepeat > 200) averageRepeat = 200; // RAM safety cap
float diffs[200];
for (uint16_t i = 0; i < averageRepeat; i++) {
measureAveragedVoltages();
diffs[i] = absf(v1 - v2); // baseline mismatch sample
}
sortFloatArray(diffs, averageRepeat);
VdiffBaseline = medianOfSortedFloatArray(diffs, averageRepeat);
// Calibrated base threshold with margin + floor
VdiffThreshold = maxf(VdiffBaseline * VdiffThresholdMargin, VdiffThresholdMinVolts);
// Derive hysteresis thresholds (Version A)
T_hold = THOLD_MULT * VdiffThreshold;
T_enter = TENTER_MULT * VdiffThreshold;
}
// -----------------------------------------------------------------------------
// State machine with hysteresis (Version A)
// -----------------------------------------------------------------------------
static void updateDirectionFromVoltages()
{
const float diff = v1 - v2;
const float absDiff = absf(diff);
// 1) Neutral region: HOLD
if (absDiff <= T_hold) {
return;
}
// 2) Strong region: allow direction change
if (absDiff >= T_enter) {
direction = (diff > 0.0f) ? (byte)2 : (byte)3;
return;
}
// 3) Deadband between T_hold and T_enter: HOLD
return;
}
// -----------------------------------------------------------------------------
// Serial output
// -----------------------------------------------------------------------------
static void serialPrintAll()
{
if (!SerialOutputAllow) return;
Serial.print("A2 for PB3 XTAL1, Pin2: ");
Serial.print(v1, 3);
Serial.print(" V | ");
Serial.print("A3 for PB4 XTAL2, Pin3: ");
Serial.print(v2, 3);
Serial.print(" V | ");
Serial.print("|V1-V2|: ");
Serial.print(absf(v1 - v2), 3);
Serial.print(" V | ");
Serial.print("VdiffBaseline: ");
Serial.print(VdiffBaseline, 3);
Serial.print(" V | ");
Serial.print("VdiffThreshold: ");
Serial.print(VdiffThreshold, 3);
Serial.print(" V | ");
Serial.print("T_hold: ");
Serial.print(T_hold, 3);
Serial.print(" V | ");
Serial.print("T_enter: ");
Serial.print(T_enter, 3);
Serial.print(" V | ");
Serial.print("direction: ");
Serial.println(direction);
}
// -----------------------------------------------------------------------------
// Arduino entry points
// -----------------------------------------------------------------------------
void setup() {
Serial.begin(115200);
pinMode(ADC_PIN_1, INPUT);
pinMode(ADC_PIN_2, INPUT);
pinMode(LED_DIR1_PIN, OUTPUT);
pinMode(LED_DIR2_PIN, OUTPUT);
// Initial calibration (assumes no movement)
calibrateVdiffThreshold(AverageRepeat);
// Init blink sequence: 2x on D2, 3x on D3
initBlinkSequence();
// One measurement for initial display + initial direction
measureAveragedVoltages();
updateDirectionFromVoltages();
applyDirectionOutputs();
if (SerialOutputAllow) {
Serial.println("=== DenshaBekutoru Optocoupler Average Test (Arduino Pro Mini 5V/16MHz) ===");
Serial.print("AverageRepeat: ");
Serial.println(AverageRepeat);
Serial.print("THOLD_MULT: ");
Serial.print(THOLD_MULT, 2);
Serial.print(" TENTER_MULT: ");
Serial.println(TENTER_MULT, 2);
}
serialPrintAll();
}
void loop() {
measureAveragedVoltages();
updateDirectionFromVoltages();
applyDirectionOutputs();
serialPrintAll();
delay(300);
}