Jump to content


Photo

Trouble with Robotics kit

kit robotics funduino how to make your first robot

  • Please log in to reply
82 replies to this topic

#41 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 24 February 2014 - 03:18 AM

Ignore this accidentally quoted when I meant to edit :P
  • 0

#42 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 24 February 2014 - 04:55 PM

However, I was wondering, how can I mount the sensors facing left and right. Don't breadboard tracks run A-E and F-J? Because, if I mount the sensors facing out, they all the power/signals will be running down one track, and I don't think that would be very good...

 

Yes, you're right. You definitely do not want to put it in the breadboard in such a way that all of the pins are connected. I'm glad you noticed that!

 

The easiest/best solution is to use a cable like this: https://www.foxytron...to-female-cable

 

One of those came in the robotics kit, but since you probably don't have any more of them you'll have to figure out another way to hook up the ultrasonic sensor to the breadboard.

 

One way would be to desolder the pins on the sensor, solder wires on, then connect the wires to the breadboard. You could also put two ultrasonics sensors in the breadboard, then use your cable for the third (this would be less destructive..). For the latter option, you would plug the ultrasonic sensors in with them facing outwards towards the power tracks.


  • 0

#43 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 05 March 2014 - 07:27 PM

I've re-oriented the breadboard so two of the ultrasonic sensors face out, but I've run into a bit an issue with the extension cable for the forward facing one. The cord is rather long and I have no clue how to affix it to the robot! Do you have any ideas?

 

Also, once I get this working, I'd like to add some features besides wall avoidance, just to add the cool/techy factor. Do you have any suggestions for extensions?

 

One last thing, remember when I said way back that I was going to do a write-up? I've started uploading them to my blog and you can see them all here: http://technicalrrr....digialis-robot/. It's a page where you can read straight down through all of the uploaded posts. You can also read them from the main page.


  • 0

#44 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 07 March 2014 - 05:49 PM

Hey, so sorry for taking so long to get back with you! This is the last week before spring break at my school, so there have been lots of exams and stuff! I read your writeup on your website and it looks great! Nice job!

 

 

I've re-oriented the breadboard so two of the ultrasonic sensors face out, but I've run into a bit an issue with the extension cable for the forward facing one. The cord is rather long and I have no clue how to affix it to the robot! Do you have any ideas?

 

Are you asking how to mount the sensor to the robot, or what to do with the cable since it's so long?

 

If you're trying to figure out how to mount the sensor, take a look at how I did it in this video:

 

 

The sensors have mounting holes in the corners and what I did was I poked holes in the cardboard base of the robot, set the sensor on the base, then ran wire through the holes to affix the sensor to the robot. It actually was very sturdy. You could also use right-angle brackets if the wire doesn't work (they're sold at hardware stores and look like this: http://7layerstudio....7dba7970b-320wi).

 

As far as what to do with the cable, I would fold it back and forth over itself, use a zip tie (or wrap electrical tap around it) to keep it from unraveling, then use some tape to fix it to the robot.

 

If that doesn't make sense, let me know and I can post some pics..

 

Also, once I get this working, I'd like to add some features besides wall avoidance, just to add the cool/techy factor. Do you have any suggestions for extensions?

 

Well, there are lots of things you could do!

 

You could easily make it into a line following robot, which is a fun project, by using 5 IR sensors like these:

 

https://www.foxytron...-optical-sensor

 

You could give it the ability to drive perfectly straight by using a gyroscope (which I think are really cool!) like this:

 

https://www.foxytron...ometer-mpu-6050

 

That board actually has 3 gyroscopes and 3 accelerometers, so you can do a lot with it.

 

You could add a color sensor to it, like this one:

 

https://www.foxytron...r-sensor-module

 

Or you could use some photoresistors (I think there were some in your robotics kit?) to make the robot drive towards or away from light sources.

 

Adding an LCD to the robot is also something cool that you could do..

 

If you haven't been on the Arduino forum, be sure to check it out at forum.arduino.cc. I'm sure people could give you a ton of suggestions for projects on there!

 

Also, if I don't have the parts you need, be sure to checkout https://www.sparkfun.com/. They're a much bigger company and have a lot more sensors and parts than me (for now!).


  • 1

#45 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 11 March 2014 - 01:40 AM

Thanks for the ideas! I will most definitely remember those and try to implement them later on, IF, and that's a big if, I manage to get the triple supersonic sensor setup working.

 

I've just started all over from the beginning simply because it will be easier than trying to adapt old code to fit the new design and so far it hasn't worked out. This is my code right now:

// Version 4.0.1
// Major:      4 - An entire re-write of the code. Much is the same,
//                  but written so that the triple set of sensors are used
// Minor:      0 - Minor change is the beginning of the re-write phase
// Micro:      1 - Micro change is the addition of setup(), loop(),
//                  and killButton() functions for debugging

#include <Servo.h>

const int serialPeriod = 250;      // Only print to the serial console every 1/4 second
unsigned long timeSerialDelay = 0;

const int loopPeriod = 20;        // A frequency of 20ms = a frequency of 50Hz
unsigned long timeLoopDelay = 0;

const int ultrasonicFrontTrig = 6; 
const int ultrasonicFrontEcho = 7;

const int ultrasonicLeftTrig = 10;
const int ultrasonicLeftEcho = 8;

const int ultrasonicRightTrip = 11;
const int ultrasonicRightEcho = 9;

int ultrasonicFrontDistance;
int ultrasonicLeftDistance;
int ultrasonicRightDistance;

int ultrasonicDuration;

// Create Servo objects
Servo leftMotor;
Servo rightMotor;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  debugOutput();
  if(millis() - timeLoopDelay >= loopPeriod)
  {
    readUltrasonicSensors();
    
    timeLoopDelay = millis();
  }
}

void readUltrasonicSensors()
{
  digitalWrite(ultrasonicFrontTrig, HIGH);
  delayMicroseconds(10);
  digitalWrite(ultrasonicFrontTrig, LOW);
  
  
  ultrasonicFrontDistance = pulseIn(ultrasonicFrontEcho, HIGH);
  ultrasonicFrontDistance = (ultrasonicFrontDistance/2)/29;
}

void debugOutput()
{
  if ((millis() - timeSerialDelay) > serialPeriod)
  {
    Serial.print("ultrasonicFrontDistance: ");
    Serial.print(ultrasonicFrontDistance);
    Serial.print("cm: ");
    Serial.println();
    
    timeSerialDelay = millis();
  } 
}

I've only coded for the frontal sensor as of right now, and that is what is giving me the issues. I am currently getting

ultrasonicFrontDistance: 0cm:  
ultrasonicFrontDistance: 0cm:  
ultrasonicFrontDistance: 0cm:

constantly. I've double-checked that the pins are all correct and they are. Here's a graphic of my wiring (with the servo wires removed to help keep it simpler):

 

tri-supersonic-e1394501506927.png

If you can see what is going wrong with my code (or my wiring, as that was the original issue that started this whole Forum Foray :P ) I would be very happy. I'm gonna keep attempting to debug it on my own, but otherwise I am at a loss with this. Could it be that it is a power issue? I've tried it both with just the USB power and also with the battery pack, but neither works.

 

I'm going to try stripping the wiring down to just the single sensor and seeing if I can't get it to work.


  • 0

#46 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 11 March 2014 - 03:12 AM

At a cursory glance, I don't see anything wrong with your code, so my guess is that it has something to do with wiring. Do you have both 5V and GND wires going to the breadboard? Your idea of just wiring up a single sensor is a good idea. Reducing the complexity of a circuit and/or program always makes it a lot easier to debug. So try your idea -- just leave one sensor hooked up and take out the code in the program for the other sensors, then see what happens.

 

