# Ohm's law exercice

**URL:** <https://discourse.processing.org/t/ohms-law-exercice/5660>\
**Category:** Coding Questions\
**Created:** [November 18, 2018, 9:28am UTC](https://discourse.processing.org/t/ohms-law-exercice/5660 "2018-11-18T09:28:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![saotome](https://avatars.discourse-cdn.com/v4/letter/s/90ced4/32.png) [@saotome](https://discourse.processing.org/u/saotome)\
**Post date:** [November 18, 2018, 9:28am UTC](https://discourse.processing.org/t/ohms-law-exercice/5660/1 "2018-11-18T09:28:01Z")

</div>

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

```auto
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

```auto
    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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 18, 2018, 11:49am UTC](https://discourse.processing.org/t/ohms-law-exercice/5660/2 "2018-11-18T11:49:01Z")

</div>

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

```auto
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

```

---

<div class="post-metadata">

**Author:** ![saotome](https://avatars.discourse-cdn.com/v4/letter/s/90ced4/32.png) [@saotome](https://discourse.processing.org/u/saotome)\
**Post date:** [November 18, 2018, 4:47pm UTC](https://discourse.processing.org/t/ohms-law-exercice/5660/3 "2018-11-18T16:47:10Z")

</div>

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