Pages: [1]   Go Down
Author Topic: Microswitch is too slow  (Read 339 times)
0 Members and 1 Guest are viewing this topic.
Florence - Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
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:
{
//Lettura numero giri
  int giri= analogRead(A5);
  float voltage = giri*(5.0/1023.0);
  if (voltage > 4.5)
  {
  String giriHeader = "N0001";
 
  Serial.println(messStart+giriHeader+messEnd);
  delay(1);
  }

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Florence - Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
int giri= analogRead(A5);
static bool msgSent=false;
if(giri > 920 && !msgSent)
  {
  Serial.print(messStart);
  Serial.print("N0001");
  Serial.println(messEnd);
  msgSent=true;
  delay(10); // debounce
  }
if(giri < 100)
  {
  msgSent=false;
  delay(10); // debounce
  }

not tested, not even compiled.
Logged

Florence - Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot. It works perfectly. Not a single mistake and it does exactly was I was looking for.
Thanks.

« Last Edit: July 06, 2013, 01:35:23 am by felicemassaro » Logged

Pages: [1]   Go Up
Jump to: