// code by Chris Orban float I = 0; // initial value float deltaI = 0; float Vind; float L = 20; float Vbatt = 10; // Volts float R = 3; //Ohms float dt = 0.1; boolean battery_is_connected = false; DPGraph graph1 = new DPGraph(); PImage imgConnected; PImage imgDisconnected; void setup() { size(750,500); I = 0; // initial value graph1.colorFunction = color(255,177,100); //orange graph1.xTitle = "time"; imgDisconnected = loadImage("http://www.physics.ohio-state.edu/~orban/physics1251lab_2016/RLdisconnected_v2.png"); imgConnected = loadImage("http://www.physics.ohio-state.edu/~orban/physics1251lab_2016/RLconnected_v2.png"); } // For people with C and C++ experience, draw() is // very similar to main(), except that draw() // is run over and over again void draw() { if (keyPressed) { if (key == 'c') battery_is_connected = true; if (key == 'd') battery_is_connected = false; } if ( battery_is_connected == true) { Vind = Vbatt - I*R; deltaI = Vind/L*dt; I += deltaI; } else { // battery is not connected Vind = I*R; deltaI = Vind/L*dt; I -= deltaI; } // This will clear the screen and re-draw it display(); graph1.addPoint(I); graph1.display(); //println("I = ",I); // Uncomment if curious about precise value of I } // end draw()