Skip to content

Instantly share code, notes, and snippets.

@cmoz
Last active April 30, 2022 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmoz/89c2aa26f3c37998a16ca8624a370c2d to your computer and use it in GitHub Desktop.
Save cmoz/89c2aa26f3c37998a16ca8624a370c2d to your computer and use it in GitHub Desktop.
#include "ADCTouch.h"
#include <Servo.h>
Servo myservo;
int ref0;
int touchPin1 = A7;
int val0constrained = 0;
int val0high = 350;
int val0low = 0;
int threshold = 10;
int pos = 0;
void setup()
{
Serial.begin(9600);
myservo.attach(12);
ref0 = ADCTouch.read(touchPin1, 500);
}
void loop()
{
int value0 = ADCTouch.read(touchPin1);
value0 -= ref0;
Serial.println(value0);
myservo.write(0);
if (value0 > threshold) {
myservo.write(180);
}
delay(100);
}
@cmoz
Copy link
Author

cmoz commented Mar 30, 2022

There are 2 values you might need to change in this code, and the sample button code. First, the val0high = 350; should be changed to approximately the highest value that your sensor puts out on the serial monitor when it is touched. So, run the code, open the monitor, wait for it to register the sensor, (a few seconds) and then touch the sensor. Note the highest number and use that value. For the leaf I used it was 350.

Secondly, threshold = 10; can be modified. This is the number that your sensor must sense above, for it to trigger a reaction. If your sensor is jumping a little around the lower numbers, you might want to make this a little higher value. However, if your sensor is consistently at 0 when it is waiting for touch, then you can safely lower this if you wanted to. Think of this value as the sensitivity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment