Skip to content

Instantly share code, notes, and snippets.

@suadanwar
Last active March 19, 2020 11: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 suadanwar/44386873b3661b95483d55b002c338e5 to your computer and use it in GitHub Desktop.
Save suadanwar/44386873b3661b95483d55b002c338e5 to your computer and use it in GitHub Desktop.
This sample code is for Automatic Soap Dispenser Using Maker UNO tutorial.
#include <Servo.h>
Servo myservo1; // create servo object to control a servo1
Servo myservo2; // create servo object to control a servo2
const int sensorPin = 3; // the number of the IR sensor pin
int sensorState = 0; // variable for reading the IR sensor status
void setup() {
// initialize the IR sensor pin as an input:
pinMode(sensorPin, INPUT);
myservo1.attach(5);
myservo2.attach(6);
}
void loop() {
// read the state of the IR sensor value:
sensorState = digitalRead(sensorPin);
// check if the sensor detects any object. If it is, the sensorState is HIGH:
if (sensorState == HIGH) {
myservo1.write(180); // sets the servo position to 180 degree
myservo2.write(0); // sets the servo position to 0 degree
delay(15);
} else {
myservo1.write(0); // sets the servo position to 0 degree
myservo2.write(180); // sets the servo position to 180 degree
delay(15);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment