Show Posts
Pages: 1 ... 50 51 [52]
766  Forum 2005-2010 (read only) / Syntax & Programs / Re: Urgent - Split Long into 2 Int using BitShift on: January 20, 2011, 11:28:21 am
Just use:

HighINT = ValueLong >> 16;
LowINT  = ValueLong;

And be done with it.  The masking is unnecessary.  The results won't match your other method, but that is because, as AWOL has correctly pointed out, your other method is incorrect.
767  Forum 2005-2010 (read only) / Syntax & Programs / Re: -output value to +output value? on: January 20, 2011, 04:16:56 pm
What you have is almost what you need.  Just needs two tweaks...

change +output to -output in your second analogWrite.

One of your if statements should handle the case where outputValue = 0.  Either use >= in the first line, or <= in the second line.
768  Forum 2005-2010 (read only) / Syntax & Programs / Re: Troubles with 4 servos and 2 dc motors on: January 11, 2011, 10:41:46 am
You shouldn't be using separate serial reads in your code.  Add your motor control code into the first serial read switch/case.

Code:
if ( Serial.available()) {
    char ch = Serial.read();

    switch(ch) {
        case '0'...'9':
           switch(readState) {
             case STATE_NONE:
               break;
             case STATE_PINNUMBER:
               pinNumber = pinNumber * 10 + (ch -'0');
               break;
             case STATE_POSITION:
               position = position * 10 + (ch -'0');
               break;
           }
           break;
        case 'g':
           servos[pinNumber].write(position);
           pinNumber = 0;
           position = 0;
           readState = STATE_NONE;
           Serial.print("p");
           Serial.print(pinNumber);
           Serial.print("m");
           Serial.print(position, DEC);
           Serial.print("OK");
           break;
        case 'p':
           readState = STATE_PINNUMBER;
           break;
        case 'm':
           readState = STATE_POSITION;
           break;
        case 's':
            motor3.run(FORWARD);
            motor4.run(FORWARD);
            delay(17);
            motor3.run(RELEASE);
            motor4.run(RELEASE);
            break;
        case 'w':
            //motor control code
            break;
        case 'a':
            //motor control code
            break;
        case 'd':
            //motor control code
            break;
    }
 }
769  Forum 2005-2010 (read only) / Syntax & Programs / Re: Calculating distance between two points accurately on: January 07, 2011, 01:03:48 pm
Quote
If I have a latitude (as a string) of say 12345678, I can convert this to a float or double such as 123.45678.

This may be part of your accuracy problem (if it's not just a trivial example without proper conversion).

In your first post you say you're getting a string as  DDDMMmmm, which is degrees, minutes, and decimal fractions of a minute.  Based on this format, 12345678 does NOT equal 123.45678 degrees.

It is 123 degrees and 45.678 minutes.

45.678 minutes = 45.678/60 degrees, or 0.7613 degrees.

So 12345678 would then equal 123.7613 degrees.  If your actual calculations are indeed making the proper conversion, then ignore this post.
770  Forum 2005-2010 (read only) / Interfacing / Re: No Interrupt Tachometer on: January 23, 2011, 04:15:21 pm
Why do you believe an interrupt would greatly disrupt your other functions?  As long as the ISR is kept short and fast, there should be no discernible interruption to your other routines.
771  Forum 2005-2010 (read only) / Interfacing / Re: Satellite control? on: January 20, 2011, 04:22:42 pm
Possible:  Yes.
Practical for a hobby RC car:  Not even remotely.
772  Forum 2005-2010 (read only) / Interfacing / Re: What do I need to do a Robot with wheels, ... on: January 20, 2011, 04:08:24 pm
One easy avenue available is to get 4 continuous rotation servos and some wheels that can be mounted to them.  For example, four of these:
http://www.robotshop.ca/roboblock-continuous-rotation-servo-wheel.html
They can be driven (though not powered) directly from an Arduino, and they can usually be powered from the same power source as the Arduino.

If you go with 4 separate motors for drive power, then you are also going to have to get a motor controller that is capable of driving those 4 motors.  It'll be more expensive and more complicated to set up.  something like:
http://www.robotshop.ca/solutions-cubed-ezr-5.html
http://www.robotshop.ca/solutions-cubed-easy-roller-robot-wheel-ezr2-1.html
could be used, but require a motor controller to drive them, something like:
http://www.robotshop.ca/Sabertooth-2x5-en-5.html

That latter setup would have a higher payload capacity and higher top speed than the servo setup, but also a higher cost and higher learning curve.  Being a novice, I'd recommend the servo route, and upgrade when you outgrow them (also, continuous rotation servos have other uses within hobby robotics, so you would likely be able to repurpose them if/when you outgrow them, as opposed to just tossing them aside)

As another option, either setup can also be reduced to a two-wheel arrangement with a caster wheel to reduce the costs by 30-50%.
773  Forum 2005-2010 (read only) / Interfacing / Re: What do I need to do a Robot with wheels, ... on: January 20, 2011, 03:33:49 pm
Do you want to piecemeal your own custom 4 wheel base?  If so, that's going to mean custom fabricating your own chassis from scratch.  If that's what you want, I can easily given you a list of required components to build a fully functional 4 wheel base (excluding the custom chassis to bolt everything to).

I'd personally recommend just buying a prefab kit.  Which kit is most suitable to you will depend on a variety of factors.  What kind of payload capacity are you looking for?  What kind of environments do you want it to operate in?  What is your budget?
774  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Stepper motor using Arduino on: January 20, 2011, 04:29:36 pm
How much torque do you need?
How slow is slow?
How accurate does your slow winding need to be?
775  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: How fast does arduino process each line of code? on: January 20, 2011, 09:04:57 am
Quote
Also say you have the most simplest photo flash type code of all, no delays, just a simple "when input #1 is high, make output #1 high".  What sort of timeframe does it take for it to actually register the input and produce the output?  Is it within the range of mere nanoseconds, or microseconds, or what?

Microseconds.  You could probably get it down to the nanosecond range if you use an interrupt.  Not really sure exactly how quickly the avr responds to an interrupt.  Never had the need to time something anywhere near that fast.
776  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: How fast does arduino process each line of code? on: January 19, 2011, 03:11:28 pm
It can often be very beneficial to implement a state machine with a high response 'run' mode and separate isolated setup mode.

It doesn't have to be a toggle switch though.  Could be a serial command.  A momentary pushbutton that that toggles between 'run' and setup, or just automatically drops back into 'run' mode if there is some period of inactivity (user enters setup, but doesn't do anything for 10 second, the unit switches back to 'run' mode).  It could offer a window of time on initial bootup to enter setup (say 2-5 seconds), after which it enters runmode with no option to go to setup other than resetting the unit.  Different methods will be suited to different projects.  
777  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: How fast does arduino process each line of code? on: January 19, 2011, 02:47:34 pm
@AWOL:  You are entirely correct, and that was the primary reason I used it as an example of how the execution time for lines of code can and will vary so wildly.
778  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: How fast does arduino process each line of code? on: January 19, 2011, 11:04:44 am
If the timing of a response to an input trigger is absolutely critical, use an interrupt.  That's exactly why interrupts exist.

That being said, it is possible to know how long a line or lines of code in your program will take to execute, but it is far from easy.  You need an understanding of assembly and machine language, and an understanding of how your C code is assembled into machine language to do so.

Something like:
Code:
byte test = 10;
if(test < 10)
    test = 20;
may only take a handful of instructions to execute (a dozen or so, for example)

where as something like:
Code:
int angle = 0;
while(angle < 180)
    Serial.println(sin(angle++/57.295));
can literally take millions of instructions to execute.

Even though each is only 3 lines of code (two if you don't count the variable definition/initialization), they compile to drastically different machine code binaries.  The first is just some move instructions, conditional jump, and assignments.  The second is a whole other ballgame with some advanced math and blocking IO manipulation.



There is, however, an easier way.  Simply benchmark your code.  Wrap it in some timing code and see how long it takes to execute.

Code:
  unsigned int time = 0;
   time = micros();
  
   byte test = 10;
   if(test < 20)
     test = 20;
  
   time = micros() - time;
  
   Serial.println(time, DEC);
   delay(1000);

On my Atmega168 Arduino, I get 0 or 4 microseconds.  In reality it's probably a microsecond or two, but the resolution of the micros() function is 4 microseconds.

The other example though:
 
Code:
  unsigned int time = 0;
  time = micros();
  
  int angle = 0;
  while(angle < 180)
    Serial.println(sin(angle++/57.295));

  
  time = micros() - time;
  
  Serial.println(time, DEC);
  delay(1000);

takes about 38900 microseconds (or about 39 milliseconds).  So only about 600,000 instructions to execute, not quite millions, but still about 4 orders of magnitude longer to execute.  It also took only a couple minutes to determine empirically, and requires no knowledge at all of assembly/machine language or compilers.
779  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: second input isn't read by arduino on: January 19, 2011, 03:02:43 pm
Your last if/else if statement has identical conditions in the if and else if clauses.  For this reason the else if block of code will never be executed.

You also have a couple of "while (pinX, HIGH)" lines of code as well.  What is the intent of these while statements?  The comma operator really has little use within a while clause (and none at all within the code you're using it in that I can see).
780  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: LoL Shield SNAKE Game on: January 13, 2011, 02:04:26 pm
Another method of implementing a non-blocking delay (but requires the rest of the code/framework to be modified to work appropriately) would be something like:

Code:
//delay 20 milliseconds
static int delaytime = millis() + 20;
if(millis() > delaytime){
   delaytime += 20;
   //dostuff() will get called every 20 milliseconds (50hz).
   dostuff();
}

Stuff to be aware of:
This code can be problematic if dostuff() takes longer than 20ms to execute, or if all the code outside this loop takes longer than 20ms to execute.

You can't just replace your delays with this code.  The rest of your code just won't work properly if you do.  You need a framework tailored to work properly.  At a high lvl, it should look something like this:

Code:
void Loop(){
    ReadInput();
    static int delaytime = millis() + 200;
    if(millis() > delaytime){
       delaytime += 200;
       SnakeMove();
    }  
    SetOutput();

}

void ReadInput(){
    //Input code here
    
}

void SnakeMove(){
    //Calculate new snake position at regular intervals
    
}

void SetOutput(){
    //Set LEDs here
    
}

You should also have a state machine to handle the various game states, eg. "PRESS RESET" after the snake dies.
Pages: 1 ... 50 51 [52]