Switch with safe function

Hello, i'm having some difficulty with giving my program a certain functionality that has to be a kind of safety feature in my application.

what i want to make is a stepper motor driving a variac via a timing belt, so that i can use it as an AC variable voltage supply.

my code reades a 3 state switch, which puts the arduino in 3 different states, that for now only turn on an SSR connected to the variac.

The part im having trouble with is the following, when starting up, i want the state to be in 0 where everything is off, even when the physical switch is in 1 or 11(2), only when the switch has been in state 0 the state command should switch and do what the user want to do, this is to prevent that things start homing and doing things when the power has been cut off of the arduino.

if something is unclear please ask because i dont know very certain if i am making it clear what i want.

here is my code:

#define SWITCH_POSITION_1  2
#define SWITCH_POSITION_2  3
#define HOME_SWITCH 11
#define SSR 9
#define BYPASS_RELAY 8


// variables
byte switchState = 0;             // to store switch reading 0 = off 1 = posA 2 = posB

void setup() {

void loop() {
    switchState = 1;
  else if (READ_SWITCH_POSITION_2 == LOW) {
    switchState = 2;
  else switchState = 0;
  switch (switchState) {
    case 0:
      digitalWrite(SSR , LOW);
    case 1:
      digitalWrite (SSR, HIGH );
    case 2:
      digitalWrite(SSR, HIGH);

What state is it in when the program starts? Kind of important information.

i want the state of the code to be in 0 when the code starts, the state of the physical switch is uncertain, only when the switch has been in state 0 at least once, i want the code to be able to switch to other states

Does the "safety" involve mitigating risks to human beings or animals? If so you should not depend on software controlled protection. You need hard wired switches that will cut the power, or perform some action that will immediately and unequivocally shut the entire system down.

You have the code, Make it so.

well maybe if someone is putting his fingers between the timing belt an injury could be made, but this module will be build into an enclosure so it will be fine, its not so critical.

Please follow the forum protocol and edit your code so it is in code tags. Isn't your switch state already zero when you start?

byte switchState = 0;             // to store switch reading 0 = off 1 = posA 2 = posB

yes it is, but if the switch is put into state 2, it goes into the switch state 2, which it should only do when it has been at least once in physical state 0.

this is so that when the power goes off the arduino, it doesnt go into state 2 or 1 and initiate the stepper motor or SSR switching the variac

What is "physical state 0"?

Put physical state 0 in setup().

physical state 0 is the 3 way switch, it is this state when neither one of the 2 inputs are connected to ground

So test for that, and don't proceed until it's true. What about your users? Won't they be baffled when the machine sits idle for no apparent reason?


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