Show Posts
Pages: [1] 2 3 ... 302
1  Using Arduino / Programming Questions / Re: Where am I going wrong with the no delay method? on: April 18, 2014, 07:03:41 pm
Nick Gammon's explanation includes blocking code and how to avoid it, with example code:
http://www.gammon.com.au/forum/?id=11411

Quote
This question comes up practically every day on the Arduino forum - "how do I blink two LEDs at different rates?" or "how do I turn on and off two motors at different times?".

Quote
Let's look at an analogy. Say you want to cook breakfast. You need to cook:


    Coffee - takes 1 minute
    Bacon - takes 2 minutes
    Eggs - takes 3 minutes




Now a seasoned cook would NOT do this:


    Put coffee on. Stare at watch until 1 minute has elapsed. Pour coffee.
    Cook bacon. Stare at watch until 2 minutes have elapsed. Serve bacon.
    Fry eggs. Stare at watch until 3 minutes have elapsed. Serve eggs.



The flaw in this is that whichever way you do it, something is going to be cooked too early (and get cold).

In computer terminology this is blocking. That is, you don't do anything else until the one task at hand is over.

What you are likely to do is this:


    Start frying eggs. Look at watch and note the time.
    Glance at watch from time to time. When one minute is up then ...
    Start cooking bacon. Look at watch and note the time.
    Glance at watch from time to time. When another minute is up then ...
    Put coffee on. Look at watch and note the time.
    When 3 minutes are up, everything is cooked. Serve it all up.



In computer terminology this is non-blocking. That is, keep doing other things while you wait for time to be up.

2  Using Arduino / Programming Questions / Re: Calculating slope using array issues on: April 18, 2014, 06:56:21 pm
And when the shaft stops turning and delta-displacement is zero your numbers go to?
3  Using Arduino / Programming Questions / Re: Sketch on Mini w/328 only runs once on: April 18, 2014, 06:52:44 pm
Too bad.

Are you sure the sketch copies?
4  Using Arduino / Project Guidance / Re: Inconsistent values with Bluetooth and Serial.Read on: April 18, 2014, 06:37:44 pm
They use some kind of packet system with its own ECC?
5  Using Arduino / Programming Questions / Re: Where am I going wrong with the no delay method? on: April 18, 2014, 11:10:32 am
in your void flash, instead of
Code:
interval = analogRead(0)/10; //read pot value, divide by 10 to reduce range of flash sweep speed
  if (interval <5) // delays of less that 5 create too fast of a flash sweep. Set to minimum of 5
  {
    interval = 5;
  }
you could maybe map the value, like this you can easily control it's min and max values. No need for that if statement.

You do know that min and max use the same basic code if not more?
6  Using Arduino / Programming Questions / Re: Calculating slope using array issues on: April 18, 2014, 11:07:36 am
I should have taken more time on that.
7  Using Arduino / Programming Questions / Re: Sketch on Mini w/328 only runs once on: April 18, 2014, 11:05:09 am
Quote
You do know that the Mini may have some differences as to pin numbers (and is there a led?) from say, the UNO?
No it doesn't. It's the same 328P chip. Only difference is that some promini's support A6/A as analog inputs.


My UNO board has a pin map just to tell board pins from 328 pins.

Does the Mini have the built-in led and resistor on pin 13?
8  Using Arduino / Programming Questions / Re: Where am I going wrong with the no delay method? on: April 18, 2014, 11:01:19 am
If you use unsigned integers then the only accounting for rollover is that you have a maximum longest interval regardless of when it starts.

Unsigned integer rollover requires no difference in code when you handle it properly in the first place.

if ( Unsigned_Now - Unsigned_Start >= Unsigned_Interval ) {}; // ALWAYS WORKS REGARDLESS OF ROLLOVER

The only times you need "rollover code" is when you don't do the unsigned subtraction or you don't use unsigned math (or manage to klutz it up in some more convoluted way).
9  Using Arduino / Programming Questions / Re: Sketch on Mini w/328 only runs once on: April 18, 2014, 10:52:27 am
You do know that the Mini may have some differences as to pin numbers (and is there a led?) from say, the UNO?
http://arduino.cc/en/Main/ArduinoBoardMini

If you can't get this sketch to run it may be due to those very differences:
Code:
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);   
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

There could be power issues for all I know.
10  Using Arduino / Programming Questions / Re: Where am I going wrong with the no delay method? on: April 18, 2014, 10:41:29 am
Perhaps you would be better served by getting help understanding BlinkWithoutDelay first...
because it's pretty clear that you don't.
11  Using Arduino / Programming Questions / Re: Calculating slope using array issues on: April 18, 2014, 10:23:57 am
Really good coding practices don't leave the possibility of a crash.

Murphy was an optimist.
12  Using Arduino / Programming Questions / Re: How to grab responses from Serial Terminal? on: April 18, 2014, 09:16:12 am
If you hadn't posted here then I wouldn't even know about these boards yet.
13  Using Arduino / Programming Questions / Re: How Can I Serial print a sensor value each minute with out delay. on: April 18, 2014, 09:14:14 am
And to help you with future coding, maybe check out all the sections in the link below that have Operator in the name.
http://arduino.cc/en/Reference/HomePage

There's patterns to them as well as differences but they're all very useful.
14  Using Arduino / Programming Questions / Re: Calculating slope using array issues on: April 18, 2014, 09:02:42 am
To fix this, you can cast either the numerator or the denominator of the fraction as float, like this:
Code:
 float slope = (float)(readings[1]-readings[0])/(readings[3]-readings[2]);
A purist might cast both as float.

Excellent guys thank you very much for the help, it's now giving me the accuracy I was looking for. Appreciate the other feedback too. The serial print is showing the correct number but if readings are equal to zero then 'nan' or 'inf' are printed, I take it this is due to me not dealing with the issue of zero value reading as you guys mentioned. I've tried to figure it out but I'm not having much joy so maybe one of you guys could help with this?

Thanks

That is going to give you a float of the finished integer operation inside the parens.
1/5 would give 0.0 not 0.2.
You need to divide float by float not int by int.

As noted above, you need to check for divide by 0 and perhaps return a very large value for slope.
15  Using Arduino / Programming Questions / Re: How Can I Serial print a sensor value each minute with out delay. on: April 18, 2014, 12:29:12 am
Code:
 if(currentMillis - previousMillis == interval) {

This is an error waiting to happen. Maybe not in this code on this Arduino but keep using that and you'll see.

What error? Well, what happens if the exact milli where that is true is missed for any reason? That error.
Pages: [1] 2 3 ... 302