still new to adruino and am experimenting with a capacitive sensor to control the brightness of an LED.
it works fine and the LED lights up or dims with the sensor reading.
now i wanted to change the programming so that the LED brightness stays at the highest level of the sensor readings. in a way like this it is only possible to make the LED brighter and it stays at that level.
if a certain level is reached or maybe with a reset button the LED should be resettable to 0 though.
among other things, i guess i need a to compare the newest sensor value to the last sensor value.
here is my code so far (sorry the coding i s a bit complicated since i needed to add the capacitive sensor library and i added a smoothing average to the sensor readings):
CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2);
int ledPin = 9;
const int numReadings = 4;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0;
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
long start = millis();
long sensor = cs_4_2.capacitiveSensor(30);
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = sensor;
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
if (index >= numReadings)
index = 0;
average = total / numReadings;
int val = average;
val = constrain(val, 160, 7000);
int ledLevel = map(val, 7000, 160, 255, 0);
Serial.print(millis() - start);
Serial.print(sensor); // print sensor output 1
many thanks in advance!!!