Show Posts
Pages: 1 [2] 3 4 ... 14
16  Using Arduino / Project Guidance / Re: Counter for measuring distances (newbee) on: December 30, 2013, 05:14:51 am
odometer. thanks for that code. but it doesnt work well.
it prints out "random" numbers directly when i push the reed
like this ex:

0,1
0,1
1,2
etc...

best regards Daniel
Sorry, I noticed a typo in my code, which I fixed. (I had written lastpushButtonState instead of lastPushButtonState.)

I am inserting something called "debouncing". This is because sometimes when you press a switch once, it will "bounce" and will register more than once. I am using some small delays to take care of this.
17  Using Arduino / Programming Questions / Re: Big Numbers in float on: December 29, 2013, 11:34:40 pm
I did a bit of messing around with Print.cpp, and here is my version.
I hope this speeds things up a bit, as well as fixing a bug in some edge cases.
18  Using Arduino / Project Guidance / Re: Counter for measuring distances (newbee) on: December 29, 2013, 06:12:37 pm
The bicycle computer is a lot more complicated than what Uppfinnarwannabee is asking for. It might also be difficult for him to adapt to his needs.

Also, just by looking at it, I found one or two bugs in the bicycle computer code. (Example: decimal comma used in place of decimal point. Sorry, but when you're writing C++ or Arduino code, you have to be American and use a dot for the decimal point.)

I think what Uppfinnarwannabee wants is just a simple measurement of distance, not speed or anything else. (If I am mistaken, please correct me.)

Like I said, for distance, you need to know the size of the wheel. Specifically, you need to know how much distance corresponds to 1 turn of the wheel.

Once you find out the correct distance, you can try this code.
(Note: You will need to put in the correct number for wheelCircumference.)
(Note #2: I have edited this code to fix a typo, and again to fix another problem.)
Code:
const int  reedPin = 2;    // the pin that the reed-relay is attached to
const int buttonPin = 3;   // the pin that the push-button is attached to
const int ledPin = 13;       // the pin that the LED is attached to
const long wheelCircumference = 1000; // wheel circumference in millimeters

// Variables
long odometer = 0;   // the odometer (it counts in millimeters)
int reedState = 0;         // current state of the reed-relay
int lastReedState = 0;     // previous state of the reed-relay
int pushButtonState = 1;
int lastPushButtonState = 1;

void setup() {
  // initialize the reed pin as a input:
  pinMode(reedPin, INPUT);
  // initialize the button pin as a input
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  lastPushButtonState = pushButtonState;
  pushButtonState = digitalRead(buttonPin);
  if (pushButtonState != lastPushButtonState) {
    if (pushButtonState == LOW) {
      // output the odometer reading in meters
      long adjOdometer = odometer + 50; // round to 1 decimal place
      Serial.print(adjOdometer / 1000); // whole meters
      Serial.print(","); // decimal comma (you prefer decimal commas, right?)
      Serial.print((adjOdometer % 1000) / 100); // tenths of a meter
      Serial.println(); // start a new line
     
      // reset odometer to zero
      odometer = 0;
    }
    // debounce
    delay(10);
  }
 
 
  // read the reed input pin:
  reedState = digitalRead(reedPin);
  // compare the reedState to its previous state
  if (reedState != lastReedState) {
    // if the state has changed, maybe add some distance to the odometer
    if (reedState == HIGH) {
      // if the current state is HIGH then the button
      // went from off to on:
     
        // increase odometer reading
        odometer += wheelCircumference;
    }
    // debounce
    delay(10);
  }
  // save the current state as the last state,
  //for next time through the loop
  lastReedState = reedState;

 
  // turns on the LED every ten meters by
  // checking the modulo of the odometer reading.
  // the modulo function gives you the remainder of
  // the division of two numbers:
  if ((odometer > 0) && ((odometer % 10000) < wheelCircumference)) {
    digitalWrite(ledPin, HIGH);
  } else {
   digitalWrite(ledPin, LOW);
  }
 
}


19  Using Arduino / Project Guidance / Re: Counter for measuring distances (newbee) on: December 29, 2013, 07:43:22 am
im not sure how big it is but maybe 1 meter around it.
If you are going to use the wheel for measuring, you will need to know the size of the wheel.

Quote
but is there a way i can make it print out 15 after i push reed 15 times
and then push button, so i dont have to hold it down and get the extra (16)
from pushing reed?
You now have this in your code:
Code:
      pushButtonState = digitalRead(buttonPin);
  if (pushButtonState == HIGH)

Serial.print(reedCounter);
The problem is where you have it in your code.
Code:
  reedState = digitalRead(reedPin);
  // compare the reedState to its previous state
  if (reedState != lastReedState) {
    // if the state has changed, increment the counter
    if (reedState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:

      // Anything you have in here will only get done if the reed switch
      // went from "off" to "on".
      // Since you want to be able to output the value of reedCounter
      // without the reed switch being "on", you have to pull the relevant
      // code out from here and put it elsewhere.

    }
   
  }

Quote
and how can i reset it without have to push the button on unoboard?
(i think i maybe can manage using a extra reed for resetting)
but how do i code for reseting?
By "reset the counter", you probably mean "set the counter to zero". This is how you do that:
Code:
reedCounter = 0;
20  Using Arduino / Project Guidance / Re: Counter for measuring distances (newbee) on: December 29, 2013, 04:07:36 am
Use Serial.println() to start a new line after printing.

Code:
Serial.print(reedCounter);  // this prints the number without starting a new line

// If you want each number on its own line, use this:
Serial.println(reedCounter); // this prints each number on its own line

// This is another way of doing the same thing:
Serial.print(reedCounter); // this prints the number
Serial.println(); // this starts a new line

For what you're doing, I think you're going to want some kind of display that can actually show numbers, so that you can read the distance while you're using the wheel.

By the way, what size is the wheel?
21  Using Arduino / Programming Questions / Re: Why does variable revert to previous value? on: December 25, 2013, 06:05:27 pm

Eventually I decided, since our system is international, to restrict users more than normal rather than giving them the free-form entry I originally thought would be good. Now, that field works like this - you must enter at least three letters for the month, and you must enter a 4-digit year - everything else is rejected.


I would have used three fields: one each for year, month, and day, in that order.
If I had to use only one field, I would make it yyyy-mm-dd.
This way, the months do not depend on language.
(By the way, China and several other Asian countries do not use names for the months. They just number them, 1 to 12.)
22  Using Arduino / Project Guidance / Re: Add minutes to a GPS clock? on: December 25, 2013, 08:48:28 am
Just add 'x' to minutes and then do a check to see if you overflowed:-

If mins > 60, take away 60 and add 1 to hours
If hours > 24, take away 24 and add one to the day
Etc

Once you've done that then do whatever you need to do to display your clock.

If you only need time, no date, then it's pretty trivial to just check for overflowing minutes and hours.

What he said.

Except you need to check for >= 60, not just > 60.

It's really just like doing arithmetic using pencil and paper. If you know how you would calculate something using pencil and paper, you can generally have the Arduino do it the same way.
23  Using Arduino / Project Guidance / Re: LED Strip project for my son to be born Feb 2nd. on: December 18, 2013, 08:10:22 pm
I will end up buying 4 LED strips

Only 4? From your description, it sounds like you need 8.

I want to be able to program the lights from my computer in the other room.

Then how will you observe the child's reaction?

24  Using Arduino / Programming Questions / Re: Arbitrary precision (big number) library port for Arduino on: December 08, 2013, 03:17:27 pm
Code:
    for (i=0; i < (n1->n_len + n1->n_scale); i++)
    {
        tmp += n1->n_value[i];
        qval->n_value[i] = tmp / 2;
        tmp -= (qval->n_value[i] * 2);
        tmp *= 10;
    }
Try this:
Code:
    for (i=0; i < (n1->n_len + n1->n_scale); i++)
    {
        tmp += n1->n_value[i];
        qval->n_value[i] = tmp >> 1;
        tmp &= 1;
        tmp *= 10;
    }
25  Using Arduino / Programming Questions / Re: Arbitrary precision (big number) library port for Arduino on: December 08, 2013, 08:08:39 am
Time you start coding yourself smiley-wink

I tried (remember Numbler4?) but I'm no good with all this malloc / pointer nonsense.
Algorithms I can do. But if you give me low-level memory allocation stuff, I'm lost.
This is why I was able to speed up BigNumber division, but not modulus. Once I killed the Knuth hocus-pocus, I had a division routine which computed both quotient and remainder. But because I'm no good with the malloc and pointer stuff, I don't know how to get it to return the remainder.
26  Using Arduino / Programming Questions / Re: Arbitrary precision (big number) library port for Arduino on: December 07, 2013, 07:50:03 pm
@robtillaart: I would think that mul2 and div2 would be more useful to have around.

Also, short division would be very useful in general.
See here: http://en.wikipedia.org/wiki/Short_division
27  Using Arduino / Programming Questions / Re: Arbitrary precision (big number) library port for Arduino on: December 07, 2013, 03:44:28 pm
@Nick Gammon:
The Arduino (Uno, at least) sucks at division, plain and simple.
Killing division speeds things up greatly.
Really, how hard is it to replace something like this:
Code:
 while (size-- > 0)
   {
     value = *nptr-- * digit + carry;
     *rptr-- = value % BASE;
     carry = value / BASE;
   }
with something like this?
Code:
   while (size-- > 0)
      {
        value = *nptr-- * digit + carry;
        carry = 0;
        while (value >= BASE) {
          value -= BASE;
          carry++;
        }
        *rptr-- = value;
      }
(I actually used a fancier trick than that, but in the same spirit.)

While I was at it, I also killed the Knuth trick in the BigNumber division routine. If nothing else, that would save a bit of RAM.
28  Using Arduino / Programming Questions / Re: Arbitrary precision (big number) library port for Arduino on: December 07, 2013, 02:38:49 pm
A simple improvement for the / 10, rewritten as a multiply

You completely missed the point.

I meant that, within the BigNumber library itself, are many instances of / 10 and % 10, most of which appear as / BASE and % BASE (as BASE is defined as 10).
What I meant was to kill these.

Besides, the BigNumber code for division is very poorly written. It seems to have been written by someone unfamiliar with pencil-and-paper division.
29  Using Arduino / Programming Questions / Re: Arbitrary precision (big number) library port for Arduino on: December 06, 2013, 04:49:39 pm
Nick, will you kill the % 10 and / 10 in the BigNumber library?
30  Community / Exhibition / Gallery / Re: Scrolling LED matrix display - Parola for Arduino on: December 04, 2013, 01:11:22 pm
How much to buy the things already assembled? Or at least with a circuit board already printed; I haven't the facilities for printing them.

Also, is there any intermediate size of matrix (maybe 40 or 50 millimeters) available?
Pages: 1 [2] 3 4 ... 14