Example 05 ‐ Motor fix speed, On Off‐Switch - GSTCH/SketchMadeEASY GitHub Wiki

Motor fix speed, controlled by switch (L-0-R). Motor turn 100% of the maximum speed forward and into the other direction (backwards) 80%. Sketch Made EASY supports controlling motors by different chips (shields):

  • L298
  • Adafruit MotorShield V2
  • L9110

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/05-MotorFixSpeedSwitch".

05a ‐ L298 chip

Schematic

Code

The thee variants differ in the definition of the actuator (motor).

#include <Easy.h>

//*****************************************************************
// Parameter Motor L298
#define MOTOR_SPEEDPIN 10
#define MOTOR_DIRECTIONPIN 12
// Parameter of Switch
#define MOTOR_SWITCH_FORWARDPIN 36
#define MOTOR_SWITCH_BACKWARDPIN 37
// Parameter Fix Value
#define MOTOR_SPEED_MIN 0
#define MOTOR_SPEED_MAX 100
#define FIX_VALUE_FORWARDSPEED_SPEED 100
#define FIX_VALUE_BACKWARDSPEED_SPEED -80

void setup()
{
  //((*** Initialize: Configure your sketch here....

  //** Create actuator:
  Actuator* motor = new MotorL298(MOTOR_DIRECTIONPIN, MOTOR_SPEEDPIN);

  //** Create input:
  // Switch used by condition in the logic.
  Input* motorSwitch = new Switch3Position(MOTOR_SWITCH_FORWARDPIN, MOTOR_SWITCH_BACKWARDPIN);

  //** Define logic with conditions and relations
  // Define relation when switch is at position 1 --> Turn forward with fix speed
  Condition* motorForwardCondition = new CompareCondition(motorSwitch, OpEQ, Switch3Position::Pos1);
  Relation* relationMotorForward = new Relation1to1(motorForwardCondition, motor, FixValue::Percent(FIX_VALUE_FORWARDSPEED_SPEED));

  // Define relation when switch is at position 2 --> Turn backward with fix speed
  Condition* motorBackwardCondition = new CompareCondition(motorSwitch, OpEQ, Switch3Position::Pos2);
  Relation* relationMotorBackward = new Relation1to1(motorBackwardCondition, motor, FixValue::Percent(FIX_VALUE_BACKWARDSPEED_SPEED));

  // Define relation when switch is at position 0 --> stopped
  Condition* motorStopCondition = new CompareCondition(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);
}

05b ‐ Adafruit MotorShield V2 at I2C

Schematic

Code

The thee variants differ in the definition of the actuator (motor).

#include <Easy.h>

//*****************************************************************
// Parameter I2C Motor
#define MOTOR_NUMBER 1
// Parameter of Switch
#define MOTOR_SWITCH_FORWARDPIN 36
#define MOTOR_SWITCH_BACKWARDPIN 37
// Parameter Fix Value
#define MOTOR_SPEED_MIN 0
#define MOTOR_SPEED_MAX 100
#define FIX_VALUE_FORWARDSPEED_SPEED 100
#define FIX_VALUE_BACKWARDSPEED_SPEED -80

void setup()
{
  //((*** Initialize: Configure your sketch here....

  //** Create actuator:
  Actuator* motor = new MotorI2C(MOTOR_NUMBER);

  //** Create input:
  // Switch used by condition in the logic.
  Input* motorSwitch = new Switch3Position(MOTOR_SWITCH_FORWARDPIN, MOTOR_SWITCH_BACKWARDPIN);

  //** Define logic with conditions and relations
  // Define relation when switch is at position 1 --> Turn forward with fix speed
  Condition* motorForwardCondition = new CompareCondition(motorSwitch, OpEQ, Switch3Position::Pos1);
  Relation* relationMotorForward = new Relation1to1(motorForwardCondition, motor, FixValue::Percent(FIX_VALUE_FORWARDSPEED_SPEED));

  // Define relation when switch is at position 2 --> Turn backward with fix speed
  Condition* motorBackwardCondition = new CompareCondition(motorSwitch, OpEQ, Switch3Position::Pos2);
  Relation* relationMotorBackward = new Relation1to1(motorBackwardCondition, motor, FixValue::Percent(FIX_VALUE_BACKWARDSPEED_SPEED));

  // Define relation when switch is at position 0 --> stopped
  Condition* motorStopCondition = new CompareCondition(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);
}

05c ‐ L9110

Schematic

Code

The thee variants differ in the definition of the actuator (motor).

#include <Easy.h>

//*****************************************************************
// Parameter Motor L9110
#define MOTOR_PINA1 44
#define MOTOR_PINB1 46
#define MOTOR_PINA2 11
#define MOTOR_PINB2 12
// Parameter of Switch
#define MOTOR_SWITCH_FORWARDPIN 36
#define MOTOR_SWITCH_BACKWARDPIN 37
// Parameter Fix Value
#define MOTOR_SPEED_MIN 0
#define MOTOR_SPEED_MAX 100
#define FIX_VALUE_FORWARDSPEED_SPEED 100
#define FIX_VALUE_BACKWARDSPEED_SPEED -80

void setup()
{
  //((*** Initialize: Configure your sketch here....

  //** Create actuator:
  Actuator* motor = new MotorL9110(MOTOR_PINA1, MOTOR_PINB1);

  //** Create input:
  // Switch used by condition in the logic.
  Input* motorSwitch = new Switch3Position(MOTOR_SWITCH_FORWARDPIN, MOTOR_SWITCH_BACKWARDPIN);

  //** Define logic with conditions and relations
  // Define relation when switch is at position 1 --> Turn forward with fix speed
  Condition* motorForwardCondition = new CompareCondition(motorSwitch, OpEQ, Switch3Position::Pos1);
  Relation* relationMotorForward = new Relation1to1(motorForwardCondition, motor, FixValue::Percent(FIX_VALUE_FORWARDSPEED_SPEED));

  // Define relation when switch is at position 2 --> Turn backward with fix speed
  Condition* motorBackwardCondition = new CompareCondition(motorSwitch, OpEQ, Switch3Position::Pos2);
  Relation* relationMotorBackward = new Relation1to1(motorBackwardCondition, motor, FixValue::Percent(FIX_VALUE_BACKWARDSPEED_SPEED));

  // Define relation when switch is at position 0 --> stopped
  Condition* motorStopCondition = new CompareCondition(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);
}
⚠️ **GitHub.com Fallback** ⚠️