This is an old revision of the document!
Ref: http://arduino-info.wikispaces.com/ArduinoPower
<pre> /* YourDuino Example: Relay Control 1.10
Handles "Relay is active-low" to assure no relay activation from reset until application is ready. terry@yourduino.com */
/*—–( Import needed libraries )—–*/ /*—–( Declare Constants )—–*/ #define RELAY_ON 0 #define RELAY_OFF 1 /*—–( Declare objects )—–*/ /*—–( Declare Variables )—–*/ #define Relay_1 2 Arduino Digital I/O pin number #define Relay_2 3 #define Relay_3 4 #define Relay_4 5 void setup() / SETUP: RUNS ONCE / { ——-( Initialize Pins so relays are inactive at reset)—-
digitalWrite(Relay_1, RELAY_OFF); digitalWrite(Relay_2, RELAY_OFF); digitalWrite(Relay_3, RELAY_OFF); digitalWrite(Relay_4, RELAY_OFF);
—( THEN set pins as outputs )—- pinMode(Relay_1, OUTPUT); pinMode(Relay_2, OUTPUT); pinMode(Relay_3, OUTPUT); pinMode(Relay_4, OUTPUT); delay(4000); Check that all relays are inactive at Reset
}–(end setup )— void loop() / LOOP: RUNS CONSTANTLY / { —( Turn all 4 relays ON in sequence)—
digitalWrite(Relay_1, RELAY_ON);// set the Relay ON delay(1000); // wait for a second digitalWrite(Relay_2, RELAY_ON);// set the Relay ON delay(1000); // wait for a second digitalWrite(Relay_3, RELAY_ON);// set the Relay ON delay(1000); // wait for a second digitalWrite(Relay_4, RELAY_ON);// set the Relay ON delay(4000); // wait see all relays ON
—( Turn all 4 relays OFF in sequence)— digitalWrite(Relay_1, RELAY_OFF); set the Relay OFF
delay(1000); // wait for a second digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF delay(1000); // wait for a second digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF delay(1000); // wait for a second digitalWrite(Relay_4, RELAY_OFF);// set the Relay OFF delay(4000); // wait see all relays OFF
}–(end main loop )— *( THE END )* </pre> Howto fade a motor or LED <pre> /* YourDuino PWM Power Example “analogWrite” outputs a Pulse Width Modulated signal terry@yourduino.com Fades an LED or Lamp up and down NOTE: analogWrite values are from 0..255 */ /*—–( Import needed libraries )—–*/ /*—–( Declare Constants )—–*/ #define ledPin 9 LED connected to digital pin 9 /*—–( Declare objects )—–*/ /*—–( Declare Variables )—–*/ int fadeValue; Value to be sent to PWM void setup() / SETUP: RUNS ONCE / { (Not Used) }–(end setup )— void loop() / LOOP: RUNS CONSTANTLY / { fade up min to max for(fadeValue = 0 ; fadeValue ⇐ 255; fadeValue +=5) { analogWrite(ledPin, fadeValue); delay(50); } End Fade Up delay(2000); Hold 2 seconds at max brightness fade out max to min for(fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) { analogWrite(ledPin, fadeValue); delay(50); } delay(2000); Hold 2 seconds at off }–(end main loop )— /*—–( Declare User-written Functions )—–*/ None *( THE END )* </pre>