If you can't figure out what's going wrong, you could always try using code you know works, such as from this tutorial: http://www.foxytroni...ino/programming

 

Let me know what happens!


  • 1

#47 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 15 March 2014 - 03:28 PM

I tried removing all but the front sensor and I'm still getting the 0cm issue. Yargh, I feel like I'm back at square-one. The only thing I can think of is that the sensor is bad, I'm going to try the other two and see if they work, but I can't right now, as I'm not home.

 

I'll try that and re-check my code, possibly copy/paste from the tutorial, and then get back here.

 

Off the topic of fixing my robot, but still regarding the robot, do you know the distance (cm/inch) that the robot considers an object in the way at? I have to write a paper on what I learned, and that would a useful contribution I think.


  • 0

#48 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 16 March 2014 - 12:17 AM

I would definitely suggest grabbing the code from the ultrasonic sensor tutorial and trying it out. If it doesn't work with that code (which we know works), then it's a wiring issue. Sorry you're having trouble getting it to work, I know how frustrating it can be when stuff doesn't work!

 

 

do you know the distance (cm/inch) that the robot considers an object in the way at? I have to write a paper on what I learned, and that would a useful contribution I think.

 

Well, if you're talking about the code from the tutorial, then the robot changes states (from driving forward to turning) when it detects an object 6cm in from of it. The robot can detect objects much further away than that, but that's the point where the code in the tutorial tells the robot to "take action" and turn.

 

Good luck with your paper!


  • 1

#49 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 16 March 2014 - 05:00 PM

Thanks for the distance! That'll help a lot. I tried the new sensor and it still was returning 0cm, both when I connected it through the wire and directly on the breadboard.

 

So, I went to the software. Lo and behold, it worked. It was returning distance that seemed to be correct (moving it back and forth at my hand), but it was doing it a lot faster than it should and there was a slight, I dunno, very fast ticking/whirring sound coming from the sensor. Is this because it is connected via the cord, or what?

 

I glanced at the new robot tutorial and noticed you included a small section for a kill switch. Just wondering, did you do that because of my attempts at it, or had you been planning it? I don't want credit or anything, I'm just naturally curious :D.

 

Thanks!


  • 0

#50 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 16 March 2014 - 06:35 PM

So, I went to the software. Lo and behold, it worked. It was returning distance that seemed to be correct (moving it back and forth at my hand)

 

Great!

 

it was doing it a lot faster than it should and there was a slight, I dunno, very fast ticking/whirring sound coming from the sensor. Is this because it is connected via the cord, or what?

 
The noise is coming from the ultrasonic sensor because of the frequency it's sending sound waves out at (in other words, how quickly it's taking distance measurements). If we read the sensor at a really low frequency, we can't hear it, but if we read it really quickly then we can actually hear the sensor. Kinda cool, actually..
 
What code were you using? If you look at the code in the the ultrasonic sensor tutorial, you can see that there's an IF statement in the loop() function that prevents the sensor from being read very fast, which prevents the noise.
 

I glanced at the new robot tutorial and noticed you included a small section for a kill switch. Just wondering, did you do that because of my attempts at it, or had you been planning it? I don't want credit or anything, I'm just naturally curious  :D.

 

I was working on the tutorial yesterday and it just kind of popped into my head that it might be a good idea  :)  Your efforts may have subconsciously inspired me, though!


  • 0

#51 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 17 March 2014 - 05:02 PM

The sound is related to the frequency... thats what I figured. However, I copied and pasted the code directly from the new tutorial, so I dunno why it would be going too fast. Possibly the extension cord?
  • 0

#52 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 17 March 2014 - 05:29 PM

Hmm, that's interesting.. I'm not sure why it would be making an audible noise if you're using the code from the tutorial. Did you modify the code at all?

 

I don't think the extension cord would cause a noise, but I guess it's possible that it might..


  • 0

#53 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 23 March 2014 - 12:40 AM

I copied the code directly from the new tutorial. However, I have noticed that if I manage to get the distance to 0cm, then it stops detecting for about half a second and the buzzing stops as well. :P Weirdness. I really need to get this working though, seeing as the fair is in a few weeks. Eep! Nothing you can help me with, just me working hard. Thanks for all the help you've provided me with so far.

 

I'm actually thinking about changing my chassis design to the new design in the tutorial. Lego chassis is too complicated and probably too heavy for two servos, at least what I had in mind. Maybe I'll do it later on.

 

Anyways, I've got to get back to developing this bugger. Again, thanks for the help. Maybe we can figure out why the code is running so fast even with the code.

 

EDIT:

 

I have discovered something that may or may not help us. I put in a debug statement to print out the current timeSerialDelay, and it was constantly incrementing upwards. Is this the reason why I am constantly reading from the sensor? However, that still doesn't explain the pauses when it hits zero cm. Any idea?

 

EDIT:

It is definitely the extension cable. I wired the sensor up direct on the breadboard and it's working just like it should. Every 1/4 second, no whirring. That is an issue I would say, as I kinda need that cable and I don't exactly want to be soldering stuff together...


Edited by PatriotAce, 23 March 2014 - 03:50 PM.

  • 0

#54 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 23 March 2014 - 06:44 PM

That's really interesting that the cable is causing the noise! 

 

Yes, timeSerialDelay should keep getting bigger, as it's set to millis() each loop (which is how long in milliseconds the Arduino has been powered on).

 

One thing you could try to eliminate the noise is to increase loopPeriod from 20 to something larger, like 40 (or even 80). That would probably fix the noise issue!

 

 

However, that still doesn't explain the pauses when it hits zero cm. Any idea?

 

What "pauses" are you experiencing?


  • 0

#55 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 23 March 2014 - 08:53 PM

Yeah, I increased the loop period to 250 and it has drastically dropped the whirring, almost to a non-existent level, which is exactly what I wanted. Actually came across something on the Arduino Stack Exchange that resolved it for me :). Those pauses have basically disappeared and aren't really affecting me anymore, it's just when the sensor is looking at nothing, like you mentioned in the tutorial.

 

Anyways, I've got it working to the point where the robot scans until it sees an object close enough and then pings from the two side sensors. However, I've got a new issue. Only one of the sensors is going off, possibly neither of them. Here's the code that is firing them:
 

 

void stateMachine()
{
    
    if(state == DRIVE_FORWARD) // no obstacles detected
    {
        if(ultrasonic2Distance > 6 || ultrasonic2Distance < 0) // if there's nothing in front of us (note: ultrasonicDistance will be negative for some ultrasonics if there's nothing in range)
        {
            // drive forward
//            rightMotor.write(180);
//            leftMotor.write(0);
            if(millis() - timeLoopDelay >= loopPeriod)
            {
            Serial.println("Driving Forward");
            }
        }
        else // there's an object in front of us
        {
            digitalWrite(ultrasonic1TrigPin, HIGH);
            delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
            digitalWrite(ultrasonic1TrigPin, LOW);
            
            digitalWrite(ultrasonic3TrigPin, HIGH);
            delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
            digitalWrite(ultrasonic3TrigPin, LOW);        
    
            ultrasonic1Duration = pulseIn(ultrasonic1EchoPin, HIGH);
            ultrasonic1Distance = (ultrasonic1Duration/2)/29;
            ultrasonic3Duration = pulseIn(ultrasonic3EchoPin, HIGH);
            ultrasonic3Distance = (ultrasonic3Duration/2)/29;
        
            Serial.print("ultrasonic1Distance: ");
            Serial.print(ultrasonic1Distance);
            Serial.println();
            
            Serial.print("ultrasonic3Distance: ");
            Serial.print(ultrasonic3Distance);
            Serial.println();
            
            if(ultrasonic1Distance < ultrasonic3Distance)
            {
              //state = TURN_RIGHT;
              Serial.println("Turning Right");
            }
            
            else if(ultrasonic3Distance > ultrasonic1Distance)
            {
              //state = TURN_LEFT;
              Serial.println("Turning Left");
            }
      }

Very strange. I'm glad I've made the progress I have though, nice to see this project finally getting somewhere :P


  • 0

#56 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 23 March 2014 - 08:53 PM

Can I delete posts? It double-posted...

 

Oo! I'm an Advanced Member now... coolness :) (As if that means anything)


  • 0

#57 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 24 March 2014 - 04:17 PM

Changing loopPeriod to 250 (milliseconds) might create problems, because that means the program will only execute 4 times per second, which is pretty slow. Usually it's best to have it run at a faster rate than that..

Could you post your full code? I can download it to my 'bot and try to see what's happening.


  • 0

#58 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 24 March 2014 - 07:17 PM

Sure, here it is:

// Version 4.0.3
// Major:      4 - An entire re-write of the code. Much is the same,
//                  but written so that the triple set of sensors are used
// Minor:      0 - Minor change is the beginning of the re-write phase
// Micro:      3 - Micro change is addition of side sensors

#include <Servo.h>

// define the states
#define DRIVE_FORWARD  0
#define TURN_LEFT      1
#define TURN_RIGHT     2

// create servo objects
Servo leftMotor;
Servo rightMotor;

int state = DRIVE_FORWARD;

const int serialPeriod = 250;       // only print to the serial console every 1/4 second
unsigned long timeSerialDelay = 0;

const int loopPeriod = 250;          // a period of 20ms = a frequency of 50Hz
unsigned long timeLoopDelay   = 0;

// specify the trig & echo pins used for the ultrasonic sensors
const int ultrasonic2TrigPin = 8;
const int ultrasonic2EchoPin = 9;

const int ultrasonic1TrigPin = 10;
const int ultrasonic1EchoPin = 11;

const int ultrasonic3TrigPin = 12;
const int ultrasonic3EchoPin = 13;

int ultrasonic2Distance;
int ultrasonic2Duration;

int ultrasonic1Distance;
int ultrasonic1Duration;
int ultrasonic3Distance;
int ultrasonic3Duration;

void setup()
{
    Serial.begin(9600);
 
    // ultrasonic sensor pin configurations
    pinMode(ultrasonic2TrigPin, OUTPUT);
    pinMode(ultrasonic2EchoPin, INPUT);
    
    pinMode(ultrasonic1TrigPin, OUTPUT);
    pinMode(ultrasonic1EchoPin, INPUT);
    
    pinMode(ultrasonic3TrigPin, OUTPUT);
    pinMode(ultrasonic3EchoPin, INPUT);
    
    leftMotor.attach(6);
    rightMotor.attach(7);
}


void loop()
{
    debugOutput(); // prints debugging messages to the serial console
    
    if(millis() - timeLoopDelay >= loopPeriod)
    {
        readUltrasonicSensors(); // read and store the measured distances
        
        timeLoopDelay = millis();
    }
    
    stateMachine();
}


void readUltrasonicSensors()
{
    // ultrasonic 2
    digitalWrite(ultrasonic2TrigPin, HIGH);
    delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
    digitalWrite(ultrasonic2TrigPin, LOW);
    
    ultrasonic2Duration = pulseIn(ultrasonic2EchoPin, HIGH);
    ultrasonic2Distance = (ultrasonic2Duration/2)/29;
}

void stateMachine()
{
    
    if(state == DRIVE_FORWARD) // no obstacles detected
    {
        if(ultrasonic2Distance > 6 || ultrasonic2Distance < 0) // if there's nothing in front of us (note: ultrasonicDistance will be negative for some ultrasonics if there's nothing in range)
        {
            // drive forward
//            rightMotor.write(180);
//            leftMotor.write(0);
            if(millis() - timeLoopDelay >= loopPeriod)
            {
            Serial.println("Driving Forward");
            }
        }
        else // there's an object in front of us
        {
            digitalWrite(ultrasonic1TrigPin, HIGH);
            delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
            digitalWrite(ultrasonic1TrigPin, LOW);
            
            digitalWrite(ultrasonic3TrigPin, HIGH);
            delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
            digitalWrite(ultrasonic3TrigPin, LOW);        
    
            ultrasonic1Duration = pulseIn(ultrasonic1EchoPin, HIGH);
            ultrasonic1Distance = (ultrasonic1Duration/2)/29;
            ultrasonic3Duration = pulseIn(ultrasonic3EchoPin, HIGH);
            ultrasonic3Distance = (ultrasonic3Duration/2)/29;
        
            Serial.print("ultrasonic1Distance: ");
            Serial.print(ultrasonic1Distance);
            Serial.println();
            
            Serial.print("ultrasonic3Distance: ");
            Serial.print(ultrasonic3Distance);
            Serial.println();
            
            if(ultrasonic1Distance < ultrasonic3Distance)
            {
              //state = TURN_RIGHT;
              Serial.println("Turning Right");
            }
            
            else if(ultrasonic1Distance > ultrasonic3Distance)
            {
              //state = TURN_LEFT;
              Serial.println("Turning Left");
            }
      }
    }
    else if(state == TURN_LEFT) // obstacle detected -- turn left
    {
        unsigned long timeToTurnLeft = 1100; // it takes around 1.1 seconds to turn 90 degrees
        
        unsigned long turnStartTime = millis(); // save the time that we started turning

        while((millis()-turnStartTime) < timeToTurnLeft) // stay in this loop until timeToTurnLeft (1.1 seconds) has elapsed
        {
            // turn left
            rightMotor.write(180);
            leftMotor.write(180);
        }
        
        state = DRIVE_FORWARD;
    }
    else if(state == TURN_RIGHT) // obstacle detected -- turn right
    {
        unsigned long timeToTurnRight = 1100; // it takes around 1.1 seconds to turn 90 degrees
        
        unsigned long turnStartTime = millis(); // save the time that we started turning

        while((millis()-turnStartTime) < timeToTurnRight) // stay in this loop until timeToTurnRight (1.1 seconds) has elapsed
        {
            // turn left
            rightMotor.write(0);
            leftMotor.write(0);
        }
        
        state = DRIVE_FORWARD;
    }
}


void debugOutput()
{
    
    if((millis() - timeSerialDelay) > serialPeriod)
    {
        Serial.print("ultrasonic2Distance: ");
        Serial.print(ultrasonic2Distance);
        Serial.print("cm: ");
        Serial.println();
        
        timeSerialDelay = millis();
    }
}

  • 0

#59 PatriotAce

PatriotAce

    Advanced Member

  • Members
  • PipPipPip
  • 47 posts
  • LocationLowell, MI, USA

Posted 28 March 2014 - 08:09 PM

Okay, I don't believe that the two side sensors are actually reading the data correctly at all. I have my hand literally pressing against the sensor on the left and it still says that there is some distance in front of the sensor, which is quite obviously wrong. And the sensor on the right still says 0cm constantly. This is most strange, and I'm not experienced enough in Arduino/C to know what to look for :P

 

Basically the programming logic goes:

 

1) Watch for objects in front

2) Object detected, set state to SCAN

3) State is SCAN, Turn ON ultrasonic1TrigPin

4) Delay 10ms

5) Turn OFF ultrasonic1TrigPin

6) Read ultrasonic1EchoPin

7-10) Repeat for ultrasonic3

11) Compare distances

12) Go longer way

 

It looks like the issue might be that the entire stateMachine function isn't getting called at all beyond the first time! Why this is happening, I have no clue, but it is definitely weird and perturbing :P

 

At least we've got the frontal sensor working :D Thinking on the positive side


  • 0

#60 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 28 March 2014 - 11:11 PM

Sorry I didn't get back with you sooner, busy week..

 

I noticed you used

 

if(millis() - timeLoopDelay >= loopPeriod)

 

Several places throughout the program. I would suggest only having that in the loop() function, because it's clearer that way and there's less chance of a mistake being made (like having that check inside of a function that already checked that).

 

loopPeriod is set way to high. At a value of 250 (which is interpreted as milliseconds), it means the sensors are only read every 1/4th second, and the state machine is executed that slowly as well. It's possible it might work, but I think it should probably run much faster than that (maybe try it at 100).

 

You put the code for ultrasonic 1 & 3 inside of the state machine, but it would fit better inside of readUltrasonicSensors(). It creates a little more overhead, since the sensors are read even when there's nothing in front of the robot, but it makes the program a little clearer.

 

