#include <Servo.h>
Servo myservo;
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 12; // Analog input pin that the LED is attached to
const int analogOutPin2 = 13; // Analog input pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
Serial.begin(9600);
myservo.attach(3);
pinMode(analogOutPin2, OUTPUT);
}
void loop() {
sensorValue = analogRead(analogInPin);
Serial.print("data sensor :");
Serial.println(sensorValue);
outputValue = map(sensorValue, 0, 1023, 0, 100);
Serial.print("data maping :");
Serial.println(outputValue);
if (outputValue < 25) {
digitalWrite(analogOutPin2,LOW);
for (int i=100;i>=0;i--){
analogWrite(analogOutPin, i);
delay (100);
}
} else if (outputValue > 25 && outputValue <= 50) {
digitalWrite(analogOutPin,LOW);
for (int i=0;i<5;i++){
digitalWrite(analogOutPin2,HIGH);
delay (150);
digitalWrite(analogOutPin2,LOW);
delay (150);
}
}
else if (outputValue > 50 && outputValue <= 75){
for(int i=0;i<90;i++){
myservo.write(i);
delay (15);
}
}
else {
digitalWrite(analogOutPin,LOW);
digitalWrite(analogOutPin2,LOW);
myservo.write(0);
}
}