#define pin_capacitive_1 10 #define pin_capacitive_2 8 #define pin_capacitive_send 9 #define pin_led_r 1 #define pin_led_b 0 int sensor_cap_value_1 = 0; int sensor_cap_value_2 = 0; int counter = 0; const int numReadings = 10; int readings[2][numReadings]; // the readings from the analog input int readIndex[] = {0, 0}; // the index of the current reading int total[] = {0, 0}; // the running total void setup() { Serial.begin(9600); pinMode(pin_capacitive_1, INPUT); pinMode(pin_capacitive_2, INPUT); pinMode(pin_capacitive_send, OUTPUT); //LEDs pinMode(pin_led_r, OUTPUT); pinMode(pin_led_b, OUTPUT); //Initialize all the readings to 0 for (int i = 0; i < numReadings; i++) { for (int j = 0; j < 2; j++) { readings[j][i] = 0; }//for j }//for i }//void setup int sensorReading(int pin_id) { digitalWrite(pin_capacitive_send, LOW); delay(10); long start_time = micros(); digitalWrite(pin_capacitive_send, HIGH); while(digitalRead(pin_id) == LOW); int sensor_value = micros() - start_time; return sensor_value; }//int sensorReading int sensorAverage(int index, int value) { // subtract the last reading: total[index] = total[index] - readings[index][readIndex[index]]; // read from the sensor: readings[index][readIndex[index]] = value; // add the reading to the total: total[index] = total[index] + readings[index][readIndex[index]]; // advance to the next position in the array: readIndex[index] = readIndex[index] + 1; // if we're at the end of the array wrap around to the beginning: if (readIndex[index] >= numReadings) { readIndex[index] = 0; }//if // calculate the average: int average = total[index] / numReadings; return average; }//int sensorAverage void loop() { //Sensor 1 int sens_value_1 = sensorReading(pin_capacitive_1); int average_1 = sensorAverage(0, sens_value_1); //Sensor 2 int sens_value_2 = sensorReading(pin_capacitive_2); int average_2 = sensorAverage(1, sens_value_2); if(counter%2 == 0) { Serial.print("Sensor 1: "); Serial.print(average_1); Serial.print("\t"); Serial.print("Sensor 2: "); Serial.println(average_2); }//if if(average_1 > 115 || average_2 > 115) { //These values here will depend on humidity, temperature, etc, so could change for every new usage int brightness_1 = map(average_1, min(average_1, 115), max(average_1, 1000), 0, 255); int brightness_2 = map(average_2, min(average_2, 115), max(average_2, 1000), 0, 255); analogWrite(pin_led_b, brightness_1); analogWrite(pin_led_r, brightness_2); } else if(average_1 < 115 && average_2 < 115) { digitalWrite(pin_led_r, LOW); digitalWrite(pin_led_b, LOW); }//else // //Just bare reading - not sure if these values truly make sense // if(counter%1000 == 0) { // sensor_cap_value_1 = analogRead(pin_capacitive_1); // sensor_cap_value_2 = analogRead(pin_capacitive_2); // Serial.print("Sensor 1: "); // Serial.print(sensor_cap_value_1); // Serial.print("\t"); // Serial.print("Sensor 2: "); // Serial.println(sensor_cap_value_2); // }//if delay(10); // arbitrary delay to limit data to serial port counter++; }//void loop