Show Posts
Pages: 1 ... 7 8 [9] 10 11 ... 25
121  Using Arduino / Programming Questions / Re: First Program on: June 27, 2013, 11:58:21 am
I thought it was pretty straight forward but I'll see what I can do.

There are two global variables seconds which we will store our seconds in and lastTime which we will use to track how long it's been since we last calculated seconds.

In loop() countSeconds will be called every time. When countSeconds runs it puts the current millis in currentTime. Then we calculate elapsedTime which is currentTime - lastTime. So now we know how long it's been, in millis() since the last time countSecnds ran. The conditional, if, statment simply checks to see if elapsedTime is more than one second becuase if it isn't there is nothing to do and it returns 0 which gets added to seconds.

If elapsed time is longer than one second (1000 millis) then it has work to do.

First I record the new lastTime for our next calculation.

Code:
lastTime = currentTime - (elapsedTime % 1000);

Since we only account for full seconds we need to save any fractions of seconds to maintain some accuracy. So lastTime gets assigned the value of the currentTime less any fractions we didn't use. The % operator is modulo and returns the remainder from a divide operation. If elapsedTime was 1500, the remainder would be 500, by subtracting this from currentTime we don't lose that fraction of a second.

The next line simply returns elapsedTime / 1000 which will be all the seconds elapsedTime represents. If elapsedTime was 1500 the return value would be 1 and this gets added to seconds. The remaining 500 millis were accounted for in the previous line.

Hope that helps. You should be able to go through it line by line and see what's happening. You might look up the % (modulo) operator as well.
122  Using Arduino / Programming Questions / Re: Fresh view - Simple L.E.D chaser on: June 26, 2013, 03:43:29 pm
Here's a good trick to do with this stuff.

Code:
byte ledPin[] = {0,1,2,3,4,5,6,7,8}; //LED array for pins
byte ledCount = sizeof(ledPin) / sizeof(ledPin[0]};  // calculates the number of elements in the array

// now use this in your for loops

for (int x=0; x < ledCount; x++)


With this approach you could add or remove a pin or two and not have to change any code to match. You can even use this value for the direction change.
123  Using Arduino / Programming Questions / Re: First Program on: June 26, 2013, 09:57:43 am
I did this quickly (about ten minutes) so it may have a rough edge or two but what it does is store how many seconds have gone by in the global variable seconds. It uses the global variable lastTime to do this. The function countSeconds simply calculates how many seconds have gone by since it was last called and returns that value.

Loop() just spits out the current value of seconds but you can see that the count changes at one second intervals. It would be up to you to decide whether you want to just count seconds and calculate the time from that or roll over and increment minutes and hours like you were doing. Watch though that seconds could be higher than sixty and you'll have to keep the extra seconds.

Code:
int seconds = 0;
unsigned long lastTime = 0;

void setup(){
  Serial.begin(115200);
}

void loop(){
  seconds += countSeconds(); // same as seconds = seconds + countSeconds()
  Serial.println(seconds);   // notice that the numbers count up every second
}

// returns number of seconds since last call
int countSeconds(){
  unsigned long currentTime = millis();
  unsigned long elapsedTime = currentTime - lastTime;
  if (elapsedTime > 1000)
  {
    // keep the new lastTime accounting for fractions of seconds not used
    lastTime = currentTime - (elapsedTime % 1000);
    return elapsedTime / 1000;
  }
  else return 0;
}


When the Arduino boots the time is 0 millis, two minutes later the time is 120000 millis. Computers just count ticks in this case the ticks are one millisecond apart. At around fifty days the unsigned long variable runs out of space and millis starts over at 0. The snippet I did shouldn't be bothered by this rollover.
124  Using Arduino / Programming Questions / Re: alter makey makey leonardo code for keyboard.press on: June 25, 2013, 06:27:58 pm
Double quotes are a string literal.

Single quotes are a character constant.

125  Using Arduino / Programming Questions / Re: alter makey makey leonardo code for keyboard.press on: June 25, 2013, 05:00:16 pm
try 's' not "s"

Welcome to C
126  Using Arduino / Programming Questions / Re: First Program on: June 25, 2013, 03:52:46 pm
endTime - startTime = elapsedTime

The Arduino counts ticks. Every millisecond it adds one. You can ask the computer for this value at any time. It's done with the millis() function and it's an unsigned long value. Some of the code snippets I posted use millis().

If you have a start time and an end time you can know how many millis have gone by and convert that to seconds. This will give you more accurate time. With your strategy if one of those led fade statements fire it could be 2 seconds before you get back to incrementing seconds. You will have missed some. With an elapsed time approach you can't miss. Look back through this thread. Arrch described this already.
127  Using Arduino / Programming Questions / Re: First Program on: June 25, 2013, 11:22:31 am
You're going to have a big problem coming your way with your strategy for tracking time. As I mentioned before simply using delay is not going to be accurate. This gets compounded by the led fading if statements. They take over 1500 millis to execute on the delays alone. If you then just sleep for one second and increment seconds it won't be accurate.

You either have to make your fading and timing non-blocking or consider using a strategy similar to what Arrch pointed out. Checking elapsed millis and calculate the amount of seconds passed and update your seconds variable accordingly. When I think about it I'd probably do both, make the fading non-blocking and use the elapsed millis to calculate seconds.

Also you should lose the cout stuff. From what I've learned most of the LCD displays inherit their output method from Serial.print. If you change your display to Serial prints it will be easy to change them to LCD display.
128  Using Arduino / Programming Questions / Re: First Program on: June 24, 2013, 03:31:34 pm
Should I assume that this is an academic exercise? Otherwise I'd say you're crazy to reinvent the wheel but inventing wheels is a great way to learn about wheels.

Incrementing a variable called seconds is fine. I'd use a technique along the lines of the blink without delay example. Just putting in a delay(1000) will result in a clock that runs slow. You might get away with simply tuning the delay amount but that will depend on how the rest of the code works out and how accurate you need the clock to be.

This loop() will count up in seconds using the blink without delay style.

Code:
void loop(){
    const unsigned long sec_delay = 1000;
    static unsigned long seconds = 0;
    static unsigned long lastTime = 0;
    unsigned long currentTime = millis();
    if (currentTime - lastTime >= sec_delay)
    {
        lastTime = currentTime;
        Serial.println(seconds++);
    }
}
129  Using Arduino / Programming Questions / Re: First Program on: June 24, 2013, 01:35:27 pm
You can't get away from using the computer's clock. A computer's clock just counts ticks (at a known interval). The time.h library looks after the details so you can just ask for the time of day. If you want to roll your own clock, certainly doable, you shouldn't use the time library.

That's why I asked why you load the library, ask it for the time with now(), but never use it. If you wish to use the time library, you're using it wrong. If not you don't need it at all.
130  Using Arduino / Programming Questions / Re: First Program on: June 24, 2013, 11:45:28 am
Quote
used the time.h library for the veriables, so that my program can refer back to the time value of ecach variable for "if" arguments.

I thought you were doing that.

They have no way of knowing about the time library. time.h has methods for getting the time, you've used the most basic now() which returns Unix time (the time in seconds since an epoch).

Here's a loop() that should display the time. I think it will demonstrate.

Code:
void loop(){
    static unsigned long lastTime = 0;   // unsigned long is the same as time_t
    unsigned long t = now();  // get the time store in t
    if (lastTime != t)           // only update display if time has change should be one second
    {
        int hh = hour(t);  // uses the value of t to return the hour
        int mm = minute(t);
        int ss = second(t);
        if (hh < 10) Serial.print("0");
        Serial.print(hh);
        Serial.print(":");
        if (mm < 10) Serial.print("0");
        Serial.print(mm);
        Serial.print(":");
        if (ss < 10) Serial.print("0");
        Serial.println(ss);
        lastTime = t; // save the time
    }
}


With the time libarry the methods hour(), minute(), second(), day() ... etc can be used without a parameter and they will return the current value. You can pass them a Unix time and they will return a value based on that. For the clock display above this is best practice. It avoids have the time change while displaying.

So, you read the time but your variables have no way of knowing about it unless you assign those values using the methods in the time library.

Make sense?
131  Using Arduino / Programming Questions / Re: First Program on: June 24, 2013, 10:27:05 am
Quote
I thought the "time_t t = now();" needed the "time.h" library.

It does, but other than reading the time you never use it.
132  Using Arduino / Programming Questions / Re: First Program on: June 24, 2013, 08:53:51 am
Use this one:

Code:
if (seconds>59)
{
     minute++;
     seconds=0;
}

My question is why do you load the time library, actually read the time but then never use it?
133  Using Arduino / Programming Questions / Re: Almost there ! - can someone help with this last bit please ? on: June 22, 2013, 01:48:54 pm
A couple of things I see quickly. You are printing the minute() twice. Once with the printDigits() and you probably left one behind. You also appear to sample the sensor once a second and save each time. That's over 86000 samples a day. I have a similar logging program and I found that every 86 seconds works nice as that's a 1000 samples a day.

134  Using Arduino / Programming Questions / Re: Reaction Time Program on: June 13, 2013, 06:09:37 pm
I haven't spent much time going through it. I do see the pinmode for the switch being set to INPUT. It should be INPUT_PULLUP. Also the pin states are HIGH and LOW and are int type, not boolean. Though that won't always cause a problem in my experience.
135  Community / Bar Sport / Re: Electric blanket design on: June 13, 2013, 12:47:46 pm
The scale of cold from where I live:

-40C and colder: Very Cold
-30C : Quite Cold
-20C : Cold
-10C : Chilly (unless it's been quite cold then we might call this warm)
0 and above : Grab your shorts and fire up the grill. Summer is here.

Canadians love wearing shorts.
Pages: 1 ... 7 8 [9] 10 11 ... 25