servo help, with 2 button inputs

This just runs through the loop avoiding the if statements and doing whats in between every if statement

dont mind the wierd ints for the servos, the midpts and such are slightly diff.

Is this just because i have no buttons hooked up yet, because my bot should be going straight because the btnpins are at a low state.

Please help

#include <Servo.h>

Servo servo;
Servo servo2;

const int mid = 70;
const int mid2 = mid-3;

const int st = 110;
const int st2 = -st+3;

const int bk = -110;
const int bk2 = st-3;

const int btnpin = 2;
const int btnpin2 = 3;

int btnstate = 0;
int btnstate2 = 0;

void setup()
{
  servo.attach(5);//right servo viewing from back
  servo2.attach(6);//left servo viewing from back
  
  pinMode(btnpin, INPUT);//right btn viewing from back
  pinMode(btnpin2, INPUT);//left btn viewing from back
}

void loop()
{
btnstate = digitalRead(btnpin);
btnstate2 = digitalRead(btnpin2);  

  if(btnstate2 == HIGH && btnstate == HIGH)
  {
  //when both btns are pressed back up
  servo.write(bk);
  servo2.write(bk2);
  delay(250);
  }
  else
  if(btnstate2 == LOW && btnstate == LOW)
  {
  //when both buttons aren't pressed go straight
  servo.write(st);
  servo2.write(st2);
  delay(1000);
  }
  else
  if(btnstate2 == LOW && btnstate == HIGH)
  {
  //turn left when right btn is pressed
  servo.write(st);
  servo2.write(bk2);
  delay(750);
  }
  else
  if(btnstate2 == HIGH && btnstate == LOW)
  {
  //turn right when left btn is pressed
  servo.write(bk);
  servo2.write(st2);
  delay(750);
  }
}

You haven't enabled the pullup resistors, so the switch pins are floating. They are NOT at a LOW state.

Is there some reason for this?

Servo servo;
Servo servo2;

You got something against servo1? It makes following code a lot easier (for me, at least) if there is consistency in the variable names.

Indent the code in the if blocks, to. That makes it much easier to see where the block ends.

What are the servos doing? What kind of servos are they?

i do have a problem with servo1 lol in the arduino code editor it looks like an l or an I, lol. anyway. so if i add this to the void setup() code it should work

digitalWrite(btnpin, HIGH);
digitalWrite(btnpin2, HIGH);

ok i went and did this but now it continually goes backwards due to both pins being high.

how do i use the buttons to determine whether the pin is recieveing +5 volts or not so i want to be able to do

if digital pin 3 is recieving 5 volts do {this}

i do have a problem with servo1

Then, use names like servoA and servoB or servoOne and servoTwo.

so if i add this to the void setup() code it should work

If you must. I'd prefer:

digitalWrite(btnpinA, HIGH);
digitalWrite(btnpinB, HIGH);

(with corresponding changes, elsewhere, of course.)

now it continually goes backwards due to both pins being high.

and by this

i want to be able to do
if digital pin 3 is recieving 5 volts do {this}

i meant
i want to be able to do
if digital pin 3 is recieving 5 volts from external button do {this}

please just write the code real fast if you can

please just write the code real fast if you can

What would the point of that be? You've got the hardware.

#include <Servo.h>

Servo servo;
Servo servo2;

const int mid = 70;
const int mid2 = mid-3;

const int st = 110;
const int st2 = -st+3;

const int bk = -110;
const int bk2 = st-3;

const int btnpin = 2;
const int btnpin2 = 3;

int btnstate = 0;
int btnstate2 = 0;

void setup()
{
  servo.attach(5);//right servo viewing from back
  servo2.attach(6);//left servo viewing from back
  
  pinMode(btnpin, INPUT);//right btn viewing from back
  pinMode(btnpin2, INPUT);//left btn viewing from back
}

void loop()
{
btnstate = digitalRead(btnpin);
btnstate2 = digitalRead(btnpin2);  

  if(btnstate2 == HIGH && btnstate == HIGH)
  {
  //when both btns are pressed back up
  servo.write(bk);
  servo2.write(bk2);
  delay(250);
  }
  else
  if(btnstate2 == LOW && btnstate == LOW)
  {
  //when both buttons aren't pressed go straight
  servo.write(st);
  servo2.write(st2);
  delay(1000);
  }
  else
  if(btnstate2 == LOW && btnstate == HIGH)
  {
  //turn left when right btn is pressed
  servo.write(st);
  servo2.write(bk2);
  delay(750);
  }
  else
  if(btnstate2 == HIGH && btnstate == LOW)
  {
  //turn right when left btn is pressed
  servo.write(bk);
  servo2.write(st2);
  delay(750);
  }
}

my problem is in the loop avoids the ifs and runs through doing the things inbetween the ifs no matter what
i want it to be

if digital pin 3 is receiving 5 volts from external button do {the servo actions}

Time for some debug prints. Those delays aren't going to help the responsiveness.

what would the point of that be

im not turning the servo power on and of, im making it do different actions based on bumping into two different buttons. it is a mazebot if you wanted to know. when it hits a wall it turns 90 degrees the opposite direction then continues to go straight again.

So what should this look like

  if(btnstate2 == HIGH && btnstate == HIGH)
  {
  //when both btns are pressed back up
  servo.write(bk);
  servo2.write(bk2);
  delay(250);
  }

No I meant "what would the point of someone else writing the code be?"

my problem is in the loop avoids the ifs

Seems unlikely to me.

i just dont get why it isnt working.

See reply #8

what are dubug prints?

is that when you click verify and it compiles, there are no errors there?

No, it is when you scatter Serial.println statements around your code to cut down on the guesswork.

Hi dsv101,

Strip you code to a bare minimum (just leave the hat on).

const int btnpin = 2;
const int btnpin2 = 3;
int btnstate = 0;
int btnstate2 = 0;

void setup(){
  pinMode(btnpin, INPUT);//right btn viewing from back
  pinMode(btnpin2, INPUT);//left btn viewing from back
  Serial.begin(9600);
}

void loop()
{
btnstate = digitalRead(btnpin);
btnstate2 = digitalRead(btnpin2);  

if(btnstate2 == HIGH && btnstate == HIGH){
  //when both btns are pressed back up
  Serial.println("Please send more cookies");
  delay(250);
  }
}

Attach buttons and test it. Does it behave as intended? :-?

Now go and read some more - Hint: PoulS did say somthing usefull.

Now test again. Does is behave as intended?

Too many cookies?? ::) You might wanna take the LOWrider instead of surfing on the HIGHsea :-X

-Fletcher

i want to be able to do if digital pin 3 is recieving 5 volts from external button do {this}

How is an external button going to provide 5 volts? Switches, and you really should learn proper terminology, control voltage. They allow it to flow, or they do not. They do not manufacture voltage from thin air.

If you really are controlling an external voltage with the switches, make sure that the external ground is connected to the Arduino ground, too.

Resolved!

I had the grounds from the buttons hooked up to the same resistor by accident, and i found a typo. Anyway here is the newer thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1291248908