Need some serious help for my project...

Hello guys.. Am very much new to this world and need some help.

In my program i have these conditions for movement of servo..

  1. If no object detected by some sensor >>>> Servo move some angle
  2. if detected at Distance A-B >>>> Servo move some angle less than above
  3. if detected at Distance B-C >>>> Servo move some angle less than above
  4. if detected at Distance C-D >>>> again some angle less than above
    NOTE: >>>>> means then, not greater than :stuck_out_tongue:

i used If loop to execute these,but the problem is if the object is detected at C-D and then moves anywhere greater than C i want to execute the first condition ( No object detected ) after some time.

ie: i dont want to execute B-C condition if the object moves to B-C from C-D.. Is there any way for this?? please help. i hope you guys understand what i mean :confused:

i dont want to execute B-C condition if the object moves to B-C from C-D.

Assign each of the distance ranges a number and save the current state number in a variable when the distance goes into that range. Then, when the distance changes to a different range you can check the range that it came from by testing the state variable.

Suppose the C-D range state value was 2, then you can write code to do the following

start loop
  measure range
  if range is B-C
    if state is 2 (ie was previously in C-D)
      do something
    end if
  else (ie was not previously in C-D)
    do something else
  end else
end loop

UKHeliBob:
Assign each of the distance ranges a number and save the current state number in a variable when the distance goes into that range. Then, when the distance changes to a different range you can check the range that it came from by testing the state variable.

Suppose the C-D range state value was 2, then you can write code to do the following

start loop

measure range
  if range is B-C
    if state is 2 (ie was previously in C-D)
      do something
    end if
  else (ie was not previously in C-D)
    do something else
  end else
end loop

Thanks.. But i want to go the first loop if the object moves to the previous range. how to do that? Is there any way? like if state is 2 then go to loop 1

like if state is 2 then go to loop 1

That is what the "do something" or "do something else" code is for, but please do not consider the goto command. Instead, call a function to do what you want then set the state number as appropriate so that next time through loop() the right thing (or nothing at all) happens based on the current state.

I deliberately used a variable named state because what I am describing is known as a Finite State Machine, but that sounds scary. At any time the system is in one of several states and you write code to deal with each of them and change the state to make the program do something different when something happens.

UKHeliBob:
That is what the “do something” or “do something else” code is for, but please do not consider the goto command. Instead, call a function to do what you want then set the state number as appropriate so that next time through loop() the right thing (or nothing at all) happens based on the current state.

I deliberately used a variable named state because what I am describing is known as a Finite State Machine, but that sounds scary. At any time the system is in one of several states and you write code to deal with each of them and change the state to make the program do something different when something happens.

Can you help me with the below program?? its done by my friend. am completely Noob. 2 servos are used here… And only 2 distance are done because of the problem i told… can u plz edit it so that the first loop executes if it goes from reverse…

#include <Servo.h>
Servo servoMain; // Define our Servo
Servo servoMain1;
#define trigPin 13
#define echoPin 12
const int switchpin = 4;
int switch_state = 0;
int value;
void setup()

