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);
}
}
}