hi! I am trying to build a maze solver robot,but i am facing a problem when i upload the code to controll the robot. I am using Arduino Uno (R3), BYJ48 stepper motor and HC04 ultrasonic sensor.
If a upload a code that makes the robot only walk straight it works fine, but when i add reading values from the sensor the robot moves veryy slow and the L led in arduino blinks constantly. Pleaseeee help!!
when i add reading values from the sensor the robot moves veryy slow
Look at what happens in your code. You trigger the ping sensor to send a pulse. That doesn't take long, even waiting for the pulse to be sent several times. Then you turn the echo pin on, and wait for an echo. How long does that take?
If the distance is less than 5, you change the destination position, take a step on each stepper if it is time, set the speed to the same speed it was set to.
Then, you take a step on each stepper if it is time, set the speed to the same speed it was set to.
Then, you stuff your head in the sand for far longer than any of that took. And, the robot moves slowly.
Well, stop stuffing your head in the sand for so long. For ANY time, for that matter.
If you don't need to check the distance on every pass through loop() (and you don't), delay() is NOT the answer. The blink without delay example, which you should thoroughly understand, IS.
Consider, too, why you need to keep setting the stepper motor speed to the same value.
PaulS:
If you don't need to check the distance on every pass through loop() (and you don't), delay() is NOT the answer. The blink without delay example, which you should thoroughly understand, IS.
Consider, too, why you need to keep setting the stepper motor speed to the same value.
if i remove stepper2.setSpeed(stepperSpeed); the motor doesnt move. what should i do?
PaulS:
How many of the setSpeed() calls did you remove?
i know i should change sth in the reading values from the sensor, i removed the delay but nothing happens. some code would be helpful, what should i change?
AWOL:
Yup, I'd love to see your code.
I may have already mentioned this
here is my code
#include <AccelStepper.h>
#define HALFSTEP 8
#define echoPin 8 // Echo Pin
#define trigPin 9 // Trigger Pin
#define motorPin1 4 // IN1 on the ULN2003 driver 1
#define motorPin2 5 // IN2 on the ULN2003 driver 1
#define motorPin3 6 // IN3 on the ULN2003 driver 1
#define motorPin4 7 // IN4 on the ULN2003 driver 1
#define motorPin5 10 // IN1 on the ULN2003 driver 2
#define motorPin6 11 // IN2 on the ULN2003 driver 2
#define motorPin7 12 // IN3 on the ULN2003 driver 2
#define motorPin8 13 // IN4 on the ULN2003 driver 2
AccelStepper stepperDjathtas(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepperMajtas(HALFSTEP, motorPin6, motorPin8, motorPin5, motorPin7);
int stepperSpeed = 1000;
long duration, distance;
void setup() {
//sensor
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// stepper motor
delay(3000);
stepperDjathtas.setMaxSpeed(2000.0);// perc max e shpejtesise se lejuar, funx run do te pershpejtohet deri ne kete vlere
stepperDjathtas.setSpeed(stepperSpeed); //Sets the motor speed in rotations per minute (RPMs).
//This function doesn't make the motor turn, just sets the speed at which it will when you call step().
stepperMajtas.setMaxSpeed(2000.0);//
stepperMajtas.setSpeed(stepperSpeed); //Sets the motor speed in rotations per minute (RPMs).
//This function doesn't make the motor turn, just sets the speed at which it will when you call step().
}
void loop() {
/* The following trigPin/echoPin cycle is used to determine the
distance of the nearest object by bouncing soundwaves off of it. */
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
if (distance < 5) {
stepperMajtas.moveTo(-stepperMajtas.currentPosition()); //Reverse motor
stepperMajtas.run();
stepperDjathtas.run();
stepperDjathtas.setSpeed(stepperSpeed);
}
stepperDjathtas.run();
stepperDjathtas.setSpeed(stepperSpeed);
stepperMajtas.run();
stepperMajtas.setSpeed(stepperSpeed);
//Delay 50ms before next reading.
delay(50);
}
PaulS:
So, you put it back? Why? What does "nothing happens" mean?
i removed all the delays and the problem was still there, two motors still move slow. then i put it back again because i thought that didn't caused the problem. I need to understand what is causing the problem because i don't have too much info about electronics and i thought you guys would help.