Im new to arduino,
I want my ultrasonic sensor to activate once an object is within 30cm, once it activate it should rotate a stepper motor one full rotation, than back the same way to its rest position, but instead, once something has triggered the ultrasonic sensor it will go clockwise, than counterclockwise, and than clockwise and wont stop, my goal is so once the sensor has gone counterclockwise it should start scanning again until something is close enough to trigger it.
#include <Stepper.h>
// change this to the number of steps on your motor
#define STEPS 200
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 4, 5, 6, 7);
int trigPin = 9;
int echoPin = 8;
void setup()
{
Serial.begin(9600);
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
stepper.setSpeed(60);
}
void loop(){
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
Serial.print ("cm ");
Serial.print (distance);
if (distance < 30)
{
stepper.step(STEPS);
for(int x = 0; x < 250; x++) {
stepper.step(-STEPS);
delay(600);
}
delay(1000);
}
}
Have a look at the idea used in the state change detect tutorial which looks for a button to become pressed as distinct from is pressed.
You need to use the same thinking: have a currentDistance and a previousDistance, and only trigger the stepper if previousDistance was out of range, and currentDistance is in range (ie, the distance has only just become in range.) Then of course you update previousDistance to be currentDistance so next time thru loop(), previousDistance is in range and your if() fails.
It will only work again once the item goes out of range and comes back.
windoze_killa:
Once the object has come within 30cm what happens to it? Does it reverse and move away? Does your sensor expect it to come backagain?
once something has come within 30cm the motor starts, (as intended) but the motor will not stop rotating both clockwise and counterclockwise.
I would like it so that once something comes in the 30cm mark that the motor spins 1 full clockwise rotation, than one full counterclockwise rotation, and than stop. until something comes back in the sensors 30cm view again, so i intend that the sensor is on a continuous loop waiting for something to come within 30cm than for it to activate the motor
So if the object comes within 30cm does it move away again? If not that is why it keeps going. If it is moved away then maybe I need to look at it again.
windoze_killa:
So if the object comes within 30cm does it move away again? If not that is why it keeps going. If it is moved away then maybe I need to look at it again.
yes, the object is removed right after the sensor activated the motor, but instead of doing its 1 cycle both ways and stopping it continues even tho nothing is within the 30cm view
As I said in #1, you need to adopt the kind of thinking used in the state change detect example.
You continually check the distance, and when it becomes less than 30 (from being over 30) you spin the motor. Then it can stay under 30 until Armageddon and never spin again. When it goes back out beyond 30, that sort of primes it for the next time it comes back in.
ps, I think I may have a sketch that works along those lines on my personal laptop; alas that's at home and I'm at work. It won't be a sketch that does stuff with a stepper, but iirc I did one to blink an led once or twice when the limit was breached and then stop until the next time.
I'll look when I get home but that's some hours away.
fionning_macara:
As I said in #1, you need to adopt the kind of thinking used in the state change detect example.
You continually check the distance, and when it becomes less than 30 (from being over 30) you spin the motor. Then it can stay under 30 until Armageddon and never spin again. When it goes back out beyond 30, that sort of primes it for the next time it comes back in.
thats it my goal yes, but im really new to arduino and im not to sure how to put them all together, since the code i have right now is failing on me im not to sure how to fix it
if (distance < 30 and previousDistance >= 30) // modified
{
stepper.step(STEPS);
for(int x = 0; x < 250; x++) {
stepper.step(-STEPS);
delay(600);
}
delay(1000);
}
previousDistance = distance; // New - saves the value for the next iteration
}
...R
that code seems to act the exact same way, i guess it is acting like a switch and it wont turn off. so the motor just keeps spinning even though there is nothing 30cm infront of the sensor
lukas0034:
that code seems to act the exact same way, i guess it is acting like a switch and it wont turn off. so the motor just keeps spinning even though there is nothing 30cm infront of the sensor
I had not noticed this piece of code
for(int x = 0; x < 250; x++) {
stepper.step(-STEPS);
delay(600);
}
That seems to be designed to make the stepper move for 250 x STEPS without allowing the distance to be checked. I suspect you should remove the FOR loop
That seems to be designed to make the stepper move for 250 x STEPS without allowing the distance to be checked. I suspect you should remove the FOR loop
...R
That seems to be designed to make the stepper move for 250 x STEPS without allowing the distance to be checked. I suspect you should remove the FOR loop
...R
infact, it kind of works the opposite, as if once it first starts rotating because of an object is within the 30cm, after that it would only rotate if the item gets closer than previous distance, so eventually it wont be able to turn anymore because the item would be within 1cm of the sensor
lukas0034:
infact, it kind of works the opposite, as if once it first starts rotating because of an object is within the 30cm, ...
That is not an accurate description of the code you refer to in your Reply #14.
The piece of code simply repeats 250 times the instruction to move the stepper motor the number of steps in the variable STEPS with a delay of 600 millisecs between moves.
250 x 600 = 150,000 or 150 seconds or over two minutes of delay