Pages: [1]   Go Down
 Author Topic: Help with my engineering project  (Read 308 times) 0 Members and 1 Guest are viewing this topic.
Offline
Newbie
Karma: 0
Posts: 1
 « on: May 21, 2013, 02:05:18 pm » Bigger Smaller Reset

For my engineering project, my group and I are usiwng Arduino to calculate the frequency of a square wave and lighting certain LEDs depending on the value of the frequency. We are having trouble with the millis() function. We have the code so that when the Arduino detects a value of 1 on digitalpin2, millis() begins. When the Arduino detects a value of 0 on digitalpin2, we want it to divide 1/(difference in time) to calculate the frequency. We want this loop to be continuous and have the millis counter reset after after calculation. can anyone help us please?

Thank you,
Marc
 Logged

Seattle, WA USA
Offline
Brattain Member
Karma: 518
Posts: 44563
Seattle, WA USA
 « Reply #1 on: May 21, 2013, 02:11:06 pm » Bigger Smaller Reset

Quote
We have the code so that when the Arduino detects a value of 1 on digitalpin2, millis() begins.
Which you didn't post, but is impossible. The value returned by millis() starts when the Arduino resets, and is not (supposed to be) ever reset.

Quote
When the Arduino detects a value of 0 on digitalpin2, we want it to divide 1/(difference in time) to calculate the frequency.
So, what is the problem?

Quote
We want this loop to be continuous and have the millis counter reset after after calculation. can anyone help us please?
What does "continuous" mean? You are either waiting for a change in the square wave or you are doing something while periodically looking for a change (possibly interrupt based). Which is it?

You shouldn't, and don't need to, reset millis(). You don't reset your watch 15 times a day, do you? Of course not. You learn early on how to use a watch to determine the interval between two events. You should be doing the same with the millis() value.
 Logged

Queens, New York
Offline
Karma: 59
Posts: 2737
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
 « Reply #2 on: May 21, 2013, 02:15:01 pm » Bigger Smaller Reset

Posting what ever code you have so far, is a good start.

Another thing is to be familiar with state changes. If the pin is high, start a timer, when the pin changes state, store the time in a variable. Once you have done that twice, (HIGH and LOW), add them together and you will have your time.

Tip: Learn how to use interupts.
 Logged

UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W

"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown

Seattle, WA USA
Offline
Brattain Member
Karma: 518
Posts: 44563
Seattle, WA USA
 « Reply #3 on: May 21, 2013, 02:16:34 pm » Bigger Smaller Reset

Quote
If the pin is high, start a timer
You mean "record the time". There is no need to use a timer.
 Logged

Global Moderator
Boston area, metrowest
Offline
Brattain Member
Karma: 415
Posts: 22724
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
 « Reply #4 on: May 21, 2013, 02:18:49 pm » Bigger Smaller Reset

Use pulseIn(). Measure the high time, measure the low time, add together, there's your period.
 Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Queens, New York
Offline
Karma: 59
Posts: 2737
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
 « Reply #5 on: May 21, 2013, 02:19:37 pm » Bigger Smaller Reset

@PaulS
Yes, record the time.