Pages: [1]   Go Down
Author Topic: Driving LEDs and sensing at the same time  (Read 376 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am working on a project where I drive an LED and want to detect (using piezo sensors) whether a person sets off the sensor relatively close to when that LED went on. Think of it as whack-a-mole, and I am trying to detect if someone did actually whack the mole pretty fast. How do I do this considering there is no native multi-threading supported on the Arduino?
Logged

Mexico City
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Keepin' it awesome
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I guess this deppends on how many LEDs you're using. If you're using just a few, then you can poll them at regular, or almost regular intervals as fast as possible and use the time between analogue reading to perform any other operation.
Take in considaration that although the human hand may be able to move pretty fast (pressing a button as fast as you can would still take dozens of millis), it's still pretty slow for a microcontroller with a 16MHz clock which can give you a resolution of microseconds, so I wouldn't be too concerned about the lack of multihreading.
One problem could be that you may need to do A LOT of things while you're sensing so you can't keep a reliable timing. For this case you could use a timer interrupt which would let you run any code and every "N" milliseconds would execute a function (like the sensing of your LED). There are no native arduino functions for that but I found a library that seems pretty easy to use for timer interrupts: http://www.arduino.cc/playground/Main/MsTimer2
I hope this helps!
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 512
Posts: 26212
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you wrote this code (with the appropriate definitions)
Code:
void setup(){
Serial.begin(9600);
delay(1000);
digitalWrite (ledPin, HIGH);
startTime = millis();

}
void loop(){

if (digitalRead(button) == LOW){
endTime = millis();
digitalWrite(ledPin, LOW);
SerialPrint (endTime - startTime);
  }
}
and press Reset to start the program, I think you wlll surprised at just how big of a number you get back even if you are pretty fast.
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.

Pages: [1]   Go Up
Jump to: