Pages: [1]   Go Down
Author Topic: If statement help.  (Read 291 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have an RF sensor connected to an ARDUINO UNO as well as a small electric motor. I am wanting the motor to turn on and off when the sensor returns a specific distance value. The sensor is working and outputs accurate distances, however my if statement is not working.
My code is commented below. Any suggestions will help.
Code:
/*
  /*
I am trying to code a "HC SR04" sensor to control  a motor (pin 1)*/

/*The problem is somewhere in the if/ else statment turning power on and off to the motor pin*/



int echoPin = 12;  //renaming pin
int trigPin = 13;  //renaming pin
int motor= 1;      // renaming pin

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT); //sensor out
  pinMode(echoPin, INPUT);  // sensor in
}

void loop() {
 
  digitalWrite(motor,HIGH); //starting motor
  unsigned long duration, cm; // time and distance unit
  digitalWrite(trigPin, LOW);  // sensor "sending" off
  delay(10);                   // delay 
  digitalWrite(trigPin, HIGH); // sensor now sending
  delay(20);                   //delay
  digitalWrite(trigPin, LOW);  //sensor no longer sending
  duration = pulseIn(echoPin, HIGH); //sensor "lsitening or receiving
  cm = (duration / 29) / 2;/* Velocity of sound is =29 cm/microseconds (divided by 2 for return time ase well)*/
   {
    if (cm <40)  //if sensor value if less than 40 cm
    {digitalWrite(motor, LOW); // Turn off motor **** This does NOT WORK****
    }
    else {
      digitalWrite(motor, HIGH);}  //else leave the motor on
      Serial.print(cm);           //print value to (for verification purposes)
      Serial.println(" cm");      // print cm for clarification of units
      delay(10);
    }
  }
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pinMode for "motor"

A simple print would show whether or not the 'if' is or isn't working (it probably is).

NB an ultrasonic rangefinder is not RF

Edit: DO NOT cross post the same question, it just wastes time.
I have deleted the other post.
« Last Edit: February 29, 2012, 12:43:42 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46299
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
NB an ultrasonic rangefinder is not RF
It isn't?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the help I now have the sensor operating two different motors. However, when I power the Arduino UNO with just a 9V battery and not from a USB cord my vehicle only goes in reverse. I know from my coding below that my sensor is  not working (therefore outputting a distance value of less than 10 cm). Should the Arduino UNO not power my sensor even though it is getting power from a 9V source?
Code:
int echoPin = 12;  //renaming pin
int trigPin = 13;  //renaming pin
int RFmotor= 11;      // renaming pin
int RRmotor= 8;    //renaming pin
int LFmotor= 10;    //renaming pin
int LRmotor= 9;    //renaming pin
void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT); //sensor out
  pinMode(echoPin, INPUT);  // sensor in
  pinMode(RFmotor, OUTPUT); // Right forward motor
  pinMode(RRmotor, OUTPUT); // Right Rev motor
  pinMode(LFmotor, OUTPUT); // Left forward motor
  pinMode(LRmotor, OUTPUT); // Left Rev motor
}

void loop() {
  unsigned long duration, cm; // time and distance unit
  digitalWrite(trigPin, LOW);  // sensor "sending" off
  delay(10);                   // delay 
  digitalWrite(trigPin, HIGH); // sensor now sending
  delay(20);                   //delay
  digitalWrite(trigPin, LOW);  //sensor no longer sending
  duration = pulseIn(echoPin, HIGH); //sensor "listening or receiving
  cm = (duration / 29) / 2;/* Velocity of sound is =29 cm/microseconds (divided by 2 for return time ass well)*/
   {
    if (cm <10)  //if sensor value if less than 10 cm
    {digitalWrite(RFmotor, LOW); // Turn off RF motor
      digitalWrite(RRmotor, HIGH); // Turn on RR motor
      digitalWrite(LFmotor, LOW); // Turn off LF motor
      digitalWrite(LRmotor, HIGH); // Turn on LR motor
      delay(1000);
      digitalWrite(RFmotor, LOW); // Turn off RF motor
      digitalWrite(RRmotor, HIGH); // Turn on RR motor
     delay(1000);
    }
    else {
     digitalWrite(RRmotor, LOW);      //else turn off the RRmotor
    digitalWrite(RFmotor, HIGH );    //else turn the RFmotor on
  digitalWrite(LRmotor, LOW);       //else turn off the LRmotor
    digitalWrite(LFmotor, HIGH );}  //else turn the LFmotor on
      Serial.print(cm);           //print value to (for distance verification purposes)
      Serial.println(" cm");      // print cm for clarification of units
     
      delay(10);
    }
  }




Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24480
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Insert Quote
Quote
NB an ultrasonic rangefinder is not RF
It isn't?

Yes, really it isn't Radio Frequency
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: