Touchy Feel lamp with Dimmer

Hello, I’m new to this, and already I’m making stuff hard for my self.
I’m trying to modify the Touchy Feel lamp, with a dimmer, and trying to do so without a potmeter, but using the PMW function analogWrite.

I have been picking ideas from different projects and this is what it looks like so far:

#include <CapacitiveSensor.h>
CapacitiveSensor capSensor = CapacitiveSensor(4, 2);
int threshold = 50;
int ledPin = 9;
int brightness = 0;
int fadeAmount = 20;
bool LEDState = false; // start with assuming LEDs are off

void setup() {
  pinMode(ledPin, OUTPUT);

void loop() {
  long sensorValue = capSensor.capacitiveSensor(30);
  if (sensorValue > 3000) {
    if (LEDState == false) {
      analogWrite(ledPin, brightness); //turn off LEDs
      brightness = brightness + fadeAmount;
    if (brightness == 0 || brightness == 255); {
    fadeAmount = -fadeAmount ;
    LEDState = !LEDState;

My idea is to add a second touch for brightness + and - (+ power on + brightness +)

  • Brightness - to off.
    As of now the light just loops from low to high, then directly down to low and starts the new light loop.
    Any suggestions to clean this mess up?

Please Auto format your code in the IDE, select all of it, use Copy for forum in the IDE and paste it here so that it is in code tags to make it easier to read and copy for examination

The easier you make it to read and copy the code the more likely it is that you will get help

Hello thank you for your feed back