I would greatly appreciate some help in coding. I would like to have the stepper motor and ultrasonic work at the same time. My project is to have the stepper motor move the ultrasonic sensor. Once the the ultrasonic detects an object, it will start beeping using a buzzer. Thats what I have so far:#include <Stepper.h> #define STEPS 255 // the number of steps in one revolution of your motor (28BYJ-48)
const int stepsPerRevolution = 2038;
const int trigPin = 7;
const int echoPin = 6;
const int buzzer = 12;
const int ledPin = 13;
long duration;
int distance;
int roundAngle;
int Angle;
Stepper stepper(STEPS, 8, 10, 9, 11);
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
stepper.setSpeed(60);
stepper.step(1300);
stepper.step(-1300);
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// 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;
driving a stepper-motor requires to create a pulse-train.
measuring distance with an ultrasonic sensor requires to initiate a "ping"
and then waiting for the echo to arrive measuring the time until the echo arrives.
So these are two things that must be done in parallel.
A more specialised library like the MobaTools can drive a stepper-motor "in the backround". This enables to do the measurings with the ultrasonic sensor "in the foreground"
Programming is not as short as
"can somebody post the cheatkey to enter platinum-level?"
This means if you don't want to depend always on other users modifying your code there are some things to learn. Once you have a little experience with the "testing-method" it becomes fun to try and test.
You can combine two things to move up the learningcurve:
Posting your actual code that tries to achieve your goal asking a specific question. The probalitiy to get quick answers is much higher for specific questions about one detail than for generalised questions.
And posting your attempt clearly shows own effort and the will to learn
From your two postings (yes I saw the other post too) I estimate that you are a real beginner in writing code. This is completely OK!
I recommend take a look into this tutorial:
Arduino Programming Course
It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial. Even if it is just two or three words.
Try to make a small modification to your complete sketch and post this new version with a specific question. Or If you don't like that post a specific question about that part you want to read a tutorial.