Pages: 1 [2]   Go Down
Author Topic: Voltage sensing switch Help..  (Read 1457 times)
0 Members and 1 Guest are viewing this topic.
Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1121
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's doing what you're asking then.

Your sketch is telling it to write your outPin to LOW if it see's the spike, and HIGH if the input is 0V (actually you're testing for values less than 0 too, but that's not going to happen with an analogRead value).

This next block after that is a legacy of when your system was reading a digital pin, and no longer does what the comments say it does
Code:
  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    }
 
  }
...since reading will never be HIGH, and you don't set previous at any stage now.

If you want to only toggle on high, and only after your 5sec debounce, something like this should do the trick:
Code:
int inPin = 0;         // input pin is A0
int outPin = 7;       // the number of the output pin (D7)
long time = 0;         // the last time the output pin was toggled
long debounce = 5000;   // the debounce time, increase if the output flickers
int reading;           // the current reading from the input pin
int previous = LOW;    // the most recent value written to outPin, also the initial state of that LED

void setup() {
  pinMode(outPin, OUTPUT);
  digitalWrite(outPin, previous);  // set initial state
}

void loop() {
  reading = analogRead(inPin);        // read the value on analog input
 
  if ( reading >= ((1 * 1023) / 5) ) {  // greater than 1V
    if (millis() - time > debounce) {  // enough time has transpired to pay attention again
      // toggle the output pin
      if(previous == LOW)
        previous = HIGH;
      else
        previous = LOW;
      digitalWrite (outPin, previous);
      time = millis();                        // reset the timer
    }
  }
}
I didn't see your rationale for having both a state and a previous variable, so did away with one.  And you'll notice there's no else component to either of the if statements at this time.  The two nested IFs could be combined into a single one with an and (&&) in the logic but this gives you flexibility to do something else if the timer isn't up yet, by adding an else to that if you choose.

I'm hoping this buys me a reprieve from that previous silly post   smiley
Geoff
Logged

"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Geoff that works like a charm, need to get help on the code, as I said I have only just started Arduino and have a million and one ideas but struggle with them, what would you reccomend the best way to learn? I have just been learning through trail and error and butchering other peoples codes, it has worked some of the time but probably doing it the hard way smiley-grin
Thanks again Dean
Logged

Brisbane, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1121
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Dean,

The best way to learn is just what you're doing I think.  Plus check out some of the very worthwhile tutorials - http://www.jeremyblum.com/portfolio/arduino-tutorial-series/ - those from Jeremy Blum are worth watching from start to finish, and are organised so you can go back to topics for reference later.

Glad it's going for you now.  Cheers ! Geoff
Logged

"There is no problem so bad you can't make it worse"
- retired astronaut Chris Hadfield

Pages: 1 [2]   Go Up
Jump to: