Create a complete, highly interactive p5.js simulation demonstrating: electrical circuit with parallel resistors
CRITICAL REQUIREMENTS:
ACCURACY: The simulation must be scientifically/mathematically accurate for electrical circuit with parallel resistors
FUNCTIONAL INTERACTIVITY: All sliders and buttons MUST actually control and modify the simulation behavior in real-time
EDUCATIONAL: Users should learn about electrical circuit with parallel resistors through experimentation and parameter manipulation
VISUAL PRECISION: Animations should be smooth, realistic, and visually accurate to real-world physics
MANDATORY INTERACTIVE ELEMENTS THAT MUST WORK:
At least 4-6 sliders controlling key parameters with realistic ranges - these MUST change simulation behavior
At least 3-4 buttons for actions (reset, pause/play, presets, etc.) - these MUST perform actual functions
Real-time parameter display showing current values that update as sliders move
Visual feedback that responds immediately and visibly to control changes
Mouse interactions where physically meaningful
CRITICAL: CONTROLS MUST BE FUNCTIONAL, NOT DECORATIVE
Every slider must control a variable that actively affects the simulation
Every button must perform a meaningful action that changes the simulation state
Parameter changes must be immediately visible in the simulation output
Use the actual slider values in mathematical calculations and physics equations
ANIMATION AND TIMING REQUIREMENTS:
Use realistic time scaling - avoid animations that are too fast or unrealistic
Implement proper frame rate control using deltaTime or fixed timesteps
Use realistic physical constants and scaling factors
Ensure smooth, stable animations at 60fps
Avoid conflicting visual elements or overlapping controls
Make animations visually appealing and scientifically accurate
CONTROL IMPLEMENTATION REQUIREMENTS:
Sliders: Use createSlider(min, max, default, step) with appropriate ranges that make physical sense
Buttons: Use createButton('Label') with meaningful functions that actually execute
Position controls below canvas using .position(x, y) - ensure no overlap
Show parameter values with text() in the main canvas that update in real-time
Use function declarations for event handlers: function resetSim() {}
ALWAYS use slider.value() in your calculations - never ignore slider inputs
CODE FORMAT REQUIREMENTS:
Assistant
Use GLOBAL MODE p5.js (NOT instance mode)
Define setup() and draw() as regular functions
All variables should be global (use let/var at top level)
Use standard p5.js function names: createCanvas, background, fill, etc.
NO sketch wrapper function - write direct p5.js code
SIMULATION ACCURACY FOR ELECTRICAL CIRCUIT WITH PARALLEL RESISTORS:
Use correct physics equations, mathematical relationships, or algorithmic principles
Realistic parameter ranges and default values that make physical sense
Smooth, continuous animations with proper time scaling
Proper scaling and units where applicable
Include scientific constants or realistic coefficients
ENSURE every parameter controlled by sliders affects the simulation visibly
Use appropriate time steps and scaling factors to avoid too-fast animations
TEXT AND DISPLAY REQUIREMENTS:
Show ONLY the simulation title at the top
Display ONLY key physics parameters (velocity, acceleration, force, etc.) - no instructions or explanations
Keep text minimal and clean - maximum 4-5 parameter displays
NO instruction text like "Click to...", "Drag to...", etc.
Use concise parameter labels like "Velocity: 5.2 m/s" not "Current velocity value is 5.2"
Position parameter text neatly in corners or edges, not cluttering the center
TEXT AND DISPLAY REQUIREMENTS:
Show ONLY the simulation title at the top
Display ONLY key physics parameters (velocity, acceleration, force, etc.) - no instructions or explanations
Keep text minimal and clean - maximum 4-5 parameter displays
NO instruction text like "Click to...", "Drag to...", etc.
Use concise parameter labels like "Velocity: 5.2 m/s" not "Current velocity value is 5.2"
Position parameter text neatly in corners or edges, not cluttering the center
VISUAL REQUIREMENTS:
Clear, educational visualization of the concept
Multiple visual elements (trails, vectors, measurements, etc.) that don't conflict
Color coding to distinguish different aspects
Grid lines, axes, or reference markers where helpful
Immediate and obvious visual response to parameter changes
Smooth, realistic motion that matches real-world physics
Proper visual hierarchy - important elements should stand out
Clean, uncluttered display with minimal text
Clean, uncluttered display with minimal text
EXAMPLE OF FUNCTIONAL CONTROLS WITH PROPER TIMING:
javascript
// Global variables that ACTUALLY affect simulation
let gravitySlider, dampingSlider;
let gravity = 0.5; // This gets updated by slider
let damping = 0.99; // This gets updated by slider
let resetBtn, pauseBtn;
let isPaused = false;
let timeScale = 0.02; // Control animation speed
function setup() {
createCanvas(400, 400);
// Create sliders that CONTROL the simulation with realistic ranges
gravitySlider = createSlider(0.1, 2.0, 0.5, 0.1);
gravitySlider.position(10, height + 10);
dampingSlider = createSlider(0.95, 1.0, 0.99, 0.01);
dampingSlider.position(150, height + 10);
// Create buttons that DO something - positioned to avoid overlap
resetBtn = createButton('Reset');
resetBtn.position(10, height + 40);
pauseBtn = createButton('Pause/Play');
pauseBtn.position(80, height + 40);
}
function draw() {
background(220);
// ACTUALLY USE the slider values - this is critical!
gravity = gravitySlider.value();
damping = dampingSlider.value();
if (!isPaused) {
// Use gravity and damping in actual physics calculations with proper time scaling
velocity += gravity * timeScale;
velocity *= damping;
position += velocity;
}
// Show ONLY title and key parameters - keep it minimal
fill(0);
textAlign(CENTER);
text('Electrical Circuit With Parallel Resistors', width/2, 20);
textAlign(LEFT);
text('Gravity: ' + gravity.toFixed(2), 10, 40);
text('Velocity: ' + velocity.toFixed(2), 10, 60);
// Draw simulation that responds to parameters with realistic motion
ellipse(position, height/2, 20, 20);
}
// Functions that actually work
function resetSimulation() {
position = width/2;
velocity = 0;
}
function togglePause() {
isPaused = !isPaused;
}
IMPORTANT:
Focus on the specific physics, mathematics, or principles of electrical circuit with parallel resistors
Make controls that ACTUALLY matter for understanding the concept
Every slider must control a variable used in the simulation
Every button must perform a visible action
Ensure the simulation behaves realistically and responds to all controls
Use proper time scaling to avoid unrealistically fast animations
Position controls carefully to avoid visual conflicts
Make animations smooth and scientifically accurate
Keep text displays MINIMAL - only title and key parameters
NO instruction text or explanations in the simulation
Only output JavaScript code, no explanations
Use global mode p5.js format (no sketch wrapper)
Position controls below the 400x400 canvas with proper spacing
Generate an accurate, interactive simulation of: electrical circuit with parallel resistors with WORKING controls, REALISTIC timing, and MINIMAL text displays
javascript
// Electrical Circuit with Parallel Resistors Simulation
let canvasW = 600;
let canvasH = 400;
let voltageSlider, r1Slider, r2Slider, r3Slider, rLoadSlider, freqSlider;
let resetBtn, pauseBtn, presetBtn;
let isPaused = false;
let time = 0;
let timeStep = 0.016; // ~60fps, seconds per frame
// Circuit parameters (updated from sliders)
let V = 10; // Voltage (Volts)
let R1 = 100; // Resistor 1 (Ohms)
let R2 = 100; // Resistor 2 (Ohms)
let R3 = 100; // Resistor 3 (Ohms)
let Rload = 100; // Load resistor (Ohms)
let freq = 0; // Frequency (Hz) for optional AC animation (0 = DC)
let I1, I2, I3, Itotal, Req;
let electronPositions = [];
let electronSpeedScale = 2000; // scaling factor for electron speed visualization
// Colors
const wireColor = '#444';
const resistorColor = '#d35400';
const electronColor = '#2980b9';
const currentArrowColor = '#27ae60';
const voltageColor = '#c0392b';
function setup() {
createCanvas(canvasW, canvasH);
frameRate(60);
// Sliders: Voltage 1-20V, Resistors 10-1000 Ohms, Frequency 0-60Hz
voltageSlider = createSlider(1, 20, 10, 0.1);
voltageSlider.position(10, canvasH + 10);
voltageSlider.style('width', '120px');
r1Slider = createSlider(10, 1000, 100, 1);
r1Slider.position(150, canvasH + 10);
r1Slider.style('width', '120px');
r2Slider = createSlider(10, 1000, 100, 1);
r2Slider.position(290, canvasH + 10);
r2Slider.style('width', '120px');
r3Slider = createSlider(10, 1000, 100, 1);
r3Slider.position(430, canvasH + 10);
r3Slider.style('width', '120px');
rLoadSlider = createSlider(10, 1000, 100, 1);
rLoadSlider.position(10, canvasH + 50);
rLoadSlider.style('width', '120px');
freqSlider = createSlider(0, 60, 0, 1);
freqSlider.position(150, canvasH + 50);
freqSlider.style('width', '120px');
// Buttons
resetBtn = createButton('Reset');
resetBtn.position(300, canvasH + 50);
resetBtn.mousePressed(resetSim);
pauseBtn = createButton('Pause/Play');
pauseBtn.position(370, canvasH + 50);
pauseBtn.mousePressed(togglePause);
presetBtn = createButton('Preset: Balanced');
presetBtn.position(470, canvasH + 50);
presetBtn.mousePressed(loadPreset);
// Initialize electrons for animation (along wires)
initElectrons();
}
function draw() {
background(245);
// Update parameters from sliders
V = voltageSlider.value();
R1 = r1Slider.value();
R2 = r2Slider.value();
R3 = r3Slider.value();
Rload = rLoadSlider.value();
freq = freqSlider.value();
// Calculate equivalent resistance for parallel resistors R1, R2, R3
let parallelRecip = 0;
if (R1 > 0) parallelRecip += 1 / R1;
if (R2 > 0) parallelRecip += 1 / R2;
if (R3 > 0) parallelRecip += 1 / R3;
Req = (parallelRecip > 0) ? 1 / parallelRecip : 1e9; // avoid div by zero
// Total resistance including load resistor in series
let Rtotal = Req + Rload;
// Calculate total current using Ohm's law: I = V / Rtotal
// For AC frequency > 0, simulate sinusoidal voltage (Vpeak = V)
let voltageInstant = V;
if (freq > 0) {
voltageInstant = V * sin(TWO_PI * freq * time);
}
Itotal = voltageInstant / Rtotal;
// Currents through each parallel resistor branch: I_branch = V_parallel / R_branch
// Voltage across parallel resistors = V_parallel = Itotal * Req
let Vparallel = Itotal * Req;
I1 = Vparallel / R1;
I2 = Vparallel / R2;
I3 = Vparallel / R3;
// Update time if not paused
if (!isPaused) {
time += timeStep;
}
// Draw grid for reference
drawGrid();
// Draw circuit schematic with animation
drawCircuit();
// Draw electrons flowing with speed proportional to current
animateElectrons();
// Draw parameter text (minimal, clean)
drawParameters();
}
// Initialize electron positions on each wire segment
function initElectrons() {
electronPositions = [];
// Define electron groups for each branch and main wire
// Each group: array of electrons with position (0-1 along path)
// Main wire before parallel branches (left to right)
electronPositions.push({
path: 'main_in',
positions: [],
speedFactor: 0.5,
count: 15
});
// Branch 1 (top branch)
electronPositions.push({
path: 'branch1',
positions: [],
speedFactor: 1,
count: 12
});
// Branch 2 (middle branch)
electronPositions.push({
path: 'branch2',
positions: [],
speedFactor: 1,
count: 12
});
// Branch 3 (bottom branch)
electronPositions.push({
path: 'branch3',
positions: [],
speedFactor: 1,
count: 12
});
// Main wire after parallel branches (right to left)
electronPositions.push({
path: 'main_out',
positions: [],
speedFactor: 0.5,
count: 15
});
// Initialize positions randomly spaced
for (let group of electronPositions) {
group.positions = [];
for (let i = 0; i < group.count; i++) {
group.positions.push(random());
}
}
}
// Draw grid lines for reference
function drawGrid() {
stroke(220);
strokeWeight(1);
for (let x = 50; x < width - 50; x += 50) {
line(x, 50, x, height - 50);
}
for (let y = 50; y < height - 50; y += 50) {
line(50, y, width - 50, y);
}
noStroke();
}
// Draw the circuit schematic with wires, resistors, voltage source, and current arrows
function drawCircuit() {
push();
translate(50, height / 2);
stroke(wireColor);
strokeWeight(3);
noFill();
// Coordinates for key points
// Left terminal (Voltage source)
let Vx = 0;
let Vy = 0;
// Junction points
let J1x = 150;
let J1y = 0;
let J2x = 350;
let J2y = 0;
// Branch vertical offsets
let branchOffset = 80;
// Draw voltage source (circle with + and -)
fill(voltageColor);
strokeWeight(2);
ellipse(Vx, Vy, 40, 40);
strokeWeight(1);
line(Vx - 10, Vy, Vx + 10, Vy);
line(Vx, Vy - 10, Vx, Vy + 10);
noFill();
stroke(voltageColor);
textAlign(CENTER, CENTER);
textSize(16);
text('V', Vx, Vy);
stroke(wireColor);
strokeWeight(3);
// Wire from voltage source to junction 1
line(Vx + 20, Vy, J1x - 20, J1y);
// Junction 1 circle
fill(wireColor);
circle(J1x, J1y, 10);
noFill();
// Wire from junction 2 to load resistor
line(J2x + 20, J2y, J2x + 80, J2y);
// Load resistor rectangle
drawResistor(J2x + 80, J2y - 15, 60, 30, 'Rload');
// Wire from load resistor to voltage source (closing loop)
line(J2x + 140, J2y, Vx + 20, Vy);
// Wire from junction 1 to junction 2 (top branch)
line(J1x, J1y, J2x, J2y);
// Branch 1 resistor (top)
drawResistor((J1x + J2x) / 2, J1y - branchOffset, 60, 30, 'R1');
// Branch 2 resistor (middle)
drawResistor((J1x + J2x) / 2, J1y, 60, 30, 'R2');
// Branch 3 resistor (bottom)
drawResistor((J1x + J2x) / 2, J1y + branchOffset, 60, 30, 'R3');
// Branch wires vertical connections
// Top branch vertical wires
line(J1x, J1y, J1x, J1y - branchOffset);
line(J2x, J2y, J2x, J2y - branchOffset);
// Middle branch horizontal line already drawn (junction 1 to 2)
// Bottom branch vertical wires
line(J1x, J1y, J1x, J1y + branchOffset);
line(J2x, J2y, J2x, J2y + branchOffset);
// Draw junction circles at branch connections
fill(wireColor);
circle(J1x, J1y - branchOffset, 8);
circle(J2x, J2y - branchOffset, 8);
circle(J1x, J1y + branchOffset, 8);
circle(J2x, J2y + branchOffset, 8);
noFill();
// Draw current arrows on wires and branches
drawCurrentArrow(Vx + 30, Vy, J1x - 30, J1y, Itotal, 'I total');
drawCurrentArrow(J1x, J1y - branchOffset + 10, J2x, J2y - branchOffset + 10, I1, 'I1');
drawCurrentArrow(J1x, J1y + 10, J2x, J2y + 10, I2, 'I2');
drawCurrentArrow(J1x, J1y + branchOffset - 10, J2x, J2y + branchOffset - 10, I3, 'I3');
drawCurrentArrow(J2x + 90, J2y, J2x + 130, J2y, Itotal, 'I total');
pop();
}
// Draw resistor symbol at (x,y) center with width w and height h
function drawResistor(x, y, w, h, label) {
push();
translate(x, y);
stroke(resistorColor);
strokeWeight(3);
noFill();
// Zigzag resistor shape
let zigzagCount = 6;
let segment = w / zigzagCount;
beginShape();
vertex(-w / 2, 0);
for (let i = 0; i < zigzagCount; i++) {
let x1 = -w / 2 + i * segment;
let y1 = (i % 2 === 0) ? -h / 2 : h / 2;
vertex(x1 + segment / 2, y1);
}
vertex(w / 2, 0);
endShape();
// Label resistor value
noStroke();
fill(resistorColor);
textAlign(CENTER, CENTER);
textSize(14);
let val;
switch (label) {
case 'R1': val = R1; break;
case 'R2': val = R2; break;
case 'R3': val = R3; break;
case 'Rload': val = Rload; break;
default: val = 0;
}
text(label + '\n' + nf(val, 0, 0) + ' Ω', 0, -h);
pop();
}
// Draw current arrow from (x1,y1) to (x2,y2) with magnitude I
function drawCurrentArrow(x1, y1, x2, y2, I, label = '') {
push();
stroke(currentArrowColor);
strokeWeight(3);
fill(currentArrowColor);
// Draw line
line(x1, y1, x2, y2);
// Draw arrowhead
let angle = atan2(y2 - y1, x2 - x1);
push();
translate(x2, y2);
rotate(angle);
triangle(0, 0, -10, 5, -10, -5);
pop();
// Label current magnitude near arrow midpoint
noStroke();
fill(currentArrowColor);
textSize(14);
textAlign(CENTER, CENTER);
let Iabs = abs(I);
text(label + '\n' + nf(Iabs, 1, 3) + ' A', (x1 + x2) / 2, (y1 + y2) / 2 - 15);
pop();
}
// Animate electrons flowing along wires and branches
function animateElectrons() {
push();
translate(50, height / 2);
noStroke();
fill(electronColor);
// Electron radius
let r = 5;
// Electron groups correspond to electronPositions array
// Main wire before parallel branches (left to right)
let mainIn = electronPositions[0];
let speedMain = abs(Itotal) * electronSpeedScale * mainIn.speedFactor;
for (let i = 0; i < mainIn.count; i++) {
mainIn.positions[i] += speedMain * timeStep;
if (mainIn.positions[i] > 1) mainIn.positions[i] -= 1;
let x = lerp(20, 130, mainIn.positions[i]);
let y = 0;
ellipse(x, y, r, r);
}
// Branch 1 (top branch)
let branch1 = electronPositions[1];
let speedB1 = abs(I1) * electronSpeedScale * branch1.speedFactor;
for (let i = 0; i < branch1.count; i++) {
branch1.positions[i] += speedB1 * timeStep;
if (branch1.positions[i] > 1) branch1.positions[i] -= 1;
// Path: vertical down from J1 to resistor, zigzag resistor, vertical up to J2
let pos = branch1.positions[i];
let x, y;
if (pos < 0.25) {
// vertical down J1 to resistor top
x = 150;
y = lerp(0, -80, pos / 0.25);
} else if (pos < 0.75) {
// across resistor zigzag horizontally
x = lerp(150, 350, (pos - 0.25) / 0.5);
y = -80;
} else {
// vertical up resistor bottom to J2
x = 350;
y = lerp(-80, 0, (pos - 0.75) / 0.25);
}
ellipse(x, y, r, r);
}
// Branch 2 (middle branch)
let branch2 = electronPositions[2];
let speedB2 = abs(I2) * electronSpeedScale * branch2.speedFactor;
for (let i = 0; i < branch2.count; i++) {
branch2.positions[i] += speedB2 * timeStep;
if (branch2.positions[i] > 1) branch2.positions[i] -= 1;
// Path: horizontal from J1 to J2 through resistor
let pos = branch2.positions[i];
let x = lerp(150, 350, pos);
let y = 0;
ellipse(x, y, r, r);
}
// Branch 3 (bottom branch)
let branch3 = electronPositions[3];
let speedB3 = abs(I3) * electronSpeedScale * branch3.speedFactor;
for (let i = 0; i < branch3.count; i++) {
branch3.positions[i] += speedB3 * timeStep;
if (branch3.positions[i] > 1) branch3.positions[i] -= 1;
// Path: vertical up from J1 to resistor bottom, zigzag resistor, vertical down to J2
let pos = branch3.positions[i];
let x, y;
if (pos < 0.25) {
// vertical up J1 to resistor bottom
x = 150;
y = lerp(0, 80, pos / 0.25);
} else if (pos < 0.75) {
// across resistor zigzag horizontally
x = lerp(150, 350, (pos - 0.25) / 0.5);
y = 80;
} else {
// vertical down resistor top to J2
x = 350;
y = lerp(80, 0, (pos - 0.75) / 0.25);
}
ellipse(x, y, r, r);
}
// Main wire after parallel branches (right to left)
let mainOut = electronPositions[4];
let speedMainOut = abs(Itotal) * electronSpeedScale * mainOut.speedFactor;
for (let i = 0; i < mainOut.count; i++) {
mainOut.positions[i] += speedMainOut * timeStep;
if (mainOut.positions[i] > 1) mainOut.positions[i] -= 1;
let x = lerp(490, 370, mainOut.positions[i]);
let y = 0;
ellipse(x, y, r, r);
}
pop();
}
// Draw minimal parameter text displays
function drawParameters() {
fill(0);
noStroke();
textSize(14);
textAlign(CENTER, CENTER);
// Title top center
textStyle(BOLD);
text('Electrical Circuit With Parallel Resistors', width / 2, 20);
textStyle(NORMAL);
// Left top corner: Voltage and Frequency
textAlign(LEFT, TOP);
text('Voltage: ' + nf(V, 1, 2) + ' V', 10, 40);
text('Frequency: ' + nf(freq, 0, 1) + ' Hz', 10, 60);
// Right top corner: Equivalent Resistance and Total Current
textAlign(RIGHT, TOP);
text('Req: ' + nf(Req, 0, 1) + ' Ω', width - 10, 40);
text('I total: ' + nf(abs(Itotal), 1, 4) + ' A', width - 10, 60);
// Left bottom corner: Individual branch currents
textAlign(LEFT, BOTTOM);
text('I1: ' + nf(abs(I1), 1, 4) + ' A', 10, height - 40);
text('I2: ' + nf(abs(I2), 1, 4) + ' A', 10, height - 20);
text('I3: ' + nf(abs(I3), 1, 4) + ' A', 10, height - 0);
}
// Button functions
function resetSim() {
voltageSlider.value(10);
r1Slider.value(100);
r2Slider.value(100);
r3Slider.value(100);
rLoadSlider.value(100);
freqSlider.value(0);
time = 0;
isPaused = false;
initElectrons();
}
function togglePause() {
isPaused = !isPaused;
}
function loadPreset() {
// Balanced preset values
voltageSlider.value(12);
r1Slider.value(220);
r2Slider.value(220);
r3Slider.value(220);
rLoadSlider.value(100);
freqSlider.value(0);
time = 0;
isPaused = false;
initElectrons();
}