Sunday, June 20, 2010

Arduino code for Relay Control

// This code just lets you turn a digital out pin on and off. That's
// all that is needed to verify a relay curcuit is working.
// Press the space bar to toggle the relay on and off.

#define RELAY_PIN 3

void setup()
{
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(9600); // open serial
Serial.println("Press the spacebar to toggle relay on/off");
}

void loop()
{
static int relayVal = 0;
int cmd;

while (Serial.available() > 0)
{
cmd = Serial.read();

switch (cmd)
{
case ' ':
{
relayVal ^= 1; // xor current value with 1 (causes value to toggle)
if (relayVal)
Serial.println("Relay on");
else
Serial.println("Relay off");
break;
}
default:
{
Serial.println("Press the spacebar to toggle relay on/off");
}
}

if (relayVal)
digitalWrite(RELAY_PIN, HIGH);
else
digitalWrite(RELAY_PIN, LOW);
}
}
Related Posts Plugin for WordPress, Blogger...
Disclaimer: All the information in this blog is just gathered from different sites in the web and placed here and I am not the owner for these content

Popular Projects

Followers

My Blog List

Give Support

Give Support
Encourage me Through Comments & by Following