Advertisement
SergioRP

Simulador de Dor Eterna

Jul 5th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.52 KB | None | 0 0
  1. /*
  2. * Sergio C de Toledo Piza
  3. *
  4. * Captador de sensores muito loucos
  5. *
  6. */
  7.  
  8. #define distTrigPin 11
  9. #define distEchoPin 12
  10.  
  11. #define tiltPin 7
  12.  
  13. #define analogX 0
  14. #define analogY 1
  15. #define buttonPress 3
  16.  
  17. #define buzzerPin 6
  18.  
  19. bool active = 0;
  20. int tiltState;
  21.  
  22. typedef struct {
  23.   int x;
  24.   int y;
  25.   int button;
  26. } JOYSTICK;
  27.  
  28.  
  29. // Função para o sensor tilt
  30. int getTilt() {
  31.   return digitalRead(tiltPin);
  32. }
  33.  
  34. void setup() {
  35.   pinMode(distTrigPin, OUTPUT); // Sets the trigPin as an Output
  36.   pinMode(distEchoPin, INPUT); // Sets the echoPin as an Input
  37.  
  38.   pinMode(tiltPin, INPUT); // Sets the tiltPin as an Input
  39.  
  40.   pinMode(buttonPress, INPUT_PULLUP); // Sets the Joystick Button as an Input
  41.  
  42.   pinMode(buzzerPin, OUTPUT);
  43.  
  44.   tiltState = getTilt();
  45.  
  46.   Serial.begin(9600); // Starts the serial communication
  47. }
  48.  
  49.  
  50. void play(int freq, int duration, int wait) {
  51.   tone(buzzerPin, freq); // C4
  52.   delay(duration);
  53.   noTone(buzzerPin);
  54.   delay(wait);
  55. }
  56.  
  57. bool buzzerAscending = 1;
  58. int osc_amount = 0;
  59.  
  60. int max_distance = 0;
  61. int current_distance;
  62. bool enabled = 1;
  63. int disable_time = 0;
  64.  
  65. void loop() {
  66.   JOYSTICK j = getJoystick();
  67.  
  68.   current_distance = getDistance();
  69.  
  70.   if (max_distance < current_distance) max_distance = current_distance;
  71.    
  72.   if (!active && j.button) {
  73.     active = 1;
  74.    
  75.     for (int i = 200; i <= 800; i++)  {
  76.       tone(buzzerPin, i);
  77.       delay(1);
  78.     }
  79.    
  80.     j = getJoystick();
  81.   }
  82.  
  83.   if (active && j.button) {
  84.     for (int i = 800; i >= 200; i--)  {
  85.       tone(buzzerPin, i);
  86.       delay(1);
  87.     }
  88.    
  89.     noTone(buzzerPin);
  90.     active = 0;
  91.     j = getJoystick();
  92.   }
  93.  
  94.   if (active) {
  95.  
  96.      if (getTilt() != tiltState) {
  97.  
  98.       play(262, 200, 50); // C4
  99.       tone(buzzerPin, 196); // G3
  100.       delay(100);
  101.       noTone(buzzerPin);
  102.       delay(50);
  103.       tone(buzzerPin, 196); // G3
  104.       delay(100);
  105.       noTone(buzzerPin);
  106.       delay(50);
  107.       tone(buzzerPin, 220); // A3
  108.       delay(200);
  109.       noTone(buzzerPin);
  110.       delay(50);
  111.       tone(buzzerPin, 196); // G3
  112.       delay(200);
  113.       noTone(buzzerPin);
  114.       delay(400);
  115.       tone(buzzerPin, 247); // B3
  116.       delay(200);
  117.       noTone(buzzerPin);
  118.       delay(50);
  119.       tone(buzzerPin, 262); // C4
  120.       delay(200);
  121.       noTone(buzzerPin);
  122.       delay(50);
  123.      
  124.       tiltState = getTilt();
  125.      }
  126.      
  127.     int osc_lvl = getOscilationLevel(j);
  128.  
  129.     if ((double) (max_distance - current_distance) / max_distance > 0.9) {
  130.       osc_lvl += (double) ((max_distance - current_distance) / max_distance ) * 200;
  131.     }
  132.    
  133.     if (abs(osc_lvl) > 10) {
  134.      
  135.       if (buzzerAscending) {
  136.  
  137.         if (osc_amount > abs(osc_lvl)) {
  138.           buzzerAscending = 0;
  139.         } else {
  140.           osc_amount += 5;
  141.         }
  142.        
  143.       } else {
  144.         if (osc_amount < -1 * abs(osc_lvl)) {
  145.           buzzerAscending = 1;
  146.         } else {
  147.           osc_amount -= 5;
  148.         }
  149.       }
  150.  
  151.  
  152.      
  153.     } else osc_amount = 0;
  154.  
  155.    
  156.  
  157.     tone(buzzerPin, abs(j.x + osc_amount)); //turn the buzzer on
  158.  
  159.      
  160.     Serial.print("max_distance: ");
  161.     Serial.print(max_distance);
  162.     Serial.print(" current_distance: ");
  163.     Serial.print(current_distance);
  164.     Serial.print(" max-current/max: ");
  165.     Serial.println((double) (max_distance - current_distance) / max_distance);
  166.     /*
  167.     Serial.print("Tilt: ");
  168.     Serial.println(getTilt());
  169.  
  170.     Serial.println("Joystick: ");
  171.     Serial.print("\t x: ");
  172.     Serial.println(j.x);
  173.     Serial.print("\t y: ");
  174.     Serial.println(j.y);
  175.     Serial.print("\t button: ");
  176.     Serial.println(j.button);
  177.  
  178.     Serial.println();
  179.     Serial.println();*/
  180.    
  181.   }
  182.  
  183. }
  184.  
  185. int getOscilationLevel(JOYSTICK j) {
  186.   int y = j.y - 514;
  187.   return y / 10;
  188. }
  189.  
  190.  
  191. // Função para o joystick
  192. JOYSTICK getJoystick() {
  193.   JOYSTICK j;
  194.   j.x = analogRead(analogX);
  195.   j.y = analogRead(analogY);
  196.   j.button = !digitalRead(buttonPress);
  197.  
  198.   return j;
  199. }
  200.  
  201.  
  202. // Função para o sensor de distância
  203. int getDistance() {
  204.   long duration;
  205.   int distance;
  206.   // Clears the trigPin
  207.   digitalWrite(distTrigPin, LOW);
  208.   delayMicroseconds(2);
  209.  
  210.   // Sets the trigPin on HIGH state for 10 micro seconds
  211.   digitalWrite(distTrigPin, HIGH);
  212.   delayMicroseconds(10);
  213.   digitalWrite(distTrigPin, LOW);
  214.  
  215.   // Reads the echoPin, returns the sound wave travel time in microseconds
  216.   duration = pulseIn(distEchoPin, HIGH);
  217.  
  218.   // Calculating the distance
  219.   distance= duration*0.034/2;
  220.  
  221.   if (!distance) return getDistance();
  222.   else return distance;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement