Example 09‐Motor toggles between limit switches, main switch - GSTCH/SketchMadeEASY GitHub Wiki
Motor with variable speed, toggles between two limit switches. Main switch to stop the motor.
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/09-MotorTogglesBetweenLimitSwitchesWithMainSwitch".

#include <Easy.h>
//*****************************************************************
// Parameter Motor L298
#define MOTOR_SPEEDPIN 10
#define MOTOR_DIRECTIONPIN 12
// Parameter MainSwitch
#define MAIN_SWITCH_PIN 14
// 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:
// Main switch to start/stop all.
Input* mainSwitch = new Switch2Position(MAIN_SWITCH_PIN);
// 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 LogicCondition(mainSwitch, OpEQ, Switch2Position::On, LgAND, toggleSwitch, OpEQ, ToggleSwitch::Pos2);
Relation* relationMotorDirection1 = new Relation1to1(motorDirection1Condition, motor, motorSpeed);
// Define direction when signal 2 (limit switch 2 pressed)
Condition* motorDirection2Condition = new LogicCondition(mainSwitch, OpEQ, Switch2Position::On, LgAND, toggleSwitch, OpEQ, ToggleSwitch::Pos1);
Relation* relationMotorDirection2 = new Relation1to1(motorDirection2Condition, motor, inverter);
// Define stop all, when switch is off
Condition* motorStopCondition = new CompareCondition(mainSwitch, OpEQ, Switch2Position::Off);
Relation* relationMotorStop = new Relation1to1(motorStopCondition, motor, FixValue::Off());
// ***))
// 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
// Parameter MainSwitch
#define MAIN_SWITCH_PIN 14
// 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:
// Main switch to start/stop all.
Input* mainSwitch = new Switch2Position(MAIN_SWITCH_PIN);
// 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 LogicCondition(mainSwitch, OpEQ, Switch2Position::On, LgAND, toggleSwitch, OpEQ, ToggleSwitch::Pos2);
Relation* relationMotorDirection1 = new Relation1to1(motorDirection1Condition, motor, motorSpeed);
// Define direction when signal 2 (limit switch 2 pressed)
Condition* motorDirection2Condition = new LogicCondition(mainSwitch, OpEQ, Switch2Position::On, LgAND, toggleSwitch, OpEQ, ToggleSwitch::Pos1);
Relation* relationMotorDirection2 = new Relation1to1(motorDirection2Condition, motor, inverter);
// Define stop all, when switch is off
Condition* motorStopCondition = new CompareCondition(mainSwitch, OpEQ, Switch2Position::Off);
Relation* relationMotorStop = new Relation1to1(motorStopCondition, motor, FixValue::Off());
// ***))
// 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
// Parameter MainSwitch
#define MAIN_SWITCH_PIN 14
// 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:
// Main switch to start/stop all.
Input* mainSwitch = new Switch2Position(MAIN_SWITCH_PIN);
// 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 LogicCondition(mainSwitch, OpEQ, Switch2Position::On, LgAND, toggleSwitch, OpEQ, ToggleSwitch::Pos2);
Relation* relationMotorDirection1 = new Relation1to1(motorDirection1Condition, motor, motorSpeed);
// Define direction when signal 2 (limit switch 2 pressed)
Condition* motorDirection2Condition = new LogicCondition(mainSwitch, OpEQ, Switch2Position::On, LgAND, toggleSwitch, OpEQ, ToggleSwitch::Pos1);
Relation* relationMotorDirection2 = new Relation1to1(motorDirection2Condition, motor, inverter);
// Define stop all, when switch is off
Condition* motorStopCondition = new CompareCondition(mainSwitch, OpEQ, Switch2Position::Off);
Relation* relationMotorStop = new Relation1to1(motorStopCondition, motor, FixValue::Off());
// ***))
// 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);
}