I'm trying to move 2 servo's when they get input from a sonic sensor.
For that I like to use the IF statement but I don't know if that statement can handle more then 1 variable.
The standard statement is like this:
if (someVariable > 50)
{
// do something here
}
But can I add more variables?
Like this:
if (someVariable > 50) and (someVariable != 0)
{
// do something here
}
Here's the void loop() from my script so far (it's just a combination of the "sweep" and "NewPingExample" scripts):
void loop()
{
delay(100); // Wait 100ms between pings. 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
if ((uS / US_ROUNDTRIP_CM) != 0) and ((uS / US_ROUNDTRIP_CM) > 50)
{for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
if ((uS / US_ROUNDTRIP_CM) != 0) and ((uS / US_ROUNDTRIP_CM) < 50)
{for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
Yes you can. You need brackets surrounding the conditions as you have in loop but they are missing from the example above it.
The C and operator is &&, not and although as you can see, it does compile.
As an example it's fine but there is no point to the check you're doing in this case of course - if the value is greater than fifty, you already know it isn't zero.
thanks for your comments.
First...hehehe...newbie question: with brackets you mean { or ( ?
And I do need both because when the sensor is out of range (being further away then 3 to 4 meters) it doesn't "see"anything and writes down a "0". So that's why I need both conditions to be checked. Under no condition the servo's may react when the count is zero.
So here's what my code looks like after your help...is this what you are trying to tell me?
void loop()
{
delay(100); // Wait 100ms between pings. 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
}
{
if ((uS / US_ROUNDTRIP_CM) != 0) && ((uS / US_ROUNDTRIP_CM) > 50)
{for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
{
if ((uS / US_ROUNDTRIP_CM) != 0) && ((uS / US_ROUNDTRIP_CM) < 50)
{for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
}
That closing brace ends your loop function. The compiler is complaining that the code that follows isn't in any function. Move the brace to the end. You have a number of additional braces in there that are unnecessary too.
The reason why I need that "if ((uS / US_ROUNDTRIP_CM) != 0) &&" is that the returned value from the sensor is 0 (zero) when the UAV (quadcopter) is flying higher then the sensor can sense. The sensor is "seeing" from 0 upto 3 to 4 meters. The quad max range is 300 meters. So when I only us "if (uS / US_ROUNDTRIP_CM > 50)" the landing gear folds down because it gets a 0 (zero).
And zero is less then 50 ( < 50 ).
i made one for you ! :*
it uses a diferent libary ,, this one for the sensor ,,
you can change it to cm values but i prefer the normal ones ,, check it out ,,
this is the lib website https://code.google.com/p/arduino-new-ping/
here is the code
//dexter gear control made 1-8-2014/
#include <Servo.h>
#include <NewPing.h>
#define TRIGGER_PIN 3 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 2 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
// set height here //
const int height =1200; // set your height here ,, i use digital value's they give better reading then the cm values ,,
int cm ; //incoming value of sensor
Servo gear; // make servo output to gear
int signal =0; // signal procces
int sensor =0;
void setup() {
Serial.begin(9600); // starting serial port baud rate 9600
delay (2000);
gear.attach(8); // servo conection out pin 8 to gear //
}
void loop() {
Serial.println(cm); // wil print digital values
Serial.println(sensor); //wil print cm
cm = sonar.ping();
sensor = cm/ US_ROUNDTRIP_CM;
if(height<cm){ // if sensor is higher height set
gear.writeMicroseconds(1800); // write system value to servo //
delay (35);
}
else if(height>cm){ // if sensor is lower height set
gear.writeMicroseconds(1000); // write system value to servo //
delay (35);
}
}
//dexter gear control made 1-8-2014/
#include <Servo.h>
#include <NewPing.h>
#define TRIGGER_PIN 3 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 2 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
// set height here //
const int height =1200; // set your height here ,, i use digital value's they give better reading then the cm values ,,
int cm ; //incoming value of sensor
Servo gear; // make servo output to gear
int signal =0; // signal procces
int sensor =0;
void setup() {
Serial.begin(9600); // starting serial port baud rate 9600
delay (2000);
gear.attach(8); // servo conection out pin 8 to gear //
}
void loop() {
Serial.println(cm); // wil print digital values
Serial.println(sensor); //wil print cm
cm = sonar.ping();
sensor = cm/ US_ROUNDTRIP_CM;
if (sensor =0)return;
if(height<cm){ // if sensor is higher height set
gear.writeMicroseconds(1800); // write system value to servo //
delay (35);
}
else if(height>cm){ // if sensor is lower height set
gear.writeMicroseconds(1000); // write system value to servo //
delay (35);
}
}
}