Show Posts
Pages: [1] 2 3 ... 8
1  Forum 2005-2010 (read only) / Bugs & Suggestions / Re: Open multiple sketches for editing on: February 01, 2008, 11:18:28 am
Thanks.  I finally dug through the files Arduino creates when it saves/compiles a sketch, and found that I can open the .pde file with a text editor.  This is all I really need - to be able to look through old code while writing a new sketch.
2  Forum 2005-2010 (read only) / Bugs & Suggestions / Re: Open multiple sketches for editing on: January 31, 2008, 07:40:28 pm
Pardon my ignorance, but is it safe running two open windows of Arduino?  That would really relieve my problem;  I didn't realize that tabs were already implemented for multi-file sketches (not having written any).
3  Forum 2005-2010 (read only) / Bugs & Suggestions / Re: Open multiple sketches for editing on: January 28, 2008, 04:20:32 pm
...of course, moments after I make the post I find earlier requests for this feature.  Nevermind.
4  Forum 2005-2010 (read only) / Bugs & Suggestions / Open multiple sketches for editing on: January 28, 2008, 04:18:00 pm
A suggestion I would like to make for the IDE - it would be nice to open another sketch in another tab, to make the cutting and pasting of code from sketch to sketch easier.  Has this been considered?
5  Forum 2005-2010 (read only) / Syntax & Programs / Re: Robot freaking out about new code??? on: March 26, 2008, 10:40:07 pm
Here is an example of nested ifs (you can see by the indentation what is meant by "nested")

Code:
 if(usValue > closeUp);
  {
    if(usValue < closeUp)
    {
      stopboth();
      delay(500);
      turnright();
      delay(500);
      usValue = scanfront();
      if(usValue < closeUp)
      {
        usValue = scanback();
        if(usValue > closeUp)
        {
          turnleft();
          delay(500);
          turnleft();
          delay(500);
          usValue = scanfront();
        }
        else
        {
          turnright();
        }
      }
      rampup();
    }
  }  

Trying to code a robot's behavior this way gets complicated fast.  I'm reading up on behavior-based programming now, but I haven't written anything yet.
6  Forum 2005-2010 (read only) / Syntax & Programs / Re: Robot freaking out about new code??? on: March 26, 2008, 02:05:47 am
I would recommend putting your robot up on "jackstands" (I use a piece of wood); connecting the serial cable, running the bot, and adding debug lines to your code to send the values your sensors are reading out to the serial monitor.  This is how I dial in my sensors - and you can debug your logic at the same time.

You can get the sort of interplay between sensors that you describe just by how your wiring is done.  When you run cables to sensors, try to use shielded cable, with a connection from the shield (one end only) to your system ground (the ground your circuits, including arduino, should be sharing).  I have really reduced my encoder signal noise this way, and the conditioned signal that your ultrasonic is using may not be very noise tolerant either (more shielded cable).

Assuming you aren't doing this already.
7  Forum 2005-2010 (read only) / Syntax & Programs / Re: Robot freaking out about new code??? on: March 25, 2008, 10:26:40 am
A few more questions - what kind of ultrasonic sensor are you using?  I use Pings and Devantech RF04s, but to use them I have to incorporate timing into my code.  I'm not seeing that in your code; you might want to read this thread for hints on ultrasonic sensor reading - http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1167315558/9

I haven't used IR sensors yet (although I recently acquired a handful) but my understanding is that they can be read very simply, just an analogRead() from the analog input pin they are connected to.  Not true for ultrasonics!
8  Forum 2005-2010 (read only) / Syntax & Programs / Re: Robot freaking out about new code??? on: March 25, 2008, 01:58:54 am
Have you put some serial prints into your code to test the sensor values you're getting?  What kind of whack false readings?

FYI - you can get away with using two fewer pins by tying the h-bridge enables to 5V.  Usually.
9  Forum 2005-2010 (read only) / Syntax & Programs / Re: how to enhance this code? on: March 24, 2008, 12:44:28 pm
The first thing I would recommend is putting your "moves" into functions, like this:

