Go Down

Topic: Help needed with controlling a robot using obstacle avoiding sensors. (Read 3606 times) previous topic - next topic

Martend


seeleft is a pointer, and because it is non zero, will always be true.


allrighty... so if I understand you correctly, no matter if the pointer is high or low, it is always present so a "true" factor inside a sketch.

So what i need to do is to create a function who checks/pulls data from the "pointer" at timed intervals?
(In this way I use a pointer to guide me to the place where the changes are happening)

or

Do I create a function for the sensor? Like when the pointers are low, the robot can move forward, if one is high change direction?
(In this way the pointers, point out to the sketch there is a changed state of one sensor)

Hmm I hope I still make sense :-) Maybe I need a little break...
Thanks for the helping hand

M

mromani

Totally wrong code:

Quote
void see_NOleft () {
   digitalRead(sensorPinLO == LOW); // no obstacle on the left side
}

void see_NOright () {
   digitalRead(sensorPinRO == LOW); // no obstacle on the right side
}


You have to understand the difference between a function that performs some action and one that should return a value.

What you want is this instead:

Quote
boolean see_NOleft() {
   return digitalRead(sensorPinLO == LOW); // no obstacle on the left side
}

boolean see_NOright() {
   return digitalRead(sensorPinRO == LOW); // no obstacle on the right side
}


Then you use them like this (just an example, of course):

Code: [Select]

if (see_NOright()) {
   // code to be executed when there's no obstacle on the right side
}

if (see_NOleft()) {
   // code to be executed when there's no obstacle on the left side
}


HTH

mromani

Forget what was said about pointers. It was totally correct, but function pointers are far too complex a subject for you to understand now. Just learn to write functions and how to properly call them. I suggest you visit the Learning section of this website, as you seem to be missing a large part of basic C knowledge.
I mean, you're showing the right will to learn IMHO, but before attempting to write a moderately complex piece of code like the one you envisioned before you have to at least know the ABC of the language, otherwise issues about how to code a particular algorithm will mix up with lower level syntactical problems.

That said, feel free to ask any further question. I'll try to be helpful nonetheless (provided I have time, of course :-)

JasonK

This isn't going to work either because you are doing a logical compare and sending it as the variable to digitalRead

Quote
boolean see_NOleft() {
   return digitalRead(sensorPinLO == LOW); // no obstacle on the left side
}

boolean see_NOright() {
   return digitalRead(sensorPinRO == LOW); // no obstacle on the right side
}



This is the format you are looking for however you may need to swap the true and false depending on your sensor output.

Code: [Select]

/*
This function tests the left sensor PIN and returns TRUE if the sensor PIN is LOW and FALSE if the sensor PIN is HIGH
This function is expecting a sensor that give a high value when it detects something in range and a low value when it does not detect something in range
*/

boolean see_NOleft(){
   boolean clear;
      if(digitalRead(sensorPinLO)  == LOW) {
         clear = TRUE;
      } else {
         clear = FALSE;
     }
   return clear;
}

mromani

Sorry for my wrong code :smiley-sad-blue: :smiley-sad-blue: :smiley-sad-blue:

I'll take this as a hint to write less and (re)read more ;-P

Martend

thanks, AWOL, JasonK & mromani for your help.
With a happy feelling I'll return to the learning sections and start with learning about functions.

I didn't expect to write an awesome-robot-sketch at once, but with starting to try it, I learn as it goes. I was following tutorials at random,
but I could not grasp the bigger picture, how I could use the things learned in a real practical project.
I did make stuff like the DIY Ambilight, a 4x4x4 led cube, reading IR output from a remote control, making a knock sensor, play sounds etc.
So with starting my robot project, I think I better understand what I'm learning and can use it at once.
I also have the feelling that making a robot move around correctly is the hardest part to learn and also the key component to learn, things
like headlights, sound-sensor, camera-control etc I can add later, first thing to do is to learn the little car to move :-)
Thanks for the help again, I'll be back with a better sketch! (I hope :-) )

M

cyclegadget


  There is more than one way to use analogRead but, here is my preferred way more often than not.
I like to store my pin reads into a variable. Then, I can call on the variable anytime in the loop to make true or false cases.
Here is some quickly written code to explain a usage.

Code: [Select]
        int LOsensor = LOW;  // define variables to hold digitalRead values
        int ROsensor = LOW;

LOsensor = digitalRead(sensorPinLO); // take the readings in the loop
ROsensor = digitalRead(sensorPinRO);

if (LOsensor && ROsensor == LOW) // compare the values and make a decision
{
do this
}
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

dxw00d

Code: [Select]
if (LOsensor && ROsensor == LOW)

So that's If (LOsensor evaluates to True) and (ROsensor is equal to LOW)?

mromani

Semms I'm not the only one mixing digital pin status and boolean values...  :smiley-mr-green:

cyclegadget


Semms I'm not the only one mixing digital pin status and boolean values...  :smiley-mr-green:



:smiley-red: Yep there is the novice of me showing. I should have used :
Code: [Select]
boolean LOsensor = LOW;
        boolean ROsensor = LOW;


Quote
Insert Quote
Code:
if (LOsensor && ROsensor == LOW)

So that's If (LOsensor evaluates to True) and (ROsensor is equal to LOW)?


This means if LOsensor and ROsensor both equal LOW then, we do the action.

