Programming a neutral/off range to control relays

I have a spring centered analog thumbwheel switch that I need to use in this project. I need help programming a neutral/off range for relays. I found the center/neutral point on thumbwheel switch at approximately what =522.
. This is too sensitive and does not always turn off relays when the thumbwheel switch is centered to the neutral/off position without searching for =522.

Please reference sketch Project 51. The lines I am having difficulty with are highlighted.

Project 51.pdf (76.6 KB)

it is very difficult to extract code from a pdf.
please edit your post, posting the code in the highlighted area after clicking the <code> icon

shouldn't setting the relay LOW simply be an else case?

are you using an analog input to read the thumb switch?

unless you are very careful and use high quality components one tends to get some noise - use a range, e.g. switch off if reading is in the range 510 to 530???

he switches in on when > 700 and < 300

else if sensorValue >> 500 && sensorValue << 600;

You can adjust the upper and lower limits to suit what works best for you.

That’s another good way of doing it.

Hello maverick61

You might mod this joystick sketch to your project needs.

// make variables
constexpr uint8_t Joy_x {A0};
// make application
void setup()
{
  Serial.begin(115200);
}
void loop()
{
  switch (analogRead(Joy_x))
  {
    case 0 ... 500: Serial.println(F("lower bounds")); break;
    case 501 ... 520: Serial.println(F("middle bounds")); break;
    case 521 ... 1023: Serial.println(F("upper bounds")); break;
  }
  delay (1000); // for testing only 
}

Have a nice day and enjoy coding in C++.

I had to do a little modification to your code and I got everything working just as I need it to. Thanks!!

} else if (sensorValue >= 400 && sensorValue <= 600) {
digitalWrite(relays, LOW);}

Solved!
Thank you all for your help.

} else if (sensorValue >= 400 && sensorValue <= 600) {
digitalWrite(relays, LOW);}

All these recent posts about analog thumbwheel switches are confusing me…

THUMBWHEEL SWITCHES

IMG_0091

It could be they are using an analog input and a set of resistors in series, with the switches selecting the several points on the series string. One input to determine which switch is closed. I watched a video that used this method to decode a 16 button keypad.

Or maybe it is a potentiometer, spring centered like a one axis joystick.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.