0

I have a problem with my Stepper Motor and Ultrasonic Sensor. I want to have the Sensor reading the distance, and when it is smaller than 20, it tells the Stepper Motor to stop. I can read the distance but can't start the motor with the pushbutton I have. If I comment the Sensor-section out, I can start the motor with the pushbutton.

I would be very happy if you guys could help me :)

My code:

#include <avr/interrupt.h>
#include <avr/io.h>
#include "TimerOne.h"

// defines pins numbers
const int interrupt = 2;
const int dirPin = 3;
const int stepPin = 4; 
const int MS0 = 5;
const int MS1 = 6;
const int MS2 = 7;
const int StepperMotorMode = 8;
const int ButtonFor = 9;
const int ButtonBack = 10;
const int echoPin = 11;
const int trigPin = 12;
const int LED = 13;
const int StepON = 250;
const int StepOFF = 1000;

// defines variables
int ButtonForState = 0;
int ButtonBackState = 0;
int FlagState = 0;
int distance;
long duration;
volatile char Flag = 0;

void setup() {
// Sets the two pins as Outputs
  pinMode(stepPin, OUTPUT); 
  pinMode(dirPin, OUTPUT);
  pinMode(MS0, OUTPUT);
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(StepperMotorMode, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(LED, OUTPUT);

  pinMode(ButtonFor, INPUT);
  pinMode(ButtonBack, INPUT);
  pinMode(echoPin, INPUT);

  Timer1.attachInterrupt(UltraSonic);   // Attach interrupt to the timer service routine 
  Serial.begin(9600);                   // Starts the serial communication
}


void loop() {
// Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  ButtonForState = digitalRead(ButtonFor);
  ButtonBackState = digitalRead(ButtonBack);
  FlagState = digitalRead(Flag);

  digitalWrite(StepperMotorMode,LOW);


  if(ButtonForState == HIGH) {
    digitalWrite(StepperMotorMode,HIGH);
    digitalWrite(dirPin,HIGH); // Enables the motor to move forwards

// Acceleration

// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 5000; x++) {
  digitalWrite(MS0,LOW);
  digitalWrite(MS1,LOW);
  digitalWrite(MS2,LOW);
  digitalWrite(stepPin,HIGH); 
  delayMicroseconds(StepON); 
  digitalWrite(stepPin,LOW); 
  delayMicroseconds(StepOFF);

  if(Flag == 1){
    Flag = 0;
    break;
  }
}
  }

  ButtonForState = 0;
  digitalWrite(StepperMotorMode,LOW);

  if(ButtonBackState == HIGH) {
digitalWrite(StepperMotorMode,HIGH);
digitalWrite(dirPin,LOW); // Enables the motor to move backwards

//Acceleration

// Makes 200 pulses for making one full cycle rotation
for(int x = 0; x < 5000; x++) {
  digitalWrite(MS0,LOW);
  digitalWrite(MS1,LOW);
  digitalWrite(MS2,LOW);
  digitalWrite(stepPin,HIGH); 
  delayMicroseconds(StepON); 
  digitalWrite(stepPin,LOW); 
  delayMicroseconds(StepOFF);
}
  }

  ButtonBackState = 0;
  digitalWrite(StepperMotorMode,LOW);

}

void UltraSonic()  {
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  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.println(distance);

  if(distance < 20){
Flag = 1;
  }
}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.