Capacitive Push/On - Push/Off switch?

I am using the "CapacitiveSensor" library to make a capacitive push on push off switch.
I have managed to make it act as a momentary on off switch for an LED for the sake of testing but I am having trouble where it comes to toggling between states. I have spent some time looking at the "StateChangeDetection" example and trying to figure out how to blend the two sketches together but I am not using states like 1's and 0's to toggle between but a sensor with a set threshold.

In the sketch below I already have it working for momentary push on/off, I just need help configuring it like the "StateChangeDetection" example.

Please help me I am lost!

Sketch:


#include <CapacitiveSensor.h>

const int ledPin = 9;       
const int threshold = 100;   

CapacitiveSensor   cs_4_2 = CapacitiveSensor(4,2);       
void setup(){
   pinMode(ledPin, OUTPUT);
   cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF);     
}
void loop(){
  
    long start = millis();
    long sensor_value =  cs_4_2.capacitiveSensor(30);

   if (sensor_value > threshold) {
    digitalWrite(ledPin, HIGH);
  } 
  else {
    digitalWrite(ledPin,LOW); 
  }     
}

click the MODIFY button in the upper right of the post window.
Highlight all you code.
click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button.
click SAVE (at the bottom).
When you post code on the forum, please make a habit of using the code tags "#" button.

Thank You for the advice.

Well I didn't get any help from anyone on here but I'm going to post what I got working anyway in case someone else is struggling to do this seemingly simple task but I'm a newbie so it might look kind of weird but it is working.

Maybe someone can help me with this one weird thing it does. As I wanted it works as a push on push off capacative button but if I hold it down the LED flashes.

Any ideas how to stop this?

Here is the sketch at this point

Sketch


#include <CapacitiveSensor.h>

const int ledPin = 9;  
CapacitiveSensor sensor = CapacitiveSensor(4,2);

int state = LOW;     
int reading;          
int previous = LOW;   

long time = 0;       
long debounce = 100;  

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

void loop(){
  
 long sensor_input =  sensor.capacitiveSensor(30);

  if (sensor_input >= 7){
   digitalWrite(reading, HIGH);
   delay(100);
  if (reading == HIGH && previous == LOW && millis() - time > debounce);
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      delay(100);
      digitalWrite(reading, LOW);
     

    time = millis();    
  }

  digitalWrite(ledPin, state);

  previous = reading;
}