Using Relays.. when reset the relays are on..

hi guys.. i need some help.. i've been doing a project using relays..
theres a problem.. when i start my arduino i want my relays in off state.. that is my pin 2 and 3 i dont know what is the problem.. please help me out thx.. and heres the code..

this is a big problem cuz i want to use in my motorcycle.. for horn and start button
i dont want to. scary off my neighboors.. everytime my arduino resets..

/*
simple LED test
*/

char val;         // variable to receive data from the serial port
int ledpin = 2;  // LED connected to pin 2 (on-board LED)

void setup()
{
  pinMode(ledpin = 2, OUTPUT); // pin 2 (on-board LED) as OUTPUT
  pinMode(ledpin = 3, OUTPUT); // pin 3 (on-board LED) as OUTPUT

    Serial.begin(9600);       // start serial communication at 115200bps

}

void loop()

{
  if( Serial.available() )       // if data is available to read
  {
    ;
  }
  val = Serial.read();         // read it and store it in 'val'

  if( val == 'a' )               // if 'a' was received led 2 is switched off
  {
    digitalWrite(ledpin = 2, HIGH);    // turn Off pin 2
  }
 
  if( val == 'A' )               // if 'A' was received led 2 on
  {
    digitalWrite(ledpin = 2, LOW);  // turn ON pin 2
  }

  if( val == 'b' )               // if 'b' was received led 3 is switched off
  {
    digitalWrite(ledpin = 3, HIGH);    // turn Off pin 3
  }
 
  if( val == 'B' )               // if 'B' was received led 3 on
  {
    digitalWrite(ledpin = 3, LOW);  // turn ON pin 3
  } //else (ledpin = 3, LOW)        //set led pin 3 to low state

}

Please learn the proper syntax. This is not proper syntax, even though yes, it does compile.

pinMode(ledpin = 2, OUTPUT); // pin 2 (on-board LED) as OUTPUT
pinMode(ledpin = 3, OUTPUT); // pin 3 (on-board LED) as OUTPUT

Btw, your comments don't reflect your code either and I am not going to fix them for you.

Made a slight change.

/*
simple LED test
*/

char val;         // variable to receive data from the serial port
const byte Relay1 = 2;  // LED connected to pin 2 (on-board LED)
const byte Relay2 = 3;

void setup()
{
  pinMode(Relay1, OUTPUT); // pin 2 () as OUTPUT
  pinMode(Relay2, OUTPUT); // pin 3 (on-board LED) as OUTPUT

  Serial.begin(9600);       // start serial communication at 115200bps
}

void loop()
{
  if ( Serial.available() > 0 )      // if data is available to read
  {
    val = Serial.read();         // read it and store it in 'val'

    if ( val == 'a' )              // if 'a' was received led 2 is switched off
    {
      digitalWrite(Relay1, HIGH);    // turn Off pin 2
    }

    if ( val == 'A' )              // if 'A' was received led 2 on
    {
      digitalWrite(Relay1, LOW);  // turn ON pin 2
    }

    if ( val == 'b' )              // if 'b' was received led 3 is switched off
    {
      digitalWrite(Relay2, HIGH);    // turn Off pin 3
    }

    if ( val == 'B' )              // if 'B' was received led 3 on
    {
      digitalWrite(Relay2, LOW);  // turn ON pin 3
    } //else (ledpin = 3, LOW)        //set led pin 3 to low state
  }
}

thx for the help..

where can i learn all this from? i'm willing to learn

my problem is still not fixed... my problem:
when i reset my arduino.. the relays.. become activated.. i want them to be on "off" state.. for example if i change my batteries.. my motorcycle is going to sound the horn and start the engine..until i send some serials..

Relays have 3 contacts, NC, NO and COM.
How do you have it wired now, NC and COM? Does the horn sound when the horn wires are connected or disconnected?

Hi,
Cross posting is a pain in the rear!! and a waste of time, are we all in such a rush that we can't wait a short time for a reply!!

Are these individual relays or the chinese relay modules?? I have used these modules and most require a LOW signal to activate the relays?? thus they activate as soon as powered up, a bit like inverted logic and I don't like it, but they work!!

