Example 06c‐Motor variable speed, On Off‐Switch (L9110 chip) - GSTCH/SketchMadeEASY GitHub Wiki

Motor with variable speed, controlled by switch (L-0-R). The motor is controlled by a L9110 chip. If Sketch Made EASY is installed, it can be opened via menu: "File:/Example/SketchMadeEASY/Examples/06-MotorVariableSpeedSwitch".

Schematic

Code

The three 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 variable Input
#define VARIABLE_INPUT_PIN A0

void setup()
{
  //((*** Initialize: Configure your sketch here....
  
  //** Create actuator:
  MotorL9110* motor = new MotorL9110(MOTOR_PINA1, MOTOR_PINB1);

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

  // Variable input sets the rotation speed of the motor.
  VariableInput* motorSpeed = new VariableInput(VARIABLE_INPUT_PIN);
  // To turn backward, change sign to negativ, this makes the Inverter.
  Inverter* inverter = new Inverter(motorSpeed);

  //** Define logic with conditions and relations
  // Define relation when switch is at position 1 
  // --> Turn forward with variable speed in dependency of the variable input (potentiometer)
  CompareCondition* motorForwardCondition = new CompareCondition(motorSwitch, OpEQ, Switch3Position::Pos1);
  Relation1to1* relationMotorForward = new Relation1to1(motorForwardCondition, motor, motorSpeed);

  // Define relation when switch is at position 2 
  // --> Turn backward with variable speed in dependency of the variable input (potentiometer)
  CompareCondition* motorBackwardCondition = new CompareCondition(motorSwitch, OpEQ, Switch3Position::Pos2);
  Relation1to1* relationMotorBackward = new Relation1to1(motorBackwardCondition, motor, inverter);

  // Define relation when switch is at position 0 --> stopped
  CompareCondition* motorStopCondition = new CompareCondition(motorSwitch, OpEQ, Switch3Position::PosMid);
  Relation1to1* 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** ⚠️