Step 4: Use your Relays!
Include the LED so you know when the relay should be on
Make sure the relay extension cord IS NOT PLUGGED IN!!!
Write up a program that turns the relay on for 2 seconds when the character "R" is received over Serial and when a button on pin 12 is pulled HIGH, the relay turns on.
Make sure you have a button on pin 12 or comment that bit out =]
for some reason it returns numbers when you press a key that ins't R. I think its the DEC I put after it =/
It should look like this:
//Relay controller for water pump
//LMC
int relay = 10;
int button = 12;
int thirteen = 13;
int val;
void setup (){
pinMode(thirteen, OUTPUT); //start of setup basic cmds
digitalWrite(thirteen, HIGH); //debug light
delay(500); //WAITZ!!!
pinMode(relay, OUTPUT);
Serial.begin(19200);
Serial.println('Bonjour. Press R to turn relay on for 2 seconds');
digitalWrite(thirteen, LOW); //End of setup cmds
delay(500); // end of setup cmds
}
void loop(){
if (Serial.available()) {
val = Serial.read();
if (val=='R'){
digitalWrite(thirteen, HIGH);
digitalWrite(relay, HIGH);
delay(2000);
digitalWrite(relay, LOW);
digitalWrite(thirteen, LOW);
} else {
Serial.print("You pressed:");
Serial.print(val, DEC);
Serial.println(". Press R for Relay, Any other key will do nothing");
}
}
if (digitalRead(button) == HIGH){
digitalWrite(thirteen, HIGH);
digitalWrite(relay, HIGH);
delay(2000);
digitalWrite(relay, LOW);
digitalWrite(thirteen, LOW);
}
delay(500);
}
Upload it to your board
Open your serial monitor
Send some R's
You should hear the relay clicking over and the LED should light up.
If not, youve put the transistor in wrong and you now need to redo it all =]
You can now see why I didn't put the transistors and all that in the box =]
Sorry the picture is a duplicate of the first step.
Remove these ads by
Signing Up































Not Nice













Visit Our Store »
Go Pro Today »



