Skip to content

Instantly share code, notes, and snippets.

@cmoz
Created March 23, 2022 14:35
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/75890755f2c065358201ba9399f8ed77 to your computer and use it in GitHub Desktop.
Save cmoz/75890755f2c065358201ba9399f8ed77 to your computer and use it in GitHub Desktop.
Hardware Touch sensor pin on board, and servo
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-touch-sensor-servo-motor
*/
#include <Servo.h>
// constants won't change
const int TOUCH_SENSOR_PIN = 7; // Arduino pin connected to touch sensor's pin
const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin
Servo servo; // create servo object to control a servo
// variables will change:
int angle = 0; // the current angle of servo motor
int lastTouchState; // the previous state of touch sensor
int currentTouchState; // the current state of touch sensor
void setup() {
Serial.begin(9600); // initialize serial
pinMode(TOUCH_SENSOR_PIN, INPUT); // set arduino pin to input mode
servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object
servo.write(angle);
currentTouchState = digitalRead(TOUCH_SENSOR_PIN);
}
void loop() {
lastTouchState = currentTouchState; // save the last state
currentTouchState = digitalRead(TOUCH_SENSOR_PIN); // read new state
if(lastTouchState == LOW && currentTouchState == HIGH) {
Serial.println("The sensor is touched");
// change angle of servo motor
if(angle == 0)
angle = 90;
else
if(angle == 90)
angle = 0;
// control servo motor arccoding to the angle
servo.write(angle);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment