Last active
September 20, 2018 08:59
-
-
Save atifahsuad/47dc677022a4e57c8180f0bb4f6c03ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This example code is for rain detector's tutorial. | |
Product page: | |
Maker UNO : https://www.cytron.io/p-maker-uno | |
Rain Sensor Module : https://www.cytron.io/p-sn-rain-mod | |
TowerPro SG90 Micro Servo : https://www.cytron.io/p-sg90 | |
Created by: | |
20/09/18 Idris Zainal Abidin and Suad Anwar, Cytron Technologies | |
*/ | |
#include <Servo.h> // Include the library for the servo | |
#define RAIN_SENSOR A0 | |
#define SERVO1_PIN 10 | |
#define SERVO2_PIN 11 | |
Servo myservo1; // Create servo object to control a servo | |
Servo myservo2; | |
int rainAdc = 0; | |
boolean rainState = false; | |
boolean previousRainState = false; | |
boolean changeState = false; | |
boolean moveServo = false; | |
int servoLoop = 0; | |
int pos1 = 0; | |
int pos2 = 0; | |
long currentMillis = 0; | |
long previousMillis = 0; | |
int interval = 2000; | |
void setup() | |
{ | |
pinMode(RAIN_SENSOR, INPUT_PULLUP); | |
Serial.begin(9600); | |
myservo1.attach(SERVO1_PIN); | |
myservo2.attach(SERVO2_PIN); | |
} | |
void loop() | |
{ | |
rainAdc = analogRead(RAIN_SENSOR); | |
if (rainAdc < 800) { | |
if (rainState == false && changeState == false) { | |
changeState = true; | |
previousMillis = millis(); | |
} | |
else if (rainState == true && changeState == true) { | |
changeState = false; | |
} | |
} | |
else { | |
if (rainState == true && changeState == false) { | |
changeState = true; | |
previousMillis = millis(); | |
} | |
else if (rainState == false && changeState == true) { | |
changeState = false; | |
} | |
} | |
if (changeState == false) { | |
previousMillis = millis(); | |
} | |
if (changeState == true) { | |
currentMillis = millis(); | |
if (currentMillis - previousMillis > interval) { | |
changeState == false; | |
if (rainState == false) { | |
rainState = true; | |
moveServo = true; | |
} | |
else { | |
rainState = false; | |
moveServo = true; | |
} | |
} | |
} | |
if (moveServo == true) { | |
if (rainState == true) { | |
pos1 = 0; | |
pos2 = 90; | |
for (servoLoop = 0; servoLoop < 90; servoLoop++) { | |
myservo1.write(pos1++); | |
myservo2.write(pos2--); | |
delay(30); | |
} | |
myservo1.write(103); | |
myservo2.write(0); | |
moveServo = false; | |
} | |
else { | |
pos1 = 103; | |
pos2 = 0; | |
for (servoLoop = 0; servoLoop < 90; servoLoop++) { | |
myservo1.write(pos1--); | |
myservo2.write(pos2++); | |
delay(30); | |
} | |
myservo1.write(0); | |
myservo2.write(90); | |
moveServo = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment