Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Sergio C de Toledo Piza
- *
- * Captador de sensores muito loucos
- *
- */
- #define distTrigPin 11
- #define distEchoPin 12
- #define tiltPin 7
- #define analogX 0
- #define analogY 1
- #define buttonPress 3
- #define buzzerPin 6
- bool active = 0;
- int tiltState;
- typedef struct {
- int x;
- int y;
- int button;
- } JOYSTICK;
- // Função para o sensor tilt
- int getTilt() {
- return digitalRead(tiltPin);
- }
- void setup() {
- pinMode(distTrigPin, OUTPUT); // Sets the trigPin as an Output
- pinMode(distEchoPin, INPUT); // Sets the echoPin as an Input
- pinMode(tiltPin, INPUT); // Sets the tiltPin as an Input
- pinMode(buttonPress, INPUT_PULLUP); // Sets the Joystick Button as an Input
- pinMode(buzzerPin, OUTPUT);
- tiltState = getTilt();
- Serial.begin(9600); // Starts the serial communication
- }
- void play(int freq, int duration, int wait) {
- tone(buzzerPin, freq); // C4
- delay(duration);
- noTone(buzzerPin);
- delay(wait);
- }
- bool buzzerAscending = 1;
- int osc_amount = 0;
- int max_distance = 0;
- int current_distance;
- bool enabled = 1;
- int disable_time = 0;
- void loop() {
- JOYSTICK j = getJoystick();
- current_distance = getDistance();
- if (max_distance < current_distance) max_distance = current_distance;
- if (!active && j.button) {
- active = 1;
- for (int i = 200; i <= 800; i++) {
- tone(buzzerPin, i);
- delay(1);
- }
- j = getJoystick();
- }
- if (active && j.button) {
- for (int i = 800; i >= 200; i--) {
- tone(buzzerPin, i);
- delay(1);
- }
- noTone(buzzerPin);
- active = 0;
- j = getJoystick();
- }
- if (active) {
- if (getTilt() != tiltState) {
- play(262, 200, 50); // C4
- tone(buzzerPin, 196); // G3
- delay(100);
- noTone(buzzerPin);
- delay(50);
- tone(buzzerPin, 196); // G3
- delay(100);
- noTone(buzzerPin);
- delay(50);
- tone(buzzerPin, 220); // A3
- delay(200);
- noTone(buzzerPin);
- delay(50);
- tone(buzzerPin, 196); // G3
- delay(200);
- noTone(buzzerPin);
- delay(400);
- tone(buzzerPin, 247); // B3
- delay(200);
- noTone(buzzerPin);
- delay(50);
- tone(buzzerPin, 262); // C4
- delay(200);
- noTone(buzzerPin);
- delay(50);
- tiltState = getTilt();
- }
- int osc_lvl = getOscilationLevel(j);
- if ((double) (max_distance - current_distance) / max_distance > 0.9) {
- osc_lvl += (double) ((max_distance - current_distance) / max_distance ) * 200;
- }
- if (abs(osc_lvl) > 10) {
- if (buzzerAscending) {
- if (osc_amount > abs(osc_lvl)) {
- buzzerAscending = 0;
- } else {
- osc_amount += 5;
- }
- } else {
- if (osc_amount < -1 * abs(osc_lvl)) {
- buzzerAscending = 1;
- } else {
- osc_amount -= 5;
- }
- }
- } else osc_amount = 0;
- tone(buzzerPin, abs(j.x + osc_amount)); //turn the buzzer on
- Serial.print("max_distance: ");
- Serial.print(max_distance);
- Serial.print(" current_distance: ");
- Serial.print(current_distance);
- Serial.print(" max-current/max: ");
- Serial.println((double) (max_distance - current_distance) / max_distance);
- /*
- Serial.print("Tilt: ");
- Serial.println(getTilt());
- Serial.println("Joystick: ");
- Serial.print("\t x: ");
- Serial.println(j.x);
- Serial.print("\t y: ");
- Serial.println(j.y);
- Serial.print("\t button: ");
- Serial.println(j.button);
- Serial.println();
- Serial.println();*/
- }
- }
- int getOscilationLevel(JOYSTICK j) {
- int y = j.y - 514;
- return y / 10;
- }
- // Função para o joystick
- JOYSTICK getJoystick() {
- JOYSTICK j;
- j.x = analogRead(analogX);
- j.y = analogRead(analogY);
- j.button = !digitalRead(buttonPress);
- return j;
- }
- // Função para o sensor de distância
- int getDistance() {
- long duration;
- int distance;
- // Clears the trigPin
- digitalWrite(distTrigPin, LOW);
- delayMicroseconds(2);
- // Sets the trigPin on HIGH state for 10 micro seconds
- digitalWrite(distTrigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(distTrigPin, LOW);
- // Reads the echoPin, returns the sound wave travel time in microseconds
- duration = pulseIn(distEchoPin, HIGH);
- // Calculating the distance
- distance= duration*0.034/2;
- if (!distance) return getDistance();
- else return distance;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement