Hiya Forum! I'm trying to make a basic capacitive sensor, but I'm encounter some errors in the code. I copied the code from this instructable: https://www.instructables.com/How-To-Use-Touch-Sensors-With-Arduino/ and installed the capacitivesensor library, but it's coming up with a lot of errors in the sketch. Any help deciphering would be great as I'm not experienced in C++. Here's the sketch & errors:
#include <CapacitiveSensor.h>
int led = 42; //change '42' to any desired pin...long time = 0;
int state = HIGH;
boolean yes;
boolean previous = false;
int debounce = 200;
CapacitiveSensor cs_4_2 = CapacitiveSensor(4,2); // 10M resistor between pins 4 & 2, pin 2 is sensor pin, add a wire and or foil if desired// To add more sensors...//CapacitiveSensor cs_4_6 = CapacitiveSensor(4,6); // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil//CapacitiveSensor cs_4_8 = CapacitiveSensor(4,8); // 10M resistor between pins 4 & 8, pin 8 is sensor pin, add a wire and or foilvoidsetup()
{
cs_4_2.set_CS_AutocaL_Millis(0xFFFFFFFF); //Calibrate the sensor...
pinMode(led, OUTPUT);
}
voidloop()
{
long total1 = cs_4_2.capacitiveSensor(30);
if (total1 > 60){yes = true;}
else {yes = false;}
// to toggle the state of state
if(yes == true && previous == false && millis() - time>debounce){
if(state == LOW){
state = HIGH;
}
else
state = LOW;
time = millis();
}
digitalWrite(led, state);
previous = yes;
Serial.println(millis()-time);
delay(10);
}
C:\Users\liamj\AppData\Local\Temp\.arduinoIDE-unsaved20241027-9808-l04jow.yl7mk\sketch_nov27a\sketch_nov27a.ino:12:1: error: expected unqualified-id before '{' token
{
^
C:\Users\liamj\AppData\Local\Temp\.arduinoIDE-unsaved20241027-9808-l04jow.yl7mk\sketch_nov27a\sketch_nov27a.ino:17:12: error: expected constructor, destructor, or type conversion before ';' token
voidloop()
^
C:\Users\liamj\AppData\Local\Temp\.arduinoIDE-unsaved20241027-9808-l04jow.yl7mk\sketch_nov27a\sketch_nov27a.ino: In function 'int voidloop()':
C:\Users\liamj\AppData\Local\Temp\.arduinoIDE-unsaved20241027-9808-l04jow.yl7mk\sketch_nov27a\sketch_nov27a.ino:25:56: error: 'time' was not declared in this scope
if(yes == true && previous == false && millis() - time>debounce){
^~~~
C:\Users\liamj\AppData\Local\Temp\.arduinoIDE-unsaved20241027-9808-l04jow.yl7mk\sketch_nov27a\sketch_nov27a.ino:25:56: note: suggested alternative: 'tone'
if(yes == true && previous == false && millis() - time>debounce){
^~~~
tone
C:\Users\liamj\AppData\Local\Temp\.arduinoIDE-unsaved20241027-9808-l04jow.yl7mk\sketch_nov27a\sketch_nov27a.ino:40:31: error: 'time' was not declared in this scope
Serial.println(millis()-time);
^~~~
C:\Users\liamj\AppData\Local\Temp\.arduinoIDE-unsaved20241027-9808-l04jow.yl7mk\sketch_nov27a\sketch_nov27a.ino:40:31: note: suggested alternative: 'tone'
Serial.println(millis()-time);
^~~~
tone
exit status 1
Compilation error: expected unqualified-id before '{' token
Compilation error: expected unqualified-id before '{' token
Thanks so much!