If individual relays you will need a transistor or FET circuit to drive it.

Also I don't think these lines are correct:

pinMode(ledpin = 2, OUTPUT); // pin 2 (on-board LED) as OUTPUT
pinMode(ledpin = 3, OUTPUT); // pin 3 (on-board LED) as OUTPUT

Should be;

pinMode(ledpin2, OUTPUT); // pin 2 (on-board LED) as OUTPUT
pinMode(ledpin3, OUTPUT); // pin 3 (on-board LED) as OUTPUT

set up ledpin3 like this;

int ledpin3 = 3; // LED connected to pin 3 (on-board LED)?? On board LED there's only one?

I don't understand where the serial comes in to it?? unless it's transmitted from another arduino!

Hope it helps.

Regards

Mel.

I don't understand where the serial comes in to it?? unless it's transmitted from another arduino!

works with bluetooth too!

everything is perfectly fine.. its just.. once i turn on or reset my arduino the relays turn on.. but.. if i turn on and off from serial it becomes normal.. its really weird.. its like..

Power to arduino = Relay turns on without signal... then when i bluetooth serial on and off it becomes good again..
i have to toggle to make the relay...

cuz i use push to on button from my android phone..

reeshan:
when i reset my arduino.. the relays.. become activated..

This is not a very complex problem. How much thought have you given it?

What does it take to make the relays close their contacts?

How is the Arduino causing that to happen?

What would you change on the Arduino so it doesn't happen?

...R

Right now, they are only set to OUTPUT, with no official state. it can be on or off when you reset the arduino, so make sure the relays are set in the setup() function.

void setup()
{
  pinMode(Relay1, OUTPUT); // pin 2 () as OUTPUT
  pinMode(Relay2, OUTPUT); // pin 3 (on-board LED) as OUTPUT
  digitalWrite(Relay1, LOW);
  digitalWrite(Relay2, LOW);
  Serial.begin(9600);       // start serial communication at 115200bps
}

sorry but i think my question was not right... when i power my arduino my relays go into NC but the button app that i made will turn on NO when hold and NC when let go... i just wanted to start my arduino without getting my relays in NO mode.. my relay boards have LEDs i'm using them as indicators.

HazardsMind:
Right now, they are only set to OUTPUT, with no official state. it can be on or off when you reset the arduino, so make sure the relays are set in the setup() function.

void setup()

{
  pinMode(Relay1, OUTPUT); // pin 2 () as OUTPUT
  pinMode(Relay2, OUTPUT); // pin 3 (on-board LED) as OUTPUT
  digitalWrite(Relay1, LOW);
  digitalWrite(Relay2, LOW);
  Serial.begin(9600);      // start serial communication at 115200bps
}

THX alot man ur a life saver.... all i need to know was that..

here's the complete code...

/*
simple LED test
*/

char val;         // variable to receive data from the serial port
const byte Relay1 = 2;  // LED connected to pin 2 (on-board LED)
const byte Relay2 = 3;

void setup()
{
  pinMode(Relay1, OUTPUT); // pin 2 () as OUTPUT
  pinMode(Relay2, OUTPUT); // pin 3 (on-board LED) as OUTPUT
  digitalWrite(Relay1, HIGH);
  digitalWrite(Relay2, HIGH);
  Serial.begin(9600);       // start serial communication at 115200bps
}

void loop()
{
  if ( Serial.available() > 0 )      // if data is available to read
  {
    val = Serial.read();         // read it and store it in 'val'

    if ( val == 'a' )              // if 'a' was received led 2 is switched off
    {
      digitalWrite(Relay1, HIGH);    // turn Off pin 2
    }

    if ( val == 'A' )              // if 'A' was received led 2 on
    {
      digitalWrite(Relay1, LOW);  // turn ON pin 2
    }

    if ( val == 'b' )              // if 'b' was received led 3 is switched off
    {
      digitalWrite(Relay2, HIGH);    // turn Off pin 3
    }

    if ( val == 'B' )              // if 'B' was received led 3 on
    {
      digitalWrite(Relay2, LOW);  // turn ON pin 3
    } //else (ledpin = 3, LOW)        //set led pin 3 to low state
  }
}