/*
int led = 13;
void setup(){
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop(){
digitalWrite(led,HIGH);
delay(1000);
digitalWrite(led,LOW);
delay (1000);
}
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int led = 13; // the pin that the LED is atteched to
int sensor = 2; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0;
int j = 0;
// variable to store the sensor status (value)
int sound = 12;
void setup() {
myservo.attach(3); // attaches the servo on pin 9 to the servo object
pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
myservo.write(0);
}
void phrase1() {
int k = random(1000,2000);
digitalWrite(led, HIGH);
for (int i = 0; i <= random(100,2000); i++){
myservo.write(pos);
pos=+90;
tone(sound, k+(-i*2));
delay(random(.9,2));
}
digitalWrite(led, LOW);
for (int i = 0; i <= random(100,1000); i++){
myservo.write(pos);
pos=+90;
tone(sound, k + (i * 10));
delay(random(.9,2));
}
}
void phrase2() {
int k = random(1000,2000);
digitalWrite(led, HIGH);
for (int i = 0; i <= random(100,2000); i++){
myservo.write(pos);
pos=-90; // tell servo to go to position in variable 'pos'
//delay(15);
tone(sound, k+(i*2));
delay(random(.9,2));
}
digitalWrite(led, LOW);
for (int i = 0; i <= random(100,1000); i++){
myservo.write(pos);
pos=-90; // tell servo to go to position in variable 'pos'
//delay(15);
tone(sound, k + (-i * 10));
delay(random(.9,2));
}
}
void sweep() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH) {
j++;
if (j % 2 == 0){
phrase1();
Serial.println("Par");
delay(100); // delay 100 milliseconds
}else{
phrase2();
Serial.println("Impar");
delay(100);
}
if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(200); // delay 200 milliseconds
if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
noTone(sound);
}
}
}