If LOsensor or ROsensor is High, we do not do the action.

Also note my mistake, I should have defined the variables as boolean. Sorry about that.

http://arduino.cc/en/Reference/Boolean
Good links: Eagle tutorial= http://www.youtube.com/playlist?list=PLDE1858BD83D19C70
General Arduion tutorials = http://tronixstuff.wordpress.com
http://www.gammon.com.au/forum/bbshowpost.php?bbtopic_id=123

Martend

Arduino programming class on steroids,
even a mistake turns to be helpful  :smiley-sweat:

Thanks for the input and will add boolean to my homework.

mromani

Once and for all, let's clarify this digitalRead vs. boolean thing, as it's getting really messy (hoping I don't do any more silly mistakes :-) )

http://arduino.cc/en/Reference/DigitalRead

digitalRead() return value should be stored into an int type variable, as shown in the example on the reference page above

boolean values/variables should not be compared to anything else than boolean values/variables.

Quote

if (LOsensor && ROsensor == LOW)

If LOsensor or ROsensor is High, we do not do the action.


You can't use the && operator to "extend" a comparison to two variables. It's just a logical "and" betweeen two boolean expressions. Here you have an implicit comparison to zero (LOsensor alone means "LOsensor is non-zero" IIRC) and an explicit one.

Code: [Select]

int LOsensor;
int ROsensor;

LOsensor = digitalRead(loSensorPin);
ROsensor = digitalRead(roSensorPin);

if ((LOsensor == LOW) && (ROsensor == LOW)) {
   // action taken if both pins are low
}



Quote

boolean LOsensor = LOW;
boolean ROsensor = LOW;


- LOW is not a boolean value. Boolean variables are either true or false;
- you should have declared them int;
- there's no point in initializing a variable if you're going to set it via value = digitalRead(pin) anyway.

dxw00d

Quote
This means if LOsensor and ROsensor both equal LOW then, we do the action.


It most certainly doesn't mean that. It means what I said.

Martend

With all the nice and helpfull replies in mind and after following a few tutorials I came up with these few steps:

Code: [Select]


#include <legopowerfunctions.h> // Lego PF Library

LEGOPowerFunctions lego(13); // Pin for Lego PF IR Output

//configure the obstacle avoiding sensor
int sensorPinEN = 7;    // Common pin for module
int sensorPinLO = 6;    // Left Object detected output pin
int sensorPinRO = 5;    // Right Object detected output pin   
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {

  Serial.begin(9600); 

  // Set the desired pin modes
  pinMode(sensorPinEN, OUTPUT);
  pinMode(sensorPinLO, INPUT);
  pinMode(sensorPinRO, INPUT);

  // ready the sensor
  digitalWrite(sensorPinEN, HIGH);

}

void loop() {

  lego.ComboPWM(PWM_REV2, PWM_FWD2, CH4);

  if (digitalRead(sensorPinLO) == HIGH)
    lego.ComboPWM(PWM_FWD4, PWM_FWD4, CH4);

  if (digitalRead(sensorPinRO) == HIGH)
    lego.ComboPWM(PWM_REV4, PWM_REV4, CH4);

}



For the moment I stayed away from boleans etc. I tried to keep it simple for my first run.
After the sketch did verify , I uploaded it and did turn the robot on.
I was very, very pleased with the result, the robot moved nicely in a forward direction and
avoided several objects. Yay! :-)

Ofcourse the sketch was not complete and the robot faced some movement problems, cause of the
lack of code that is missing at this point.
So my next step would be to improve the code.

For my understanding, would I add code to the IF left high and IF right high statements? Or write a new seperate function?
Like:

Code: [Select]


if (digitalRead(sensorPinLO) == HIGH ( < 5000)   <---new timer function less than x seconds
    lego.ComboPWM(PWM_FWD4, PWM_FWD4, CH4);
     if (digitalRead(sensorPinLO) == HIGH ( > 5000) <---new timer function more than x seconds
        lego.ComboPWM(PWM_FWD7, PWM_FWD7, CH4);



I also did try to find some tuts about moving robots around, but many tutorials include the use of a ultrasonic sensor and
ofcourse the code is accordenly for that kind of sensor and not the one I use. Maybe someone knows a tutorial?
My Sensor: http://www.elechouse.com/elechouse/index.php?main_page=product_info&cPath=152_156&products_id=589

Also if my choice of hardware was not the right one to start with, I would love to hear that also :-) When I did purchase this sensor
I did not know there was an ultrasonic one too. Thanks again for the help, I really enjoyed it when the robot moved like intended
for the first time.

M


mromani

You see ? Simple, clean and syntactically correct code, and in "magically" works! :-)

Code: [Select]
if (digitalRead(sensorPinLO) == HIGH ( < 5000)   <---new timer function less than x seconds
    lego.ComboPWM(PWM_FWD4, PWM_FWD4, CH4);
     if (digitalRead(sensorPinLO) == HIGH ( > 5000) <---new timer function more than x seconds
        lego.ComboPWM(PWM_FWD7, PWM_FWD7, CH4);


Nonono! C'mon, you're on the right track, but you need to keep studying the basis of the C syntax. Also, don't just put new things "randomly" together and ask if it would work. Do a little more research in Reference and Learning pages. I suggest you try to understand the "blink without delay" example.

HTH

Go Up