Coding around reed switch "noise"


the code writing has been designed record time takes between reed switch being turned on, , next occurrence of switch being turned on.

during time, other things happening.

generally speaking switch turn on , off in short space of time, however, discovered arduino cycle few times in high switch state (anything 2 - 5 times) causes degree of inaccuracy.

code: [select]

void loop() {
  reedswitchstate1 = digitalread(reedswitch1);
  if (reedswitchstate1 == high) {
        elapsedtime=((millis()-prevtime));
        serial.println(elapsedtime);
    prevtime = millis();

    //wait switch turn off

    int = 1;
    while (reedswitchstate1 == high) {     
      reedswitchstate1 = digitalread(reedswitch1);
    }


  }

  //check other switch states , stuff...



while code works, problem face if reed switch stays on high or spends large amount of time on high, other functionality not work efficiently.

now purpose of switch measure rotation time of wheel. if rotation slow, reed switch on longer period of time , see issue.


one solution use beam rather reed switch have lot less "play" , not have code in while loop...

any suggestions?


it's more reliable if detect changes in reed switch state, rather monitoring when goes high or low. this:

  • store old state of reed switch in variable.
  • test state , compare current state old state.
  • if old state low, , new state high, whatever need etc. otherwise nothing.
  • upate old state variable , go start of loop.


Arduino Forum > Using Arduino > Programming Questions > Coding around reed switch "noise"


arduino

Comments

Popular posts from this blog

DHT11 Time out error using v0.4.1library

Sketch upload fails with Java error (___REMOVE___/bin/avrdude)!

Arduino Uno + KTY81/210 temperature sensor