void moveForward()
{
    digitalWrite(12, HIGH);  
    digitalWrite(9, HIGH);  
    digitalWrite(11, LOW);  
    digitalWrite(10, HIGH);  
    digitalWrite(8, HIGH);  
    digitalWrite(7, LOW);
}

Now you can construct logic to control your bot, using conditional statements in the loop() function:

void loop()
{
    if (bumper == 0)
    {
        moveForward();
        delay(200);
    }
    else
    {
        moveBackward();
        delay(200);
    }
}

A more effective way to remove time-dependent outputs from your main loop might be to relocate your control logic to a "state machine" function:

void loop()
{
   sensorPoll();           // a function to poll sensors and smooth data if required
   outputChange();     // this is where your State Machine lives, based on sensor data it determines whether you need to update
                                // motor outputs
   if (changeFlag == TRUE)
   {
      outputAdjust();
   }
}
  

...now your PWM output levels are being adjusted only when needed (by outputAdjust(), based on input from outputChange() )

You have to think about what the robot does a bit differently to do this; instead of asking "What does the robot do next?" think "What state changes next?"
10  Forum 2005-2010 (read only) / Syntax & Programs / Re: making my code simpler on: March 21, 2008, 06:10:11 pm
quick and dirty...

When you create the function you specify the data type that the function will return.  For functions that don't return a value, the type is "void" (you will see a lot of this).  If you want actual data returned, for example an integer, you would write a function that looks like this (to steal from one of my favorite webcomics):

int randomValue()
{
  int r = 4;  //chosen by dice roll, guaranteed random
  return r;
}

You call this function in your code like this:

int val = randomValue();

Now when you call this function, the value of r (a strictly local variable, only the function that creates it gets to use it) gets passed to val, a variable with more scope (since it is created in the main loop of the code, it can be seen globally).

If you want to pass a value to the function, you need to add an "argument" to the empty parentheses after the function name when you write it:

int randomValue(int a)
{
  int r = 4;  //chosen by dice roll, guaranteed random
  r = r + a;
  r = r / 2;
  return r;
}

You call THIS version like this:

int val = 8;
int newVal = randomValue(val);

...and voila, randomValue() will take the 8 you sent it, add 4, divide by 2, and return your processed value to the variable newVal.

For a little more detail on functions, try this link http://www2.its.strath.ac.uk/courses/c/section3_9.html
For a lot more detail on functions, read The C Programming Language by Ritchie and Kernighan (sp?).  Functions are the essential building blocks for structuring C code, so practice, practice, practice!
11  Forum 2005-2010 (read only) / Syntax & Programs / Re: making my code simpler on: March 15, 2008, 03:48:57 pm
A good first step might be to organize your code into functions, then call the functions from setup() or loop().  This lets you organize simple code without dealing with the complexities of structs or classes (yet).
Have you used functions in your code yet?  Or do you need an example to see what I mean?

Oops - scrolling down farther in your code I see that you are familiar with this  :-[
12  Forum 2005-2010 (read only) / Syntax & Programs / Re: C or Embedded C on: March 15, 2008, 04:02:04 pm
I can recommend C++ Primer Plus - it was the textbook for a programming class I took last year.  And for straight C the concensus pick is the second edition C Programming Language by Kernighan and Ritchie (the ANSI K&R) which is a model text for clarity of language and presentation.
13  Forum 2005-2010 (read only) / Syntax & Programs / Re: pulseIn without pausing the program... on: February 02, 2008, 02:17:26 pm
Thanks for posting that link, mem.  I'm going to use the Qdata code from that thread to try to smooth noisy ultrasonic sensor and encoder data.
14  Forum 2005-2010 (read only) / Syntax & Programs / Re: Function declared as unsigned long - no compil on: February 01, 2008, 11:10:00 am
That makes sense.
Now that my bot is "hardware complete", it's time to concentrate on code.  I brought my K&R to work for lunchtime reading.  Those programming classes I took seem long ago and far away!
15  Forum 2005-2010 (read only) / Syntax & Programs / Re: Function declared as unsigned long - no compil on: February 01, 2008, 12:56:30 am
I should add (having just discovered) that the comments in caps must be removed.  I added them in the forum post editor - so when I double checked my posted code NOTHING compiled!
Pages: [1] 2 3 ... 8