So I need to a make project which is part of a live display. I have never worked on stepper motors prior to this, only dc and servos.
So basic idea is for an ultrasonic (US) sensor to detect something which will signal the stepper motor to rotate 3 times in one direction and then 3 times back to its original position. Where it will finally comeback to stop until the US sensor detects something again.
I will be using an HC-SR04 ultrasonic sensor and a Nema 23 stepper motor. I will also be using a micro step drive and an Arduino Uno with a shield.
So far the system works with a PIR infrared sensor (which I did not make) and I need to convert it to an ultrasonic sensor.
Would I need any special stepper or ultrasonic libraries to do this.
Here is the code for system using the PIR sensor.
// declaring global pins
#define DIR_PIN 2
#define STEP_PIN 3
#define ENABLE_PIN 4
//declaring pins
int ledPin = 13; // choose the pin for the LED
int inputPin = 5; // choose the input pin (for PIR sensor)
//int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin statusvoid stop1()
void setup() {
Serial.begin(9600);
//defining outputs
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(ENABLE_PIN, OUTPUT);
pinMode(ledPin, OUTPUT); // declare LED to test as output
//input from PIR
pinMode(inputPin, INPUT); // declare sensor as input
// delaying the sensor by 3 secs when the setup starts
for(int i=0;i<3;i++)
{
// Serial.println(" ");
delay(1000);
}
}
#define STEPS 800
void constantAccel(int highSpeed) {
int i;
int delays[STEPS];
float angle = 1;
float accel = 0.01;
float c0 = 2000 * sqrt( 2 * angle / accel ) * 0.67703;
float lastDelay = 0;
// int highSpeed = 3000;
for (int i = 0; i < STEPS; i++) {
float d = c0;
if ( i > 0 )
d = lastDelay - (2 * lastDelay) / (4 * i + 1);
if ( d < highSpeed )
d = highSpeed;
delays[i] = d;
lastDelay = d;
}
// use delays from the array, forward
for (int i = 0; i < STEPS; i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds( delays[i] );
digitalWrite(STEP_PIN, LOW);
Serial.println(delays[i]);
}
// use delays from the array, backward
for (int i = 0; i < STEPS; i++) {
digitalWrite(STEP_PIN, HIGH);
delayMicroseconds( delays[STEPS-i-1] );
digitalWrite(STEP_PIN, LOW);
//Serial.print(i);
}
}
bool flag=0;
void loop()
{
val = digitalRead(inputPin); // read input value
//Serial.println(val);
if(val == HIGH && flag==0)
{
digitalWrite(ledPin, HIGH);
digitalWrite(DIR_PIN, HIGH);
for(int j=0;j<3;j++)
{
constantAccel(500);
}
flag=1;
//delayMicroseconds(3);
}
else if(val==LOW && flag==1)
{
digitalWrite(ledPin, LOW);
digitalWrite(DIR_PIN, LOW);
for(int j=0;j<3;j++)
{
constantAccel(1000);
}
flag=0;
//delayMicroseconds(3);
}
}
Am I correct to think that your program causes the stepper motor to move as you want based on the input from the PIR sensor?
And that the PIR sensor is detected with the line
val = digitalRead(inputPin);
If so then all you need to do is write some code to update the variable val based on input from the Ultrasonic sensor rather than the PIR sensor.
You may have saved yourself some trouble if you had used the AccelStepper library.
Robin2:
Am I correct to think that your program causes the stepper motor to move as you want based on the input from the PIR sensor?
And that the PIR sensor is detected with the line
val = digitalRead(inputPin);
If so then all you need to do is write some code to update the variable val based on input from the Ultrasonic sensor rather than the PIR sensor.
You may have saved yourself some trouble if you had used the AccelStepper library.
...R
Thank you for getting back.
Yes the programme works perfectly fine right now , taking input from the PIR sensor. I did not make it however.
My role is to change it, to take input from the ultrasonic senor.
easy-8:
My role is to change it, to take input from the ultrasonic senor.
Then I suggest that you put your working program to one side for a while and write another separate short program to get data from the ultrasonic sensor and display the results on the Arduino Serial Monitor.
Robin2:
Then I suggest that you put your working program to one side for a while and write another separate short program to get data from the ultrasonic sensor and display the results on the Arduino Serial Monitor.
Google should find you lots of examples.
...R
Would it be possible to remove the PIR values from the existing bit and replace it with the values from Ultrasonic sensor, or make new programme from scratch
Robin2:
First thing is to write a short program to learn how to use the ultrasonic sensor without any stepper motor stuff.
When you can do that we can consider how to integrate that learning with your existing program.
...R
Hi,
So I looked up this simple ultrasonic programme online, which updated the distance in the serial monitor in the IDE every few seconds in inches and cm. What do you suggest I work on now.
I don't have any experience with one of those sensors. You code seem to be the same as this tutorial
I am surprised to see pinMode() commands inside loop(). I always put them in setup().
Anyway, if the program works properly for you what distance do you want it to trigger at? And can you figure that out in microseconds so you don't have to waste time on conversions?
Supposing you want it to trigger if the duration is less than 18,000 microsecs then I think you could integrate it with your other program like this
Robin2:
I don't have any experience with one of those sensors. You code seem to be the same as this tutorial
I am surprised to see pinMode() commands inside loop(). I always put them in setup().
Anyway, if the program works properly for you what distance do you want it to trigger at? And can you figure that out in microseconds so you don't have to waste time on conversions?
Supposing you want it to trigger if the duration is less than 18,000 microsecs then I think you could integrate it with your other program like this
if (duration < 18000) {
val = HIGH;
}
...R
Yes, I'm using the 4 pin ultrasonic sensor (HC-SR04). The one you linked is for the 3 pin based sensor.
So I want the sensor to trigger when it detects something at 250 cm (2.5m). So according to this formula 250 cm = 7278 ms
Time(Ms) = [Distance (Cm)]/ [0.03435 (Cm/Ms)] // 0.03435 cm/ms being the speed of sound at 20°C.