Pages: [1]   Go Down
Author Topic: project help  (Read 553 times)
0 Members and 1 Guest are viewing this topic.
Arizona
Offline Offline
Newbie
*
Karma: 0
Posts: 17
Electronics engineer semi retired to Arizona. Owned my own electronics Co. for 14 years Prior to that I was a Electronics Lab Manager at Trionics Inc. and HyTech for many years along with many other jobs. I never used a micro controler till recently I was
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Online Online
Faraday Member
**
Karma: 99
Posts: 3635
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at the button state change example.
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't use goto.

Why use 2 x while and then throw in a goto?
Logged


texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 17
Electronics engineer semi retired to Arizona. Owned my own electronics Co. for 14 years Prior to that I was a Electronics Lab Manager at Trionics Inc. and HyTech for many years along with many other jobs. I never used a micro controler till recently I was
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  //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.

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, 
Code:
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." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: