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);
}
}
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
#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}
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.
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
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.