Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pitches.h"
- const byte ledPin = 13;
- int vibrationSensor = A0;
- const byte snoozePin = 3;
- byte mood = 100;
- unsigned long lastMoodChange;
- unsigned long lastStep;
- unsigned long lastPlayedSound;
- volatile byte state = LOW;
- unsigned int soundinterval;
- bool happy;
- bool veryhappy;
- bool veryveryhappy;
- //Melodie1
- int melodie1[] = {NOTE_A5, 0, NOTE_A4, 0, NOTE_A6};
- int melodieduur1[] = {8, 8, 8, 8, 8,};
- int aantal1 = 5;
- //Melodie 2
- int melodie2[] = {NOTE_A6, 0, NOTE_A5, 0, NOTE_A4};
- int melodieduur2[] = {8, 8, 8, 8, 8,};
- int aantal2 = 5;
- // Melodie 3
- int melodie3[] = {NOTE_A6, 0, NOTE_A5, 0, NOTE_A4};
- int melodieduur3[] = {8, 8, 8, 8, 8,};
- int aantal3 = 5;
- void setup() {
- pinMode(ledPin, OUTPUT);
- pinMode(snoozePin, INPUT_PULLUP);
- attachInterrupt(digitalPinToInterrupt(snoozePin), snooze, CHANGE);
- attachInterrupt(digitalPinToInterrupt(2), movement, CHANGE);
- pinMode(vibrationSensor, INPUT);
- Serial.begin(9600);
- }
- void loop() {
- digitalWrite(ledPin, state);
- if (mood < 50)
- {
- soundinterval = 5000;
- }
- if (mood < 30)
- {
- soundinterval = 3000;
- }
- if (mood < 10)
- {
- soundinterval = 1000;
- }
- if (lastPlayedSound + soundinterval < millis() && mood < 50)
- {
- for (int noot3 = 0; noot3 < aantal3; noot3++) {
- int nootduur3 = 1000 / melodieduur3[noot3];
- tone(8, melodie3[noot3], nootduur3);
- noTone(8);
- }
- lastPlayedSound = millis();
- }
- if (mood > 60 && happy == false)
- {
- for (int noot1 = 0; noot1 < aantal1; noot1++) {
- int nootduur1 = 1000 / melodieduur1[noot1];
- tone(8, melodie1[noot1], nootduur1);
- noTone(8);
- }
- happy = true;
- }
- if (mood > 80 && veryhappy == false)
- {
- for (int noot1 = 0; noot1 < aantal1; noot1++) {
- int nootduur1 = 1000 / melodieduur1[noot1];
- tone(8, melodie1[noot1], nootduur1);
- noTone(8);
- }
- veryhappy = true;
- }
- if (mood > 90 && veryveryhappy == false)
- {
- for (int noot1 = 0; noot1 < aantal1; noot1++) {
- int nootduur1 = 1000 / melodieduur1[noot1];
- tone(8, melodie1[noot1], nootduur1);
- noTone(8);
- }
- veryveryhappy = true;
- }
- if (mood < 60)
- {
- happy = false;
- }
- if (mood < 80)
- {
- veryhappy = false;
- }
- if (mood < 90)
- {
- veryveryhappy = false;
- }
- if (mood >= 100)
- {
- mood = 100;
- }
- if (lastMoodChange + 800 < millis())
- {
- mood--;
- lastMoodChange = millis();
- }
- while (mood == 0)
- {
- Serial.println("ik ben deaud");
- //play dead sound
- }
- Serial.println(mood);
- }
- void movement() {
- static unsigned long last_interrupt_time = 0;
- unsigned long interrupt_time = millis();
- // If interrupts come faster than 200ms, assume it's a bounce and ignore
- if (millis() > lastStep + 500)
- {
- mood = mood + 2;
- lastStep = millis();
- }
- }
- void snooze() {
- static unsigned long last_interrupt_time = 0;
- unsigned long interrupt_time = millis();
- // If interrupts come faster than 200ms, assume it's a bounce and ignore
- if (interrupt_time - last_interrupt_time > 200)
- {
- state = !state;
- }
- last_interrupt_time = interrupt_time;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement