void loop() {

static unsigned int angle = 0;

static unsigned long lastTime = 0;

static unsigned long time;

anglerate= analogRead(2);

/*Scale anglerate so that it's in degrees per millisecond, centered around 0 - depends on specific gyroscope*/

time = millis();

angle += anglerate * (lastTime - time); /* This is the important part */

lastTime = time;

}

If you look up integrals, the second to last line (angle +=...) is what is doing the integration. anglerate is the height of the rectangle, and (lastTime - time) is dx. In a real integral, they say that dx is infinitely close to 0, but in real life, you can only get so accurate so dx is just the time per loop.