Example 08‐Motor toggles between Limit‐Switches - GSTCH/SketchMadeEASY GitHub Wiki
Motor with variable speed, toggles between two limit switches.
Sketch Made EASY supports controlling motors by different chips (shields):
The different variants are presented in the following chapters. The difference is in the definition of the actuator. If Sketch Made EASY is installed, it can be opened via menu: "File:/Example/SketchMadeEASY/Examples/08-MotorTogglesBetweenLimitSwitches".

#include <Easy.h>
//*****************************************************************
// Parameter Motor L298
#define MOTOR_SPEEDPIN 10
#define MOTOR_DIRECTIONPIN 12
// ToggleSwitch
#define DIRECTION_PIN1 43 // NANO_DIRECTION_PIN1 5
#define DIRECTION_PIN2 41 // NANO_DIRECTION_PIN2 6
// Parameter variable Input
#define VARIABLE_INPUT_PIN A0
void setup()
{
//((*** Initialize: Configure your sketch here....
//** Create actuator:
Actuator* motor = new MotorL298(MOTOR_DIRECTIONPIN, MOTOR_SPEEDPIN);
//** Create input:
// Create variable input, defines speed
Input* motorSpeed = new VariableInput(VARIABLE_INPUT_PIN);
// To turn backward an inverter changes the sign of the variable input value
Input* inverter = new Inverter(motorSpeed);
// Switch knows the value Pos1 and Pos2. The value changes depending on two signal.
// This makes it possible to define motor direction (when value change).
Input* toggleSwitch = new ToggleSwitch(DIRECTION_PIN1, DIRECTION_PIN2);
//** Define logic with conditions and relations
// Define direction when signal 1 (limit switch 1 pressed)
Condition* motorDirection1Condition = new CompareCondition(toggleSwitch, OpEQ, ToggleSwitch::Pos2);
Relation* relationMotorDirection1 = new Relation1to1(motorDirection1Condition, motor, motorSpeed);
// Define direction when signal 2 (limit switch 2 pressed)
Condition* motorDirection2Condition = new CompareCondition(toggleSwitch, OpEQ, ToggleSwitch::Pos1);
Relation* relationMotorDirection2 = new Relation1to1(motorDirection2Condition, motor, inverter);
// ***))
// Initialize control
ControlManagerFactory::GetControlManager()->Setup();
}
//*****************************************************************
void loop() {
//*** Run: No additional code is required
ControlManagerFactory::GetControlManager()->Loop();
// Depending on Arduino it needs a short delay. Do not add any other delays!
delay(5);
}

#include <Easy.h>
//*****************************************************************
// Parameter I2C Motor
#define MOTOR_NUMBER 2
// ToggleSwitch
#define DIRECTION_PIN1 43 // NANO_DIRECTION_PIN1 5
#define DIRECTION_PIN2 41 // NANO_DIRECTION_PIN2 6
// Parameter variable Input
#define VARIABLE_INPUT_PIN A0
void setup()
{
//((*** Initialize: Configure your sketch here....
//** Create actuator:
Actuator* motor = new MotorI2C(MOTOR_NUMBER);
//** Create input:
// Create variable input, defines speed
Input* motorSpeed = new VariableInput(VARIABLE_INPUT_PIN);
// To turn backward an inverter changes the sign of the variable input value
Input* inverter = new Inverter(motorSpeed);
// Switch knows the value Pos1 and Pos2. The value changes depending on two signal.
// This makes it possible to define motor direction (when value change).
Input* toggleSwitch = new ToggleSwitch(DIRECTION_PIN1, DIRECTION_PIN2);
//** Define logic with conditions and relations
// Define direction when signal 1 (limit switch 1 pressed)
Condition* motorDirection1Condition = new CompareCondition(toggleSwitch, OpEQ, ToggleSwitch::Pos2);
Relation* relationMotorDirection1 = new Relation1to1(motorDirection1Condition, motor, motorSpeed);
// Define direction when signal 2 (limit switch 2 pressed)
Condition* motorDirection2Condition = new CompareCondition(toggleSwitch, OpEQ, ToggleSwitch::Pos1);
Relation* relationMotorDirection2 = new Relation1to1(motorDirection2Condition, motor, inverter);
// ***))
// Initialize control
ControlManagerFactory::GetControlManager()->Setup();
}
//*****************************************************************
void loop() {
//*** Run: No additional code is required
ControlManagerFactory::GetControlManager()->Loop();
// Depending on Arduino it needs a short delay. Do not add any other delays!
delay(5);
}

#include <Easy.h>
//*****************************************************************
// Parameter Motor L9110
#define MOTOR_PINA1 44
#define MOTOR_PINB1 46
#define MOTOR_PINA2 11
#define MOTOR_PINB2 12
// ToggleSwitch
#define DIRECTION_PIN1 43 // NANO_DIRECTION_PIN1 5
#define DIRECTION_PIN2 41 // NANO_DIRECTION_PIN2 6
// Parameter variable Input
#define VARIABLE_INPUT_PIN A0
void setup()
{
//((*** Initialize: Configure your sketch here....
//** Create actuator:
Actuator* motor = new MotorL9110(MOTOR_PINA1, MOTOR_PINB1);
//** Create input:
// Create variable input, defines speed
Input* motorSpeed = new VariableInput(VARIABLE_INPUT_PIN);
// To turn backward an inverter changes the sign of the variable input value
Input* inverter = new Inverter(motorSpeed);
// Switch knows the value Pos1 and Pos2. The value changes depending on two signal.
// This makes it possible to define motor direction (when value change).
Input* toggleSwitch = new ToggleSwitch(DIRECTION_PIN1, DIRECTION_PIN2);
//** Define logic with conditions and relations
// Define direction when signal 1 (limit switch 1 pressed)
Condition* motorDirection1Condition = new CompareCondition(toggleSwitch, OpEQ, ToggleSwitch::Pos2);
Relation* relationMotorDirection1 = new Relation1to1(motorDirection1Condition, motor, motorSpeed);
// Define direction when signal 2 (limit switch 2 pressed)
Condition* motorDirection2Condition = new CompareCondition(toggleSwitch, OpEQ, ToggleSwitch::Pos1);
Relation* relationMotorDirection2 = new Relation1to1(motorDirection2Condition, motor, inverter);
// ***))
// Initialize control
ControlManagerFactory::GetControlManager()->Setup();
}
//*****************************************************************
void loop() {
//*** Run: No additional code is required
ControlManagerFactory::GetControlManager()->Loop();
// Depending on Arduino it needs a short delay. Do not add any other delays!
delay(5);
}