Example 01 ‐ Switch and Lamp - GSTCH/SketchMadeEASY GitHub Wiki

This document describe a simple example, turning a LED on and off by switch. To not use any resistors, the input is set to PullDownInternal. This is the default value as well. If Sketch Made EASY is installed, it can be opened via menu: "File:/Example/SketchMadeEASY/Example/01-SwitchAndLamp".

Schematic

Code

#include <Easy.h>

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

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

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

  //---- Create actuator:   
  // A DigitalOutput knows the value On and Off.   
  Actuator* led = new DigitalOutput(LED_PIN);

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

  // Define relation when switch 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** ⚠️