Leon Chu
Published © GPL3+

Magnetic Multi Switch for Desk Lights

Camouflage visible switches with simple elegant decor to control multiple lights and devices using pointing orientation.

EasyShowcase (no instructions)3 hours96

Things used in this project

Hardware components

General Purpose Transistor NPN
General Purpose Transistor NPN
×4
RGB variable color led strip
×1
Relay (generic)
×1
LED light strip
×1
3D Magnetic Sensor 2Go
Infineon 3D Magnetic Sensor 2Go
×1
Neodymium Magnets,
×1

Story

Read more

Schematics

Breadboard

Wire 77grk1sdtu

Code

Magnetic Light Switcher

C/C++
Arduino Sketch
#include <Tle493d_w2b6.h>

#define FADE_LIGHT_PIN 8
#define SW1_PIN 4
#define SW2_PIN 5
#define SW3_PIN 2
#define SW4_PIN 3
#define LED_INDICATOR_PIN 14


float  PROXIMITY = 1.5;

Tle493d_w2b6 Tle493dMagnetic3DSensor = Tle493d_w2b6();

float azimuth;

void setup() {
  Serial.begin(9600);
  while (!Serial);
  Tle493dMagnetic3DSensor.begin();
  Tle493dMagnetic3DSensor.begin();
  //Tle493dMagnetic3DSensor.enableTemp();
  pinMode(LED_INDICATOR_PIN, OUTPUT);
  pinMode (FADE_LIGHT_PIN,OUTPUT);  
  pinMode (SW1_PIN,OUTPUT);  
  pinMode (SW2_PIN,OUTPUT);  
}


void DebugInfo() {
  Serial.print(Tle493dMagnetic3DSensor.getX());
  Serial.print("\t");
  Serial.print(Tle493dMagnetic3DSensor.getY());
  Serial.print("\t");
  Serial.print(Tle493dMagnetic3DSensor.getZ());
  Serial.print("\t");

  Serial.print(Tle493dMagnetic3DSensor.getNorm());
  Serial.print("\t");
  Serial.print(Tle493dMagnetic3DSensor.getAzimuth());
  Serial.print("\t");
  Serial.print(Tle493dMagnetic3DSensor.getPolar());

  Serial.print("\t");
  Serial.println(Tle493dMagnetic3DSensor.getTemp());
}

void SwitchOnAngleRange( int switchPinNum, float startAngle, float endAngle) {
    if (azimuth  >= startAngle && azimuth < endAngle) {
      digitalWrite(switchPinNum, HIGH);
    } else {
      digitalWrite(switchPinNum, LOW);
    }
}


void loop() {

  Tle493dMagnetic3DSensor.updateData();
  digitalWrite(LED_INDICATOR_PIN, LOW);
  digitalWrite(SW1_PIN, LOW);
  digitalWrite(SW2_PIN, LOW);
  digitalWrite(SW3_PIN, LOW);
  analogWrite(FADE_LIGHT_PIN, 0);

  //DebugInfo();
  //delay(5);
  
  while (Tle493dMagnetic3DSensor.getNorm() > PROXIMITY)  {  
    Tle493dMagnetic3DSensor.updateData();
 
      
    digitalWrite(LED_INDICATOR_PIN, HIGH);
    azimuth = (3.1415 - Tle493dMagnetic3DSensor.getAzimuth()) / 6.283 * 360; // magnet heading in degrees
  
    // Fade light angle range
    if (azimuth  >= 10 && azimuth < 180) {
      float fadeValue = azimuth / 180 * 1024;
      fadeValue = constrain(fadeValue, 0, 1023);
      analogWrite(FADE_LIGHT_PIN, fadeValue);
      continue;
    }
  
    // Switch on port 1 PWM LED Light
    if (azimuth  >= 340 || azimuth < 10) {
        // Turn all on
        digitalWrite(SW1_PIN, HIGH);
        digitalWrite(SW2_PIN, HIGH);
        digitalWrite(SW3_PIN, HIGH);
        analogWrite(FADE_LIGHT_PIN, 1023);
    }

  SwitchOnAngleRange(SW1_PIN, 180, 245);
  SwitchOnAngleRange(SW2_PIN, 235, 300);
  SwitchOnAngleRange(SW3_PIN, 290, 350);
   
   
  }
}

Credits

Leon Chu

Leon Chu

8 projects • 8 followers
Indie Developer / Artist / Lifelong learner

Comments