Help with a capacitive sensor to turn on LEDs using Arduino UNO ATmega 328?

Hello community! I’m currently working on a project that uses capacitors to control LEDs. Basically, I have a capacitor made out of aluminum foil and a box. When someone places their hand into the box, the capacitance changes and the LEDs are lit. I have the circuit properly set up, but I’m struggling with the software of this project. I’ve looked around the forums, but I’ve yet to find something that covers this topic. My current code gives no errors, but I just wanted to confirm that it would do what I want it to do. As the subject suggests, I’m using an Arduino UNO with an ATmega 328 chip. Using a multi-meter, I’ve determined the capacitance of my box to be approximately .095mF. Any feedback would be extremely helpful. If needed, I can provide a schematic of the circuit. Thank you all for your help!


/*This code reads voltage discharge from a capacitor and then turns on LEDs if the capacitance changes*/

/*Pin Declaration*/
const int sensorpin= PCINT22;
const int charge_pin = 12;
const int discharge_pin = 11;
const int LED = 13;

/*Declaration of which pins will be outputs or inputs*/
void setup(){
  Serial.begin(9600);               //Helps keep track of loops and useful for debugging
  pinMode(LED, OUTPUT);
  pinMode(charge_pin, OUTPUT);
  digitalWrite(charge_pin, LOW);

/*Primary Loop*/
void loop(){
 /*Order of functions in which they will occur*/
  unsigned long sensor_func();           //Get first reading
  unsigned long move_time();             //Move first time to new variable 
  unsigned long sensor_func();           //Get second reading 
  float calc();                          //Find the time difference 
  void led_control();
}                                        //Repeat until disconnected 

/*Function Definitions*/

/*Function 1*/
unsigned long sensor_func(){              //Function used to obtain times

  int i;                                  //Variables used to calculate average time
  unsigned long start_time;
  unsigned long end_time;
  unsigned long avg_time = 0;
  unsigned long total_time;
  for (i = 0; i < 1000; i++){             //Repeat readings 1000 times
  digitalWrite(charge_pin, HIGH);         //Capacitor charges
  while(analogRead(sensorpin) < 1023){    //This tells it to keep charging it until it has reached max voltage
  start_time = millis();                  //Timer starts to measure discharge
  digitalWrite(charge_pin, LOW);          //Capacitor begins to discharge
  pinMode(discharge_pin, OUTPUT);
  digitalWrite(discharge_pin, LOW);
  while(analogRead(sensorpin) > 0){       //Wait for the capacitor to completely discharge
  end_time = millis();                    //Timer stops once capacitor is discharged
  pinMode(discharge_pin, INPUT);          //Discharge pin is reset 
  total_time = end_time - start_time;     //Total time it took to discharge the capacitor
  avg_time = avg_time + total_time;       //Time readings 
  avg_time = avg_time / 1000;             //Average time over 1000 samples
  return avg_time;

/*Function 2*/
unsigned long move_time(unsigned long avg_time){                //Function used to move average time to old average time 
  unsigned long avg_old_time;
  avg_old_time = avg_time;
  return avg_old_time;                    //Average time was moved to a new variable 

/*Function 3*/
float calc(unsigned long avg_time, unsigned long avg_old_time){                              //Function used to calculate time differences  
  float time_difference;
  time_difference = avg_time - avg_old_time;  // Difference is calculated
  return time_difference;

/*Function 4*/
void led_control(float time_difference){                        //Function used to control the LEDs in the circuit

if (time_difference > 0){                                       //Check to see if capacitance has changed LEDs turn on if it has
  digitalWrite(LED, HIGH);
  Serial.println("LEDs are on hooray :3");

else {
  digitalWrite(LED, LOW);                                      //LEDs stay off
  Serial.println("LEDs are off :(");