Example 07‐Motor variable speed, direction‐ and main‐switch - GSTCH/SketchMadeEASY GitHub Wiki
Motor variable speed, controlled by switch (L-0-R) and a additional main switch (0-1). 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/07-MotorVariableSpeedDirectionAndMainSwitch".

#include <Easy.h>
//*****************************************************************
// Parameter Motor L298
#define MOTOR_SPEEDPIN 10
#define MOTOR_DIRECTIONPIN 12
// Parameter MainSwitch
#define MAIN_SWITCH_PIN 29
// Parameter of Switch
#define MOTOR_SWITCH_FORWARDPIN 32
#define MOTOR_SWITCH_BACKWARDPIN 33
// Parameter variable Input
#define VARIABLE_INPUT_PIN A15
void setup() {
//((*** Initialize: Configure your sketch here....
//** Create actuator:
Actuator* motor = new MotorL298(MOTOR_DIRECTIONPIN, MOTOR_SPEEDPIN);
//** Create input:
// Create input direction switch
Input* motorSwitch = new Switch3Position(MOTOR_SWITCH_FORWARDPIN, MOTOR_SWITCH_BACKWARDPIN);
// Create input main switch
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);
//** Define logic with conditions and relations
// Define relation when motor turns forward
Condition* motorForwardCondition = new LogicCondition(mainSwitch, OpEQ, Switch2Position::On, LgAND, motorSwitch, OpEQ, Switch3Position::Pos1);
Relation* relationMotorForward = new Relation1to1(motorForwardCondition, motor, motorSpeed);
// Define relation when motor turns backward
Condition* motorBackwardCondition = new LogicCondition(mainSwitch, OpEQ, Switch2Position::On, LgAND, motorSwitch, OpEQ, Switch3Position::Pos2);
Relation* relationMotorBackward = new Relation1to1(motorBackwardCondition, motor, inverter);
// Define relation when motor stops
Condition* motorStopCondition = new LogicCondition(mainSwitch, OpEQ, Switch2Position::Off, LgOR, motorSwitch, OpEQ, Switch3Position::PosMid);
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 1
// Parameter MainSwitch
#define MAIN_SWITCH_PIN 29
// Parameter of Switch
#define MOTOR_SWITCH_FORWARDPIN 32
#define MOTOR_SWITCH_BACKWARDPIN 33
// Parameter variable Input
#define VARIABLE_INPUT_PIN A15
void setup() {
//((*** Initialize: Configure your sketch here....
//** Create actuator:
Actuator* motor = new MotorI2C(MOTOR_NUMBER);
//** Create input:
// Create input direction switch
Input* motorSwitch = new Switch3Position(MOTOR_SWITCH_FORWARDPIN, MOTOR_SWITCH_BACKWARDPIN);
// Create input main switch
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);
//** Define logic with conditions and relations
// Define relation when motor turns forward
Condition* motorForwardCondition = new LogicCondition(mainSwitch, OpEQ, Switch2Position::On, LgAND, motorSwitch, OpEQ, Switch3Position::Pos1);
Relation* relationMotorForward = new Relation1to1(motorForwardCondition, motor, motorSpeed);
// Define relation when motor turns backward
Condition* motorBackwardCondition = new LogicCondition(mainSwitch, OpEQ, Switch2Position::On, LgAND, motorSwitch, OpEQ, Switch3Position::Pos2);
Relation* relationMotorBackward = new Relation1to1(motorBackwardCondition, motor, inverter);
// Define relation when motor stops
Condition* motorStopCondition = new LogicCondition(mainSwitch, OpEQ, Switch2Position::Off, LgOR, motorSwitch, OpEQ, Switch3Position::PosMid);
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);
}

//*****************************************************************
// 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 29
// Parameter of Switch
#define MOTOR_SWITCH_FORWARDPIN 32
#define MOTOR_SWITCH_BACKWARDPIN 33
// Parameter variable Input
#define VARIABLE_INPUT_PIN A15
void setup() {
//((*** Initialize: Configure your sketch here....
//** Create actuator:
Actuator* motor = new MotorL9110(MOTOR_PINA1, MOTOR_PINB1);
//** Create input:
// Create input direction switch
Input* motorSwitch = new Switch3Position(MOTOR_SWITCH_FORWARDPIN, MOTOR_SWITCH_BACKWARDPIN);
// Create input main switch
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);
//** Define logic with conditions and relations
// Define relation when motor turns forward
Condition* motorForwardCondition = new LogicCondition(mainSwitch, OpEQ, Switch2Position::On, LgAND, motorSwitch, OpEQ, Switch3Position::Pos1);
Relation* relationMotorForward = new Relation1to1(motorForwardCondition, motor, motorSpeed);
// Define relation when motor turns backward
Condition* motorBackwardCondition = new LogicCondition(mainSwitch, OpEQ, Switch2Position::On, LgAND, motorSwitch, OpEQ, Switch3Position::Pos2);
Relation* relationMotorBackward = new Relation1to1(motorBackwardCondition, motor, inverter);
// Define relation when motor stops
Condition* motorStopCondition = new LogicCondition(mainSwitch, OpEQ, Switch2Position::Off, LgOR, motorSwitch, OpEQ, Switch3Position::PosMid);
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);
}