Switches

Watch a video tutorial

Switches can be connected to any of the digital pins on your Arduino/Genuino Uno or Mega, labelled 2-13 on the Uno and 2-53 on the Mega. They can also be connected to any of the or any of the analogue pins, labelled A0-A5 on the Uno and A0-A15 on the Mega, as shown in this example:

Switch circuit diagram

The 10kΩ resistor (kΩ is pronounced ‘kilo Ohm’) is being used here as a pulldown resistor. Its function is to pull down the input pin to ground (a voltage of 0V) when the switch is off, and to avoid a short-circuit occurring when the switch is on.

Resistor circuit symbol and identification

Once your switch is connected to your Arduino/Genuino Uno or Mega, use the Connect command to give it a name and tell MECControl which pin it is connected to:

Connect Power Switch To Pin A5

In this example, a Switch named Power has been connected to the analogue pin labelled A5. You can then refer to the switch by name in later commands, as in this example:

Wait For Power On

This Wait command pauses the sequence until the Power switch is On. You could just as easily wait until the switch is Off:

Wait For Power Off

The commands High and Low or Pressed and Released can be used in place of On and Off if you prefer.

Sometimes it is useful to give the switch actions names that mean something in the context of your project.

You can do this by adding named Actions to the end of the Connect command:

Connect EndStop Switch To Pin A5 With Actions Hit NotHit

If the switch, now named EndStop, is used to detect when a mechanism has hit the end of its travel, this action can be detected using the command:

Wait For EndStop Hit

The Jump command can also be used to jump playback to a labelled line when a switch action occurs:

Jump To StopMotor If EndStop Hit

Switch vs Button

Switches and buttons are treated in similar ways by MECControl, but there is one important difference:

A Switch action will be missed if it isn’t happening at the exact moment that a Wait or Jump command checks to see if the action is occurring.

A Button action will be remembered until a Wait or Jump command checks to see if the action is occurring.