Go Down

Topic: Wall avoidance robot help please! (Read 1 time) previous topic - next topic

chuey157

Hey everyone first time posting.

I am making a wall avoidance robot using Arduino however I am having issues getting the code to run the way I want.

My issue is: The motor will not operate.

Observations:The serial monitor shows that the sensor intermittently takes data very quickly and at the intended rate at other times.

Also,of the LEDs on the driver (A,B,C,D) A lights up and stays lit. The motor itself will also end up becoming warm if left plugged in while inspecting code.

I have also noticed that my sensor will return 0's when plugged into my breadboard unless I wiggle the pins into a certain position.

My setup is:
Arduino Uno board
HC-SR04 Ultrasonic distance sensor
28byj-48 5 V step motor
Step control board that looks like this one http://www.hobbyking.com/hobbyking/store/__26925__Stepper_Motor_and_Driver_Board_5V.html?gclid=Cj0KEQjw4J-6BRD3h_KIoqijwvkBEiQAfcPiBaDQKext3OZSx4mQhigDhAukq9Po6z5QuXbgatzFavgaAhbP8P8HAQ

My code is:
Code: [Select]
/*Mark II moves forward as the code states. Now I will attempt to get it to read the distance
and move at the same time*/

//all necessary for the distance calculator
int echoPin=11;
int trigPin=12;
int ledPin=13;
int val=0;
long duration;

//stepper library
#include <CustomStepper.h>

//required statement for the motor
CustomStepper stepper(2,3,4,5,(byte[]){8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001}, 4075.7728395, 12, CW);

void setup() {
//sets rpm
stepper.setRPM(12);
//sets step per rotation. I dont actually know what to put but the other code works so what the hell
stepper.setSPR(4075.7728395);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}

void loop()
{
  digitalWrite(trigPin, LOW);
  delay(2);

  digitalWrite(trigPin, HIGH);
  delay(10);

  digitalWrite(trigPin, LOW);
  duration=pulseIn(echoPin, HIGH);
 
  //distance is in cm based on speed of sound
  val=duration/58.2;
  Serial.print("Distance:  ");
  Serial.print(val);
  Serial.println(" cm");
  delay(1000);
  if (stepper.isDone())
  {
    stepper.setDirection(CCW);
    stepper.rotate();
  }
  stepper.run();
}


If you need any more information from me I would be more than happy to provide it so I can get this project moving again!

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

These can be byte:
int echoPin=11;
int trigPin=12;
int ledPin=13;

Use unsigned long for time events:
long duration;

Put # statements at the top of the sketch:
//stepper library
#include <CustomStepper.h>

type should be float?
int val=0;
but code is:
val=duration/58.2;

No idea what this does, looks like too much running together to me. Does it compile?
//required statement for the motor
CustomStepper stepper(2,3,4,5,(byte[]){8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001}, 4075.7728395, 12, CW);



Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Rupert909

#3
May 28, 2016, 01:00 pm Last Edit: May 28, 2016, 01:17 pm by Rupert909
Code: [Select]
digitalWrite(trigPin, LOW);
  delay(2);

  digitalWrite(trigPin, HIGH);
  delay(10);


I believe it should be delayMicroseconds(10) instead of delay(10) following the setting of trigPin to HIGH, before it is written LOW again.

Also, if your timeout is too short, you may well be making a new ping before receiving the echo of the previous ping, getting very confusing results. I use interrupt pin 2 rather than pulseIn, and do a delay of 50-100 ms after sending ping. The interrupt routine just stores the current time in microseconds in a variable, which I then check after the sleep. Works nicely.

Added:

looked at your code in more detail. You are omitting the third parameter to pulseIn, which is the timeout in microseconds (unsigned long). You should set it to at least 50000UL. In 50 milliseconds, sound travels 17 meters, corresponding to an echo distance of 8.5 meters. It is doubtful if the strength of the sound emitted is sufficient to create echos at much bigger distances.


The internet is a great thing. Use it.
:s/internet/mind/g
Real Programmers use ++C

PaulS

Code: [Select]
stepper.setSPR(4075.7728395);
I have never heard of a stepper motor that had anything but an integral number of steps per revolution, and nothing anywhere near that high.

You really need to pay some attention to the types of values you are using AND the number of significant digits in those values. That value is not a meaningful float value.

TomGeorge

Hi,
Have you written your code a step at a time.
Write code to get the motors working and under control
Write code to activate and interpret the sonar.
Then put them together?

Did the stepper library come with example code?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Tom.... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up