Example 03 ‐ Dimming LED with Switch - GSTCH/SketchMadeEASY GitHub Wiki

This document describe a dimming a LED by potentiometer with an on/off switch. If Sketch Made EASY is installed, it can be opened via menu: "File:/Example/SketchMadeEASY/Example/03-DimmingLedWithSwitch".

Schematic

Code

#include <Easy.h>

//*****************************************************************
#define SWITCH_PIN 39
#define LED_PIN 6
#define POTI_PIN A0

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

  //** Create actuator:
  // A variable output has a range of 0...255. It uses a PWM pin.
  Actuator* led = new VariableOutput(LED_PIN);

  //** Create input:
  // Switch as input for condition. A switch with two positions 
  // knows the value On and Off.
  Input* switchOnOff = new Switch2Position(SWITCH_PIN);

  // Input for the actuator is a variable input has a range 
  // of 0...1023. This is an analog pin. the range is autmatic mapped.
  Input* poti = new VariableInput(POTI_PIN);

  //** Define logic with conditions and relations
  // Define relation when button is on
  Condition* conditionSwitchOn = new CompareCondition(switchOnOff, OpEQ, Switch2Position::On);
  Relation* switchOnRelation = new Relation1to1(conditionSwitchOn, led, poti);

  // Define relation when button is off
  Condition* conditionSwitchOff = new CompareCondition(switchOnOff, OpEQ, Switch2Position::Off);
  Relation* switchOffRelation = new Relation1to1(conditionSwitchOff, led, 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** ⚠️