This is your problem (probably :P):

            digitalWrite(ultrasonic1TrigPin, HIGH);
            delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
            digitalWrite(ultrasonic1TrigPin, LOW);
            
            digitalWrite(ultrasonic3TrigPin, HIGH);
            delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
            digitalWrite(ultrasonic3TrigPin, LOW);        
    
            ultrasonic1Duration = pulseIn(ultrasonic1EchoPin, HIGH);
            ultrasonic1Distance = (ultrasonic1Duration/2)/29;
            ultrasonic3Duration = pulseIn(ultrasonic3EchoPin, HIGH);
            ultrasonic3Distance = (ultrasonic3Duration/2)/29;

I wouldn't have realized why the above code won't work unless I had read about the pulseIn() function on the Arduino website. pulseIn() is a blocking function, which means that other code can't execute until it's finished. The pulseIn() function blocks until it either detects the pin going HIGH and then going LOW (in which case it returns how long it was high), or it returns 0 if there wasn't a pulse before timeout occurred (timeout is a third, optional parameter for the function).

 

You use this code to get the ultrasonic sensors to send out a "ping":

            digitalWrite(ultrasonic1TrigPin, HIGH);
            delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
            digitalWrite(ultrasonic1TrigPin, LOW);
            
            digitalWrite(ultrasonic3TrigPin, HIGH);
            delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
            digitalWrite(ultrasonic3TrigPin, LOW);        
 
And then you have the pulseIn() statements one after another. The problem is that the first pulseIn() function will block until it receives the pulse, so if the other sensor's "ping" returns before the first one, then it won't be read. Make sense?
 
What you need to do is read one sensor at a time, like this:
 
    digitalWrite(ultrasonic1TrigPin, HIGH);
    delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
    digitalWrite(ultrasonic1TrigPin, LOW);
    ultrasonic1Duration = pulseIn(ultrasonic1EchoPin, HIGH);
    ultrasonic1Distance = (ultrasonic1Duration/2)/29;
            
    digitalWrite(ultrasonic2TrigPin, HIGH);
    delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
    digitalWrite(ultrasonic2TrigPin, LOW);
    ultrasonic2Duration = pulseIn(ultrasonic2EchoPin, HIGH);
    ultrasonic2Distance = (ultrasonic2Duration/2)/29;
    
    digitalWrite(ultrasonic3TrigPin, HIGH);
    delayMicroseconds(10);                  // must keep the trig pin high for at least 10us
    digitalWrite(ultrasonic3TrigPin, LOW);
    ultrasonic3Duration = pulseIn(ultrasonic3EchoPin, HIGH);
    ultrasonic3Distance = (ultrasonic3Duration/2)/29;
I'm pretty sure that might be your problem, or at the very least part of it! Try making those changes and see what happens. If it still doesn't work, post your revised code and I'll try it out.
 
Good luck!

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users