Coding 3 push buttons

Hey there guys, I would like to ask your help here. I've come up with a code and I put it in the void setup and placed it after Serial.begin(9600). The code goes like this:

  if
  {
    while(digitalRead(key_s4) == HIGH)
    {
      delay(2500);
      setpos();
    }
  }
  else if
  {
    while(digitalRead(key_s5) == HIGH)
    {
      delay(2500);
      setposright();
    }
  }
  else if
  {
    while(digitalRead(key_s6) == HIGH)
    {
      delay(2500);
      setposleft();
    }

The logic by the way is that the robot should remain still for a 3 seconds and should face the desired direction, depending on the pressed button. I tried compiling it and I got this error:

sketch_dec12b.cpp: In function 'void setup()':
sketch_dec12b:123: error: expected (' before '{' token sketch_dec12b:201: error: expected }' at end of input

I also tried it without the if()... else but although it didn't encounter any errors, it still didn't work when I plugged it in a power supply :~

Any replies would be deeply appreciated. Thanks in advance!

-Cenna

if There has to be a condition here in parentheses. No condition? Compiler complains.
{
while(digitalRead(key_s4) == HIGH)

Try this...

  if(digitalRead(key_s4) == HIGH)
  {
    while(digitalRead(key_s4) == HIGH)
    {
      delay(2500);
      setpos();
    }
  }
  else if(digitalRead(key_s5) == HIGH)
  {
    while(digitalRead(key_s5) == HIGH)
    {
      delay(2500);
      setposright();
    }
  }
  else if(digitalRead(key_s6) == HIGH)
  {
    while(digitalRead(key_s6) == HIGH)
    {
      delay(2500);
      setposleft();
    }

I've tried the code you've suggested but it didn't work properly. :disappointed_relieved:
Are there any alternatives you have in mind? =(

loracenna:
I've tried the code you've suggested but it didn't work properly. Are there any alternatives you have in mind?

Sadly, I did not take the mind reading course in college so I will be unable to help.

If you want help, you will have to find a mind reader who is also able to program or provide more of a description than "it didn't work".

Woops, sorry 'bout that. :. What I mean to say is that the robot stayed immobile even after pushing the switch and the push button (but I'm thinking of changing the wirings in case that's the problem and not the program itself).

Anyways, I was maybe hoping if you can help me figure out a different code with the same logic as what I've posted a while ago... Thanks! :slight_smile:

I was maybe hoping if you can help me figure out a different code with the same logic as what I've posted a while ago.

If that code did not do what you expected you have a problem elsewhere.
Code doesn't work by just rewriting functioning code.

So what you need to do is to post the whole code using the code tags, if you don't know what that means read the sticky post at the top.

In the mean time withe a simple sketch that just checks out the operation of your hardware.