Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.23 KB | None | 0 0
  1. #include "pitches.h"
  2.  
  3. const byte ledPin = 13;
  4. int vibrationSensor = A0;
  5.  
  6. const byte snoozePin = 3;
  7.  
  8. byte mood = 100;
  9. unsigned long lastMoodChange;
  10. unsigned long lastStep;
  11. unsigned long lastPlayedSound;
  12.  
  13. volatile byte state = LOW;
  14.  
  15. unsigned int soundinterval;
  16.  
  17. bool happy;
  18. bool veryhappy;
  19. bool veryveryhappy;
  20.  
  21. //Melodie1
  22. int melodie1[] = {NOTE_A5, 0, NOTE_A4, 0, NOTE_A6};
  23. int melodieduur1[] = {8, 8, 8, 8, 8,};
  24. int aantal1 = 5;
  25.  
  26. //Melodie 2
  27. int melodie2[] = {NOTE_A6, 0, NOTE_A5, 0, NOTE_A4};
  28. int melodieduur2[] = {8, 8, 8, 8, 8,};
  29. int aantal2 = 5;
  30.  
  31. // Melodie 3
  32. int melodie3[] = {NOTE_A6, 0, NOTE_A5, 0, NOTE_A4};
  33. int melodieduur3[] = {8, 8, 8, 8, 8,};
  34. int aantal3 = 5;
  35.  
  36. void setup() {
  37.   pinMode(ledPin, OUTPUT);
  38.  
  39.   pinMode(snoozePin, INPUT_PULLUP);
  40.   attachInterrupt(digitalPinToInterrupt(snoozePin), snooze, CHANGE);
  41.   attachInterrupt(digitalPinToInterrupt(2), movement, CHANGE);
  42.  
  43.  
  44.   pinMode(vibrationSensor, INPUT);
  45.  
  46.   Serial.begin(9600);
  47. }
  48.  
  49. void loop() {
  50.   digitalWrite(ledPin, state);
  51.  
  52.  
  53.   if (mood < 50)
  54.   {
  55.     soundinterval = 5000;
  56.   }
  57.  
  58.   if (mood < 30)
  59.   {
  60.     soundinterval = 3000;
  61.   }
  62.  
  63.   if (mood < 10)
  64.   {
  65.     soundinterval = 1000;
  66.   }
  67.  
  68.  
  69.   if (lastPlayedSound + soundinterval < millis() && mood < 50)
  70.   {
  71.     for (int noot3 = 0; noot3 < aantal3; noot3++) {
  72.       int nootduur3 = 1000 / melodieduur3[noot3];
  73.       tone(8, melodie3[noot3], nootduur3);
  74.       noTone(8);
  75.     }
  76.     lastPlayedSound = millis();
  77.   }
  78.  
  79.  
  80.   if (mood > 60 && happy == false)
  81.   {
  82.     for (int noot1 = 0; noot1 < aantal1; noot1++) {
  83.       int nootduur1 = 1000 / melodieduur1[noot1];
  84.       tone(8, melodie1[noot1], nootduur1);
  85.       noTone(8);
  86.     }
  87.     happy = true;
  88.   }
  89.  
  90.  
  91.  
  92.   if (mood > 80 && veryhappy == false)
  93.   {
  94.     for (int noot1 = 0; noot1 < aantal1; noot1++) {
  95.       int nootduur1 = 1000 / melodieduur1[noot1];
  96.       tone(8, melodie1[noot1], nootduur1);
  97.       noTone(8);
  98.     }
  99.     veryhappy = true;
  100.   }
  101.  
  102.  
  103.  
  104.   if (mood > 90 && veryveryhappy == false)
  105.   {
  106.     for (int noot1 = 0; noot1 < aantal1; noot1++) {
  107.       int nootduur1 = 1000 / melodieduur1[noot1];
  108.       tone(8, melodie1[noot1], nootduur1);
  109.       noTone(8);
  110.     }
  111.     veryveryhappy = true;
  112.   }
  113.  
  114.  
  115.   if (mood < 60)
  116.   {
  117.     happy = false;
  118.   }
  119.  
  120.   if (mood < 80)
  121.   {
  122.     veryhappy = false;
  123.   }
  124.  
  125.   if (mood < 90)
  126.   {
  127.     veryveryhappy = false;
  128.   }
  129.  
  130.  
  131.   if (mood >= 100)
  132.   {
  133.     mood = 100;
  134.   }
  135.  
  136.  
  137.   if (lastMoodChange + 800 < millis())
  138.   {
  139.     mood--;
  140.     lastMoodChange = millis();
  141.   }
  142.  
  143.  
  144.  
  145.   while (mood == 0)
  146.   {
  147.     Serial.println("ik ben deaud");
  148.     //play dead sound
  149.   }
  150.  
  151.   Serial.println(mood);
  152.  
  153. }
  154.  
  155.  
  156. void movement() {
  157.   static unsigned long last_interrupt_time = 0;
  158.   unsigned long interrupt_time = millis();
  159.   // If interrupts come faster than 200ms, assume it's a bounce and ignore
  160.  
  161.   if (millis() > lastStep + 500)
  162.   {
  163.     mood = mood + 2;
  164.     lastStep = millis();
  165.   }
  166. }
  167.  
  168.  
  169. void snooze() {
  170.   static unsigned long last_interrupt_time = 0;
  171.   unsigned long interrupt_time = millis();
  172.   // If interrupts come faster than 200ms, assume it's a bounce and ignore
  173.  
  174.   if (interrupt_time - last_interrupt_time > 200)
  175.   {
  176.     state = !state;
  177.   }
  178.   last_interrupt_time = interrupt_time;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement