Arizona
Offline
Newbie
Karma: 0
Posts: 4
|
 |
« on: February 22, 2013, 11:27:54 pm » |
Hello I am new to Arduino but not new to electronics I have over 30 years electronics engineering experience. This is the 2nd controller I have purchased the first was a basic stamp and I hated it. With my Arduino I have figured out more in 5 days than I have with my stamp in a year. My issue is i did a setup that each time a switch or reed is closed it increments a counter by one. The reed is on a 22" wheel for my wheelchair I am trying to calculate distance i wheel when i go out to exercise. It all works great does the math and on my LCD 16x2 display it shows feet traveled and miles. Issue I have is I cant figure out how to get it to count once then wait for the switch to change states. the micro is faster than the switch so some times I get multiple counts. I put in a delay of a few milliseconds that fixed the multiple counts but now it some times misses counts. I did a setup using a cap and 2 resistors and a diode to give about a 2 millisecond pulse low. I looked at the low pulse on my DSO scope and am not seeing a bounce. I need a way to execute the program one time once the switch goes low then wait till it sees the switch go high again before it can do it all over again. Currently if i just put a switch in with out the cap, resistor and diode and press it the counter will count until i let the switch go. Any help would be greatly appreciated.
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 27
Posts: 1539
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #1 on: February 22, 2013, 11:38:28 pm » |
Look at the button state change example.
|
|
|
|
|
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, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #2 on: February 23, 2013, 01:10:36 am » |
Issue I have is I cant figure out how to get it to count once then wait for the switch to change states.
Find the state. Has it changed from the old state? If so, count, and remember this as the old state.
|
|
|
|
|
Logged
|
|
|
|
|
texas
Offline
God Member
Karma: 26
Posts: 834
old, but not dead
|
 |
« Reply #3 on: February 23, 2013, 03:56:53 am » |
If you eliminated the noise from your trigger pulse, then you can simply "watch" the pin by doing digitalRead() inside an empty while loop. Once you see the pin go low, you just sit on it with something like:
looper: while(digitalRead(PIN) == HIGH); // wait for pulse while(digitalRead(PIN) == LOW); // wait for end revcounter++; // one more revolution occurred any other code you want to do; goto looper; // wait for the next one
I like to use interrupts for this kind of thing.
|
|
|
|
|
Logged
|
Experience, it's what you get when you were expecting something else.
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #4 on: February 23, 2013, 05:38:46 am » |
Don't use goto.
Why use 2 x while and then throw in a goto?
|
|
|
|
|
Logged
|
|
|
|
|
texas
Offline
God Member
Karma: 26
Posts: 834
old, but not dead
|
 |
« Reply #5 on: February 23, 2013, 05:57:04 am » |
Don't use goto.
Why use 2 x while and then throw in a goto?
It was just an example. I thought it would be more clear to the OP to show it that way.
|
|
|
|
|
Logged
|
Experience, it's what you get when you were expecting something else.
|
|
|
|
Arizona
Offline
Newbie
Karma: 0
Posts: 4
|
 |
« Reply #6 on: February 25, 2013, 01:20:33 am » |
OK I tried various versions of what you suggested and came up with one that works only problem is i have no idea why it works can you take a look at the following and tell me why this works, but it works perfect? /* 1st attempt to make a distance counter for a wheelchair. Switch is a momentary switch on right wheel that is 22" diamater. Axel has a cam that pushes switch each single rotation. */ #include <LiquidCrystal.h>
/* LCD Connections: rs (LCD pin 4) to Arduino pin 12 rw (LCD pin 5) to Arduino pin 11 enable (LCD pin 6) to Arduino pin 10 LCD pin 15 to Arduino pin 13 LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2 */
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); int led = 13; // pin 13 shows button activity int x; float y; float z; int d; long m; const int Button1 =7 ; // assigns input 7
void setup() { pinMode(led, OUTPUT); digitalWrite(Button1, HIGH); //turn on internal pull up x=0; //rotation button y=5.759; //circumfrence of wheel in feet z=0; //feet traveled so far m=5280; // feet per mile lcd.begin(16, 2); // rows, columns. use 16,2 for a 16x2 LCD, etc. lcd.clear(); // start with a blank screen lcd.setCursor(0,0); // set cursor to column 0 row 0 }
void loop(){ if(digitalRead(Button1) == LOW) { while(digitalRead(Button1)== LOW); x++;} //Increments x by one if Button 1 is Low
digitalWrite(led, LOW); //Sets Pin 13 low Led is off.
{
digitalWrite(led, HIGH);} //Sets Pin 13 high if button is not pushed z=x*y; //Rotations times distance per rotation= feet traveled d=z/m; //Feet traveled / 5280(feet per mile) lcd.print("Feet: "); lcd.print(z); //Prints numeric amount of feet lcd.setCursor(0,1); lcd.print("Miles:"); lcd.print(d); //Prints numeric amount of feet lcd.setCursor(0,0); //Resets dispay. }
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 137
Posts: 19001
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: February 25, 2013, 03:27:00 am » |
//Increments x by one if Button 1 is Low The comment is incorrect; x is incremented when the pin transitions from LOW to HIGH because of the while loop. Please use code tags when posting code. y=5.759; //circumfrence of wheel in feet z=0; //feet traveled so far m=5280; // feet per mile Unless you're planning on changing these values, const float y=5.759; //circumfrence of wheel in feet const float m=5280; // feet per mile when they're declared would be clearer.
|
|
|
|
« Last Edit: February 25, 2013, 03:28:54 am by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
|