Go Down

Topic: If statement help. (Read 389 times) previous topic - next topic

Juanka

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: [Select]
/*
  /*
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);
    }
  }

AWOL

#1
Feb 29, 2012, 06:34 pm Last Edit: Feb 29, 2012, 06:43 pm by AWOL Reason: 1
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.
"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.

PaulS

Quote
NB an ultrasonic rangefinder is not RF

It isn't?

Juanka

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: [Select]
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);
    }
  }






AWOL

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


Yes, really it isn't Radio Frequency
"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.

Go Up