Non-Working Compiled Simple Elevator Code

Hi guys,

the code i am going to write belongs to simple elevator of 3 floored-apartment.

it is compiled,but when i try it with the sytem,the elevator doesnt move anyfurther.

what might be the mistake? please help.

ps. the system is based upon arduino uno,one dc motor,l298n and ultrasonic sensor.In addition
elevator and ultrasonic is working with another codes(but out of the aim of this Project) very well,as you see there is nothing wrong with the connections.

 #define CURRENTFLOOR2 5
#define CURRENTFLOOR1 32
#define CURRENTFLOOR0 62
 
#define TARGETSTOPS   999
#define TARGETFLOOR2               200       
#define TARGETFLOOR1       100
#define TARGETFLOOR0       0
 
int target = TARGETSTOPS;

    // defines pins numbers
const int trigPin = 11;
const int echoPin = 10;

// defines variables
long duration;
int distance;


 
void setup() {
    pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication

  pinMode(3,OUTPUT); //IN1   dc motor
  pinMode(5,OUTPUT); //IN2   dc motor

  
  
  pinMode(6,INPUT); //button floor 0
  pinMode(7,INPUT); //button floor 1
   pinMode(8,INPUT); //button floor 2
   
 
}
 
void loop() {
    digitalWrite(trigPin, LOW);
  
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(2);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
 
        if(target == TARGETSTOPS) return;
        
        
        if( target == TARGETFLOOR0 )
        {
                if( distance < TARGETFLOOR0){
                        digitalWrite(3,0);     
                        digitalWrite(5,1);
                }else if( distance > TARGETFLOOR0 ){
                        digitalWrite(3,1);     
                        digitalWrite(5,0);              
                }else{
                        digitalWrite(3,0);     
                        digitalWrite(5,0);                      
                }
        }
        
        
        if( target == TARGETFLOOR1 )
        {
                if( distance < TARGETFLOOR1){
                        digitalWrite(3,0);     
                        digitalWrite(5,1);
                }else if( distance > TARGETFLOOR1 ){
                        digitalWrite(3,1);    
                        digitalWrite(5,0);              
                }else{
                        digitalWrite(3,0);     
                        digitalWrite(5,0);                      
                }
        }
        
                
        if( target == TARGETFLOOR2)
        {
                if( distance < TARGETFLOOR2){
                        digitalWrite(3,0);     
                        digitalWrite(5,1);
                }else if( distance > TARGETFLOOR2 ){
                        digitalWrite(3,1);    
                        digitalWrite(5,0);              
                }else{
                        digitalWrite(3,0);     
                        digitalWrite(5,0);                      
                }
        }
 
}

You have debug prints in your code.
They tell you stuff.

Make use of that information, and add more prints if you need them.

Give the pins names, and use the pin names, not magic numbers
.

i have forgotten to write what do those codes mean.are they the''debug prints'' you are talking about?
i really didnt get what does the numbers belongs to targetfloor definings.but i predict that it is written
for comparisons leading to decision of arduino.

pins names have already been given void setup, what else should i do?

 #define CURRENTFLOOR2 5  //cms far away from ultrasonic sensor
#define CURRENTFLOOR1 32   //cms far away from ultrasonic sensor
#define CURRENTFLOOR0 62   //cms far away from ultrasonic sensor
 
#define TARGETSTOPS   999
#define TARGETFLOOR2               200       
#define TARGETFLOOR1       100
#define TARGETFLOOR0       0

pins names have already been given void setup, what else should i do?

i really didnt get what does the numbers belongs to targetfloor definings

Why not write your own code? You will learn more that way.

pins names have already been given void setup, what else should I do

Which of the following is more meaningful to you, and what could you do to make it more meaningful?

               if( distance < TARGETFLOOR0){
                        digitalWrite(3,0);     
                        digitalWrite(5,1);
               if( distance < TARGETFLOOR0){
                        digitalWrite(motor_IN1,LOW);     
                        digitalWrite(motor_IN2,HIGH);

Only two posts, using a new user name?

Guys guys guys...yes we had very patient teaching period of delta G to me, But things didnt work.
near the dead end ,tension went high,
we could have had a very hard fight with delta G in terms of the manners based upon teaching-learning ethics,but administrator of the page probably has deleted the last letter of me,thus everything has göne.

Yes we can know discuss the case for hours,days and weeks.
but i wont.

i have tried lots of codes in terms of my knowdledge and it failed, and i couldnt have found a simple code
of elevator in the internet..there are always complex variables.delta g tried to teach me a way,but i couldnt have got what he said.and we couldnt have constructed a comminucation bridge.thats all
there is no need to WATCH OUT or something.

what i want is yes... ''someone correct my mistake''
because i am not good at shaping variables.

let me tell you an example of what i want:

before month ago i have had a question like this:

hey guys i write a code 45<=potvalue<=60 BUT HEY IT DIDNT WORK!

a coder has corrected my mistake like this:

potvalue<=45 && potvalue<=60

Yes i have taken this corrected code to my ''else if ' construction
based upon a Project of 433 mhz RC with potentiometer and it worked.

this time all i wanna have a correction like this as well.

but if it seems not ethical, ( because you may think correcting the code of this Project takes
too long, it means work explotion.i understand it.)

but if someone can write me a simple code, i will understand the algorythm much better.

i know guidance style teaching is much healthier, (as delta g did,and j remington is on the same way as well.) but in this case things got stucked and dont move any further.

well the mechanic elevator that i have done...it has only up&down button, driving the cabinet up and
down and at least it is making a 4 year old kid happier.

but the code….becomes a mystery. :))), i will appreciate direct helps and

i wont get into single piece of discussion,polemics or what so ever based upon manners, because in this case i have lost sufficient times of time and energy and doesnt want someone to lose it as well.

best wishes for everyone.

We strongly recommend that people start with the simple examples that come with the Arduino -- learn to read a button, a voltage, a sensor, blink an LED without using delay(), etc. to learn the special features of the Arduino.

Also study the language tutorials on line -- there is one for every little feature of the programming language.

That way you avoid endless frustration.