Go Down

Topic: Microswitch is too slow (Read 696 times) previous topic - next topic


I am using a microswitch to count a very slow motor turns (5 turns per minute).
When I get the pulse from the switch I need to print out on serial a message and the application on the other side calculate the time between impulses and manage other features.
My problem is that being the motor very slow the microswitch is closed for 700 ms sending out for all that time messages on the serial and I need to send out a single impulse instead.
The switch is normally grounded and it gets the 5 V only when is closed, I am then measuring the voltage on an analog input and if the voltage is close to 5 I print out the message. meesStart and messEnd are simply ><.
This is my code:
Code: [Select]
//Lettura numero giri
  int giri= analogRead(A5);
  float voltage = giri*(5.0/1023.0);
  if (voltage > 4.5)
  String giriHeader = "N0001";

How can I get out a single Serial.println each time the switch is closed (even if it is closed for 1 second).


Add a boolean variable to keep track of whether you've sent a message. Set it when you send the message, clear it when you see voltage close to zero again. Test it as well as the voltage as a condition of sending the message.You'll likely need a debounce delay too.

You might consider working with the integer return from analog read too. In your snippet at least, you're not making any use of the voltage reading, so you can just compare the raw reading to 920.

Use of Strings seems unnecessary too.


Thank you. I never tought about using a bolean  to achieve this. I will try. If you can suggest a draft code would help me a lot. However really thanks.


Code: [Select]
int giri= analogRead(A5);
static bool msgSent=false;
if(giri > 920 && !msgSent)
  delay(10); // debounce
if(giri < 100)
  delay(10); // debounce

not tested, not even compiled.


Jul 05, 2013, 11:41 pm Last Edit: Jul 06, 2013, 08:35 am by felicemassaro Reason: 1
Thanks a lot. It works perfectly. Not a single mistake and it does exactly was I was looking for.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131