{
Serial.begin (9600);
servoMain.attach(8); // servo on digital pin 8
servoMain1.attach(9); // servo on digital pin 9
pinMode(switchpin,INPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop()
{
switch_state = digitalRead(switchpin);

if(switch_state == HIGH)
{
Serial.print(“Switch ON”);
Serial.println(" ");

value=0;

servoMain.write(90);
servoMain1.write(0);
delay(200);

while(1)
{
switch_state = digitalRead(switchpin);

if(switch_state == HIGH)
{

long duration, distance;

digitalWrite(trigPin, LOW); // Added this line

delayMicroseconds(2); // Added this line

digitalWrite(trigPin, HIGH);
//delayMicroseconds(1000); - Removed this line

delayMicroseconds(10); // Added this line

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

delay(500);

if ((distance>30)&&(distance<60))
{
value=value+1;

if(value==1)
{
Serial.print(distance);
Serial.println(" cm");

servoMain.write(0);
delay(200);
servoMain1.write(90);
delay(3000);// 3 sec delay
servoMain1.write(0);
delay(200);
servoMain.write(45);
delay(200);
}
if(value>1)
{
Serial.print(value);
Serial.println("***");
Serial.print(distance);
Serial.println(“cm limited speed”);
servoMain.write(45);
}

}
if(distance<30)
{
value=0;
Serial.print(distance);
Serial.println(“cm Break”);
servoMain.write(0);
delay(200);
servoMain1.write(90);
}
if(distance>60)
{
Serial.print(distance);
Serial.println(“cm Speed”);
servoMain1.write(0);
delay(200);
servoMain.write(90);
}

}

else
{
Serial.print(“Switch OFF”);
Serial.println(" ");
servoMain.write(0);
servoMain1.write(0);
}

}

}

}

can u plz edit it so that the first loop executes if it goes from reverse..

I may be able to give you some ideas but I am not sure that I will give you the full solution. What do you mean by "the first loop" ?

In any case I will do nothing until you post the code properly without smileys in it.
Read this then post the code again.

UKHeliBob:
I may be able to give you some ideas but I am not sure that I will give you the full solution. What do you mean by “the first loop” ?

In any case I will do nothing until you post the code properly without smileys in it.
Read this then post the code again.

#include <Servo.h>
Servo servoMain; // Define our Servo
Servo servoMain1; 
#define trigPin 13
#define echoPin 12
const int switchpin = 4;    
int switch_state = 0;
int value;
void setup()

{
  Serial.begin (9600);  
  servoMain.attach(8); // servo on digital pin 8
  servoMain1.attach(9); // servo on digital pin 9
  pinMode(switchpin,INPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}



void loop()
{
switch_state = digitalRead(switchpin);

if(switch_state == HIGH)
{
    Serial.print("Switch ON");
    Serial.println(" ");
    
    value=0;
    
    servoMain.write(90);
    servoMain1.write(0);
    delay(200);
    
while(1)
 {
      switch_state = digitalRead(switchpin);

if(switch_state == HIGH)
{

    long duration, distance;

    digitalWrite(trigPin, LOW);  // Added this line

    delayMicroseconds(2); // Added this line

    digitalWrite(trigPin, HIGH);
    //delayMicroseconds(1000); - Removed this line

    delayMicroseconds(10); // Added this line
                                                                                                                                                         
    digitalWrite(trigPin, LOW);

    duration = pulseIn(echoPin, HIGH);

    distance = (duration/2) / 29.1;
    
    delay(500);
    

   if ((distance>30)&&(distance<60)) 
    { 
      value=value+1;
      
      if(value==1)
      {
      Serial.print(distance);
      Serial.println(" cm");
      
      servoMain.write(0); 
      delay(200); 
      servoMain1.write(90);
      delay(3000);// 3 sec delay
      servoMain1.write(0);
      delay(200);
      servoMain.write(45); 
      delay(200);
     }
           if(value>1)
      {  
      Serial.print(value);
      Serial.println("***");  
      Serial.print(distance);
      Serial.println("cm limited speed");  
      servoMain.write(45); 
      }  
     
   }
    if(distance<30)
     {
      value=0;
      Serial.print(distance);
      Serial.println("cm Break");  
      servoMain.write(0);
      delay(200);
      servoMain1.write(90);
     }
    if(distance>60)
     {
      Serial.print(distance);
      Serial.println("cm Speed");  
      servoMain1.write(0);
      delay(200);
      servoMain.write(90);
     }
  
}
   
else
{
    Serial.print("Switch OFF");
    Serial.println(" ");
    servoMain.write(0); 
    servoMain1.write(0);
} 
   
 
   }

  }

}

let me give an example with above program… suppose object detected at 20, " if(distance < 30)" loop will be executed. But suppose after detection, the object moves away to 50 i want the program to go to “if (distance >60)” directly… Not to “if ((distance>30)&&(distance<60))” loop… hope you get it.

ie: first time when it detects, it should go to appropriate loop. once detected and object moves away, it should not go and execute other loops one by one until it reaches > 60… Instead it should go directly to > 60 once object moves

No wonder I was confused. None of the things that you are referring to as loops are actually loops. The code executed when an if test returns true is executed once (unless it contains a loop, of course)

first time when it detects, it should go to appropriate loop. once detected and object moves away, it should not go and execute other loops one by one until it reaches > 60… Instead it should go directly to > 60 once object moves

Leaving aside the innappropriate reference to loops, what you could do is set a boolean variable to true when distance < 30. Suppose that it was called hasBeenLessThanThirty then you could use it in other tests like this

if ((distance>30)&&(distance<60) && !hasBeenLessThanThirty)

The code will then only be executed if the distance has not previously been less than 30. Depending on what you want to do you will need to set hasBeenLessThanThirty back to false again at some time.

UKHeliBob:
No wonder I was confused. None of the things that you are referring to as loops are actually loops. The code executed when an if test returns true is executed once (unless it contains a loop, of course)
Leaving aside the innappropriate reference to loops, what you could do is set a boolean variable to true when distance < 30. Suppose that it was called hasBeenLessThanThirty then you could use it in other tests like this

if ((distance>30)&&(distance<60) && !hasBeenLessThanThirty)

The code will then only be executed if the distance has not previously been less than 30. Depending on what you want to do you will need to set hasBeenLessThanThirty back to false again at some time.

Sorry man… I meant the if condition. My bad :frowning: so how to skip the if condition ? Like you said above? Can you edit it in the program?

Edited: ( sorry for telling if as loop :stuck_out_tongue: )
suppose object detected at 20,program under " if(distance < 30)" case will be executed. But suppose after detection, the object moves away to 50 i want the program to go to “if (distance >60)” case directly… Not to “if ((distance>30)&&(distance<60))” case. hope you get it.

And thanks so much for your time… Am a newbie :confused:

Can you edit it in the program?

You would learn more if you tried to do it yourself. Have a stab at it and post your program here whether it works or not, describing any problems that you encounter.