Wenn man in Deinem Programm ein paar Dinge ändert, kommt tatsächlich PWM raus. Die Zeitspanne beträgt 408 ms, weshalb eine LED für uns Menschen eher als blinkend wahrgenommen wird, Dimmen müßte schneller sein.
Also PWM ist es, aber Dimmen eher nicht.
const byte sensorPin = A0; // select the input pin for the potentiometer
unsigned int sensorValue; // variable to store the value coming from the sensor
const byte dimled = 8; // pin led to dime
unsigned int a; // hilfsvariable berechnung
unsigned int b; // prozentualer wert von senorValue (100% = 50)
unsigned int dauer;
void setup()
{
Serial.begin(9600);
// declare the ledPin as an OUTPUT:
pinMode(dimled, OUTPUT);
}
void loop()
{
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
a = sensorValue * 10;
b = (100 / 50) * a; // prozent Helligkeit
dauer = (b / 100) * 2;
Serial.print("sensorValue: "); Serial.print(sensorValue);
Serial.print("\ta: "); Serial.print(a);
Serial.print("\tb: "); Serial.print(b);
Serial.print("\tdauer: "); Serial.print(dauer);
Serial.println();
digitalWrite(dimled, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(dauer);
// turn the ledPin off:
digitalWrite(dimled, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(1023 * 2 / 10 * 2 - dauer);
}
Mit delayMicroseconds kommst Du näher an Dein Ziel, nur Serial geht dann nicht mehr, weil zu langsam:
const byte sensorPin = A0; // select the input pin for the potentiometer
unsigned int sensorValue; // variable to store the value coming from the sensor
const byte dimled = 8; // pin led to dime
unsigned int a; // hilfsvariable berechnung
unsigned int b; // prozentualer wert von senorValue (100% = 50)
unsigned int dauer;
void setup()
{
// declare the ledPin as an OUTPUT:
pinMode(dimled, OUTPUT);
}
void loop()
{
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
a = sensorValue * 10;
b = (100 / 50) * a; // prozent Helligkeit
dauer = (b / 100) * 2;
digitalWrite(dimled, HIGH);
// stop the program for <sensorValue> milliseconds:
delayMicroseconds(dauer);
// turn the ledPin off:
digitalWrite(dimled, LOW);
// stop the program for for <sensorValue> milliseconds:
delayMicroseconds(1023 * 2 / 10 * 2 - dauer);
}
Ob Deine Berechnungen zum Kommentar passen, habe ich mir nicht angesehen ![]()