LED Einbauleuchte dimmen, Ideen?!

Teile ich gerne!

Die kleine LED steht symbolisch für den Strahler (Farlux SF42049, 3W, DC/12V).
Der MOSFET ist ein IRF520.

Strom kommt über ein 12V 3A Netzteil (es sollen ja noch ein paar Strahler mehr werden).

Feedback ist willkommen :wink:

Grüße und Danke nochmal!

// The button that should function like a switch
int const buttonPin = 2;
int button = LOW;
int currState = LOW;
int prevState = LOW;

// The poti used to dimm
int const potPin = A0;
int potVal;

// The LED we want to dimm
int const led1Pin = 11;
int led1Val = 0;

unsigned long currTime = millis();
unsigned long buttonTime = 0;
unsigned long interval = 500;

void setup() {
  // Initialize serial output
  Serial.begin(9600);

  // Initialize pins
  pinMode(buttonPin, INPUT);
  pinMode(led1Pin, OUTPUT);
  digitalWrite(led1Pin, LOW);
}

void loop() {
  // get millis since start
  currTime = millis();

  // read the button
  button = digitalRead(buttonPin);

  // read the poti
  potVal = analogRead(potPin);

  // Check if the button state changed
  if(button == HIGH && prevState == LOW && currTime - buttonTime >= interval) {
    if(currState == HIGH) {
      currState = LOW;
    } else {
      currState = HIGH;
    }
    buttonTime = currTime;
  }
  if(currState == LOW) {
    // Turn off the LED
    digitalWrite(led1Pin, currState);
  } else {
    // Turn on the LED and dimm
    led1Val = map(potVal, 0, 1023, 0, 254);
    analogWrite(led1Pin, led1Val);
  }
  // Some debug output
  Serial.print("potVal: ");
  Serial.print(potVal);
  Serial.print(" led1Val: ");
  Serial.println(led1Val);

  // Save the state off the button
  prevState = button;
}

sketch_lights.ino (1.33 KB)