Adjustable switch and Button to Start Arduino programme

I am looking to use this button

https://docs-emea.rs-online.com/webdocs/0d9a/0900766b80d9a9ac.pdf

To start off an arduino code which will fully rotate a stepper motor 1-6 times depending on the position of this adjustable switch:

I have soldered the pins of the adjustable switch to read the inputs 1, 3, 5, 7, 9, 11 - does anyone have any advice on how this code should look so that when the

adjustable switch is at position 1 the motor rotates once at the push of a button,
at position 3, the motor rotates twice at the push of a button, etc.

If anyone is able to help me with my problem or if any information is missing please let me know :slight_smile:

Renfree:
I have soldered the pins of the adjustable switch to read the inputs 1, 3, 5, 7, 9, 11 - does anyone have any advice on how this code should look so that when the

I am not sure what you mean. For example do you mean you have the switch contacts connected to Arduino pins 1, 3, 5 etc. Or are you referring to the numbers on the switch itself? (If they are Arduino pin numbers then do not use pins 0 or 1 as they are necessary for uploading programs.

You need to wire the switch so that when a position is selected it connects the relevant Arduino pin to GND. That means connecting the common pole of the switch to Arduino GND and connecting the other contacts to suitable Arduino pins.

Each of the Arduino pins you use should be initialized with

pinMode(pin, INPUT_PULLUP);

and then your program can read each pin turn to see which one is selected (it will read LOW).

pinState = digitalRead(pin);

It will be much easier to manage the group of selector pins if you use arrays. Read about them in the Reference section

...R

What have you tried?

Let's start easy. Don't bother with the switches at all for the moment, can you make the motor rotate once?

And for part two:

Renfree:
adjustable switch is at position 1 the motor rotates once at the push of a button,
at position 3, the motor rotates twice at the push of a button, etc.

So what do you want to happen in position 2? :wink:

And 6 inputs for 12 positions?

Robin2:
I am not sure what you mean. For example do you mean you have the switch contacts connected to Arduino pins 1, 3, 5 etc. Or are you referring to the numbers on the switch itself? (If they are Arduino pin numbers then do not use pins 0 or 1 as they are necessary for uploading programs.

You need to wire the switch so that when a position is selected it connects the relevant Arduino pin to GND. That means connecting the common pole of the switch to Arduino GND and connecting the other contacts to suitable Arduino pins.

Each of the Arduino pins you use should be initialized with

pinMode(pin, INPUT_PULLUP);

and then your program can read each pin turn to see which one is selected (it will read LOW).

pinState = digitalRead(pin);

It will be much easier to manage the group of selector pins if you use arrays. Read about them in the Reference section

...R

Hi and thanks for your response, I have a wire connected to the location 1, 3, 5, 7, 9 and 11 on the rotary switch, the input pins to the Arduino will likely use pins 2, 3, 4, 5, 6 and 7 on the Arduino, I have also connected a wire to the 5V reference on the rotary switch and a wire to the ground pin of the rotary switch.

septillion:
What have you tried?

Let's start easy. Don't bother with the switches at all for the moment, can you make the motor rotate once?

And for part two:So what do you want to happen in position 2? :wink:

And 6 inputs for 12 positions?

I currently have a motor that rotates continuously when connected to a driver and a power supply, but am unsure of how to integrate the rotary switch to get this to work.

The reason I have 12 positions is due to the switch that was easiest available to me, in an idle world it would have been a switch with just 12 positions :slight_smile:

Renfree:
I have also connected a wire to the 5V reference on the rotary switch and a wire to the ground pin of the rotary switch.

That sounds like a recipe for a short circuit and a damaged Arduino.

Please make a simple pencil drawing of all the connections and post a photo of the drawing. Written descriptions are very easy to misunderstand. See this Simple Image Guide

...R

Doesn't have a 12 positions switch just 12 positions? Even in an ideal world?

But yeah, time for a drawing. And up to the code to make the motor rotate :slight_smile: Switches not in sight yet.

Renfree:
The reason I have 12 positions is due to the switch that was easiest available to me, in an idle world it would have been a switch with just 12 positions :slight_smile:

I assume you meant " in an ideal world it would have been a switch with just 6 positions :slight_smile: "

(We are in an idle world- lol).

That type of switch usually has an adjustable end-stop that you can use to configure it to operate as anything between a 2 position and a 12 position switch.

Have a look under the mounting nut, there is usually a part you lift, rotate and then re-engage to limit the angle of rotation.

Your switch does have an adjustable end-stop, it is mentioned in the datasheet.