trashbin bot problem

can anyone please help me.
this is the code that im using.
the program is supposed to open a trash can using servo once the sensor is triggered by 0.5m
afterwhich you can press a button to move the stepper and servo

what happened is my sensor and servo cant sync

#include <Servo.h>
#include <Stepper.h>

Servo servo;
Servo servo1;
int servoPin = 5;
int servoPin1 =6 ;

int const trigPin = 12;
int const echoPin = 13;

const int stepsPerRevolution = 2048;
Stepper myStepper = Stepper(stepsPerRevolution, 8, 10, 9, 11);

int buttonPin =2;
int buttonPin2 =3;
int buttonPin3 =4;

int buttonstart= 0;
int button2start= 0;
int button3start= 0;
int button1stop= 0;
int previous = 0;
int previous2 = 0;
int previous3 = 0;

int distadjust=50;
int Bin=3;
int Bin2=3;
int Bin3=3;
int Servoinitial=0;
int Servofinal=75;
int coverServoinitial=0;
int coverServofinal=75;
int i;

void setup()
{

Serial.begin (9600);
servo.attach (servoPin);
servo1.attach (servoPin1);
myStepper.setSpeed(10);

pinMode (buttonPin, INPUT);
pinMode (buttonPin2, INPUT);
pinMode (buttonPin3, INPUT);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

void loop()
{
buttonstart = digitalRead(buttonPin);
button2start = digitalRead(buttonPin2);
button3start = digitalRead(buttonPin3);

int duration, distance;
digitalWrite(trigPin, HIGH);
delay(1);
digitalWrite(trigPin, LOW);
// Measure the pulse input in echo pin
duration = pulseIn(echoPin, HIGH);
// Distance is half the duration devided by 29.1 (from datasheet)
distance = (duration/2) / 29.1;
// if distance less than 0.5 meter and more than 0 (0 or less means over range)

Serial.print(distance);
Serial.print("m");
Serial.println();

if (distance <= distadjust && distance >= 0) {
servo.write(coverServofinal);
delay(3000);
}
if (distance <=1) {
servo.write(coverServoinitial);
delay(3000);
}

delay(60);

if (buttonstart== HIGH && previous == LOW )
{

previous = HIGH;
Serial.println("Bio button selected");
delay(500);
}

if (button2start== HIGH && previous2 == LOW )
{

previous2 = HIGH;
Serial.println("NON-Bio button selected");
delay(500);
}

if (button3start== HIGH && previous3 == LOW )
{

previous3 = HIGH;
Serial.println("Residual button selected");
delay(500);
}

//stepper and servo motor2

if (previous == HIGH)
{
if (distance <=1) {
servo.write(coverServoinitial);
delay(3000);
}
Serial.println("clockwise Bio");
for (i=0; i<Bin; i++)
{
myStepper.step(stepsPerRevolution);
}
delay(500);
servo1.write(Servofinal);
Serial.println("Servo2 Bio Down");
delay(2000);
servo1.write(Servoinitial);
Serial.println("Servo2 Bio Up");
delay(2000);
Serial.println("counterclockwise Bio");
for (i=0; i<Bin; i++)
{
myStepper.step(-stepsPerRevolution);
}
previous = LOW;
delay(500);

}
else
{
if (previous2 == HIGH)
{
if (distance <=1) {
servo.write(coverServoinitial);
delay(3000);
}
Serial.println("clockwise Non- Bio");
for (i=0; i<Bin2; i++)
{
myStepper.step(stepsPerRevolution);
}
delay(500);
servo1.write(Servofinal);
Serial.println("Servo2 Non- Bio Down");
delay(2000);
servo1.write(Servoinitial);
Serial.println("Servo2 Non- Bio Up");
delay(2000);
Serial.println("counterclockwise Non- Bio");
for (i=0; i<Bin2; i++)
{
myStepper.step(-stepsPerRevolution);
}
delay(500);
previous2 = LOW;
}
else
{
if (previous3 == HIGH)
{
if (distance <=1) {
servo.write(coverServoinitial);
delay(3000);
}
Serial.println("clockwise Residual");

for (i=0; i<Bin3; i++)
{
myStepper.step(stepsPerRevolution);

Serial.println("Step");
}
delay(500);
servo1.write(Servofinal);
Serial.println("Servo2 Residual Down");
delay(2000);
servo1.write(Servoinitial);
Serial.println("Servo2 Residual Up");
delay(2000);
Serial.println("counterclockwise Residual");

for (i=0; i<Bin3; i++)
{
myStepper.step(-stepsPerRevolution);
Serial.println("Step2");
}
delay(500);
previous3 = LOW;
}

}

}

}

Instructions for posting code.

migaraymindaro:
what happened is my sensor and servo cant sync

What is that supposed to mean?

Hi,
Welcome to the forum.

What model Arduino are you using?
How are you powering the robot?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

migaraymindaro:
can anyone please help me.
this is the code that im using.
the program is supposed to open a trash can using servo once the sensor is triggered by 0.5m
afterwhich you can press a button to move the stepper and servo

what happened is my sensor and servo cant sync

  Serial.print(distance);
  Serial.print("m");

Shouldn't that be "cm"?

 // if distance less than 0.5 meter and more than 0 (0 or less means over range)
  if (distance <= distadjust && distance >= 0)

If you want to test for "distance > 0" you should not use "distance >= 0".

  if (distance <= 1)
  {

So you want "very close or out of range"?!? I think you just want an 'else' in there to cover "beyond 50cm or out of range (0)":

 // if distance less than 0.5 meter and more than 0 (0 or less means over range)
  if (distance <= distadjust && distance > 0)
  {
    servo.write(coverServofinal);
    delay(3000);
  }
  else
  {
    // distance is > distadjust or distance = 0
    servo.write(coverServoinitial);
    delay(3000);
  }

I call it dead thread. :roll_eyes:

I'll remove this comment if proven wrong! :grinning: