How to program rc controlled relays?

Hello,

I've been having trouble with programming this one (I'm not very good at programming) and was wondering if anyone could help me. I'm working on a RC controlled bot, which uses relays to power motors. However, my problem is that the rc buttons "lock" the relays. This basically means that my bot can't stop. If I press a button, the bot will either get stuck driving forward, backwards, left or right until another button is pressed. The bot then will do the last function it was told to do, and so on. [u]Basically, what I want it to do is to completely stop doing everything (all relays off) when no buttons are being pressed[/u]. Can anyone help me with programming this? It's a dual drive bot, and I have the code included below. Thanks!

//right forward-rf, left forward-lf //right back-rb, left back-lb int rf = 11; int rb = 10; int lb = 9; int lf = 8;

void setup() {

pinMode(rf, OUTPUT); pinMode(rb, OUTPUT); pinMode(lb, OUTPUT); pinMode(lf, OUTPUT);

pinMode(A0, INPUT); pinMode(A1, INPUT); pinMode(A2, INPUT); pinMode(A3, INPUT);

}

void loop() { //forward if(digitalRead(A0) == HIGH){ digitalWrite(rf,HIGH); digitalWrite(rb,LOW); digitalWrite(lf,HIGH); digitalWrite(lb,LOW);}

//turn right if(digitalRead(A1) == HIGH){ digitalWrite(rb,HIGH); digitalWrite(rf,LOW); digitalWrite(lf,HIGH); digitalWrite(lb,LOW);}

//back if(digitalRead(A2) == HIGH){ digitalWrite(lf,LOW); digitalWrite(rb,HIGH); digitalWrite(rf,LOW); digitalWrite(lb,HIGH);}

//turn left if(digitalRead(A3) == HIGH){ digitalWrite(lb,HIGH); digitalWrite(rb,LOW); digitalWrite(rf,HIGH); digitalWrite(lf,LOW);}

}

Basically, what I want it to do is to completely stop doing everything (all relays off) when no buttons are being pressed.

So, what is the problem? Read all 4 switches first. Then, do something if one is pressed, else if another is pressed do another thing, else do nothing (or stop doing anything).

What have you got connected to the analogue pins ?

Yes, all the pins are connected. [u]What I'm trying to do is to get the bot to go forward when I hold a button and to stop when I release that button.[/u] The bot should do this process to turn left and right, and go forwards and backwards. The problem is [u]I need an else function or something in there, but I don't really know where to put it, or what exactly to put in there[/u]

all the pins are connected

Yes, but what are they connected to ? What is providing the input to them ? Is it an analogue or digital signal ?

Try this

//forward
if (digitalRead(A0) == HIGH) 
{
  digitalWrite(rf, HIGH);
  digitalWrite(rb, LOW);
  digitalWrite(lf, HIGH);
  digitalWrite(lb, LOW);
}
else
{
   //code here to stop the bot 
}

There is a better way to do this using a series of if/else commands to read all of the input pins but try the simple way first.

The problem is I need an else function or something in there, but I don't really know where to put it, or what exactly to put in there

I gave you a clue. You need an if statement to deal with the first switch. You then need an else if statement to deal with the second switch, another to deal with the third switch, and another to deal with the 4th switch. You need an else statement to deal with the no switch pressed case.

if(forwardSwitchValue == LOW) // Or HIGH if that means pressed
{
   goForward();
}
else if(leftSwitchValue == LOW)
{
   goLeft();
}
else if(rightSwitchValue == LOW)
{
   goRight();
}
else if(backwardSwitchValue == LOW)
{
   goBackward();
}
else // no switch is pressed
{
   whoaNellie();
}