Friday, July 22, 2011

Duty cycle and pulse-width modulation on the Arduino UNO with Linux



int PIN = 13;
float dutyCycle = .75; // between 0-1

void setup() {
pinMode(PIN, OUTPUT);
}

void loop() {
int state = 0;

while(1) {
if(state == 0) {
digitalWrite(PIN, LOW);
delay(1000 * (1-dutyCycle));
state = 1; }
else {
digitalWrite(PIN, HIGH);
delay(1000 * dutyCycle);
state = 0;
}
}
}

1 comment:

  1. Chad, thank you very much for the educational video. I had a few questions:

    1.) Is "dutyCycle" a global available in your development environment?

    2.) Do you know if it would be possible to (cheaply) build a circuit to measure the water level of a container and report it to a webserver using the Arduino UNO? I would very much like to build a custom web application to allow for measuring amounts of beer left in kegs, which can then be reported online. A tutorial on this custom application would ensure my purchase of an Arduino UNO.

    Again, thank you very much for creating this educational and entertaining video.

    ReplyDelete