- Joined
- May 20, 2007
- Location
- New York
Hopefully someone that knows Arduino really well will stumble upon this.
I'm having trouble running the the if/else statements that I've labeled "Up/Down Movement" and "Left/Right Movement".
1. If I leave the code as is, the "Left/Right" section is the only one that seems to be in effect.
2. If I comment out the "Up/Down" section, only the "Left/Right" works (obviously). Vice-versa when I comment out "Left/Right".
3. All 4 motors run off the 5V fine surprisingly, I tried it again with a separate 5V power supply for the motors but the problem persists.
I don't really have an extensive knowledge in C/C++, just a basic one really.. so I can't really see what my mistake is at all. Should I be using a switch...case statements instead (which I'm not really sure how to write out either but I'm trying it now as I write this)?
If it helps in any way, I'm using these components so you can get an idea of what my analog input is:
http://www.adafruit.com/products/245
And the Motors that I'm using:
http://www.adafruit.com/products/154
Thanks for any help offered on this, really appreciate your time on this!
Code:
#include <Servo.h> // include servo library files
Servo servo1; // initiate servo 1
Servo servo2; // initiate servo 2
Servo servo3; // initiate servo 3
Servo servo4; // initiate servo 4
int analogPin0 = 0; // initiate analog pin 0
int analogPin1 = 1; // initiate analog pin 1
int value0 = 0;
int value1 = 0;
void setup()
{
Serial.begin(9600);
servo1.attach(8);
servo2.attach(9);
servo3.attach(10);
servo4.attach(11);
}
void loop()
{
value0 = analogRead(analogPin0);
value1 = analogRead(analogPin1);
Serial.print(value0);
Serial.print(" LEFT/RIGHT, ");
Serial.print(value1);
Serial.print(" UP/DOWN");
Serial.println();
delay(10);
// UP AND DOWN MOVEMENT
if(value1 > 500)
{
servo1.write(180);
servo2.write(0);
servo3.write(180);
servo4.write(0);
}
else if(value1 < 200)
{
servo1.write(0);
servo2.write(180);
servo3.write(0);
servo4.write(180);
}
else if(value1 < 400 || value1 > 300)
{
servo1.write(90);
servo2.write(90);
servo3.write(90);
servo4.write(90);
}
// LEFT AND RIGHT MOVEMENT
else if(value0 > 500)
{
servo1.write(180);
servo2.write(180);
servo3.write(180);
servo4.write(180);
}
else if(value0 < 200)
{
servo1.write(0);
servo2.write(0);
servo3.write(0);
servo4.write(0);
}
else if(value0 < 400 || value0 > 300)
{
servo1.write(90);
servo2.write(90);
servo3.write(90);
servo4.write(90);
}
}
I'm having trouble running the the if/else statements that I've labeled "Up/Down Movement" and "Left/Right Movement".
1. If I leave the code as is, the "Left/Right" section is the only one that seems to be in effect.
2. If I comment out the "Up/Down" section, only the "Left/Right" works (obviously). Vice-versa when I comment out "Left/Right".
3. All 4 motors run off the 5V fine surprisingly, I tried it again with a separate 5V power supply for the motors but the problem persists.
I don't really have an extensive knowledge in C/C++, just a basic one really.. so I can't really see what my mistake is at all. Should I be using a switch...case statements instead (which I'm not really sure how to write out either but I'm trying it now as I write this)?
If it helps in any way, I'm using these components so you can get an idea of what my analog input is:
http://www.adafruit.com/products/245
And the Motors that I'm using:
http://www.adafruit.com/products/154
Thanks for any help offered on this, really appreciate your time on this!