# Ohm's law exercice

Good morning. Thanks to jb4x I been able to draw a simple electric circuit

boolean isOpen;
float Vpila=10.0;
float R1=100.00;
float Rv=500.00;
float DR=5.00;
float color_alpha=0;
float I=0.00;

void setup() {
size(600, 600);
isOpen = true;
}

void draw() {
background(200); // CLear the screen
fill(255);
ellipse (300, 150, 40, 40);
line (280, 150, 120, 150);
line (120, 150, 120, 220);
stroke (0);
if (!isOpen) {
line (120, 220, 120, 290); //closed circuit
}
line (120, 290, 120, 380);
if (isOpen) {
line (80, 240, 120, 290); // open circuit
//line (120,220,120,300);
}
line (320, 150, 500, 150);
line (500, 150, 500, 250);
line (500, 250, 500, 380);
line (500, 380, 350, 380);
rect (250, 350, 100, 50);
line (250, 380, 120, 380);
ellipse (500, 250, 40, 40);
if (isOpen) {
fill(0);
} else {
fill(255);
}
ellipse (500, 250, 40, 40);
line (270, 320, 325, 425);
}

void mouseClicked() {
isOpen = !isOpen;
}

And works fine

The purpose of the exercise is to control with up & down the value of the variable resistance, and thus making the alpha channel of the bulb higher or lower.

I have a concept like this one

void setup() {
Vpila = 10.0; // Battery voltage in volts
Rl = 100.0; // Bulb resistance in Ohms
Rv=500.0; // Initial variable resistance in Ohms
DR=5.; // Incremental step of variable resistance in ohms
color_alpha= 0; // Initial transparency in zero

start = false; // Closed switch
}
//
// In draw() animations are implemented
//
void draw() {
if (start) { // Close switch.
I = Vpila/Req; // Intensity in the circuit, Ohm’s LawLlei
color_alpha = 255*I/Imax; // Brightness is proportional to intensity using alpha transparency
}

//
// For changing the value of variable resistance we use UP for increasing resistance
//
void keyPressed() { //
if (key == CODED & keyCode == UP ) {
Rv = Rv+DR;
}
}

// Mouse click closes swith and simulation begins
void mouseClicked() {
start = true;
}
// End

But the problem is that i’m not able to integrate the design with the bulb glowing function.

I’m really an absolute begginer and by now this seems very difficult for me.

Thanks to all

you had a functioning sketch & drawing and started with a second code?
instead growing it step by step by filling electric math…

float Vpila = 10.0;           // Battery voltage in volts
float Rl = 10.0;              // Bulb resistance in Ohms
float Rv = 10.0;              // Initial variable resistance in Ohms
float DR = 1.0;               // Incremental step of variable resistance in ohms
float Rsum = Rl + Rv;
float I=0;                    // current
float Imax=1.0;               // ?? rangeing
int mcolor = 0;                // Initial lamp col
boolean isOpen;

void setup() {
size(600, 500);
isOpen = true;
}
//
void show_numbers() {
fill(255);
int                                          posX =  20;
text("V : "+Vpila+" V", posX, 20);
posX += 80;
text("Rl : "+nf(Rl, 1, 1)+" Ω", posX, 20);
posX += 80;
text("Rv : "+nf(Rv, 1, 1)+" Ω", posX, 20);
posX += 80;
text("Rs : "+nf(Rsum, 1, 1)+" Ω", posX, 20);
posX += 80;
text("I : "+nf(I, 1, 2)+" A", posX, 20);
posX += 80;
text("color: "+mcolor, posX, 20);
posX += 80;
}
//
void draw_circuit() {
fill(255);
ellipse (300, 150, 40, 40);
fill(0);text("V", 300-5, 150+5); fill(255);
line (280, 150, 120, 150);
line (120, 150, 120, 220);
ellipse (120, 220, 5, 5);
stroke (0);
if (!isOpen)     line (120, 220, 120, 290); //closed circuit
else             line (80, 240, 120, 290); // open circuit
line (120, 290, 120, 380);
line (320, 150, 500, 150);
line (500, 150, 500, 250);
line (500, 250, 500, 380);
line (500, 380, 350, 380);
rect (250, 350, 100, 50);
fill(0);text("Rv", 250+55, 350+30); fill(255);
line (250, 380, 120, 380);
ellipse (500, 250, 40, 40);
if (isOpen)    fill(0);
else           fill(mcolor, mcolor, 0);
ellipse (500, 250, 40, 40);
line (270, 320, 325, 425);
}
//
void draw() {
background(100, 100, 100);
show_numbers();
if (!isOpen) {                            // Close switch.
Rsum = Rl + Rv;
I = Vpila/Rsum;                         // Current acc. Ohm’s Law
} else {
I = 0.0;
}
mcolor = int(map(I, 0, Imax, 0, 255));   // Brightness 0 .. 255
draw_circuit();
}
//
// For changing the value of variable resistance we use UP for increasing resistance
//
void keyPressed() {
if (key == CODED & keyCode == UP )    Rv += DR;
if (key == CODED & keyCode == DOWN ) {
Rv -= DR;
if (Rv < 0.0 ) Rv = 0;
}
}

// Mouse click closes swith and simulation begins
void mouseClicked() {
isOpen = !isOpen;
}
// End

Thanks so much for the effort. Now I can see better how interact drawing with Ohm’s law