Go Down

### Topic: Voltage sensing switch Help.. (Read 3884 times)previous topic - next topic

#### strykeroz

#15
##### Apr 06, 2012, 11:18 am
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: [Select]
`  // 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: [Select]
`int inPin = 0;         // input pin is A0int outPin = 7;       // the number of the output pin (D7)long time = 0;         // the last time the output pin was toggledlong debounce = 5000;   // the debounce time, increase if the output flickersint reading;           // the current reading from the input pinint previous = LOW;    // the most recent value written to outPin, also the initial state of that LEDvoid 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
Geoff
"There is no problem so bad you can't make it worse" - retired astronaut Chris Hadfield

#### Aviator1

#16
##### Apr 06, 2012, 11:28 am
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
Thanks again Dean

#### strykeroz

#17
##### Apr 06, 2012, 11:32 am
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
"There is no problem so bad you can't make it worse" - retired astronaut Chris Hadfield

Go Up