aboutsummaryrefslogtreecommitdiffstats
path: root/src/virtual_sqrl/virtual_sqrl.pde
blob: 62e7efaf1ae1fdca53e6ecaeeb74f43dbc21764d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <MsTimer2.h>

/*
 * Virtual SQRLS Program
 *
 * Requires the Metro Arduino libary: http://www.arduino.cc/playground/Code/Metro
 */

int speakerOnePin = 5;
int speakerTwoPin = 6;
int activeSpeaker = speakerOnePin;
int volumePin = 9;
int motionSensorPin = 3;
int defaultVolume=12;
int b = 0;
int bb = 0;
int OCC=0;
int TSL=0;
int TIM=0;
boolean logicLock = 0;


void setup() {
  noInterrupts();
  pinMode(motionSensorPin, INPUT);
  pinMode(speakerOnePin, OUTPUT);
  pinMode(speakerTwoPin, OUTPUT);
  pinMode(volumePin, OUTPUT);
  // Set pins 5&6 to a 62500Hz PWM clock
  TCCR0B = TCCR0B & 0b11111000 | 0x01;
  TCCR1B = TCCR1B & 0b11111000 | 0x01;
  
  TIM = random(200,400);
  
  // Sets a starting volume
  analogWrite(volumePin,defaultVolume);
  
  MsTimer2::set(1000, handleLogic); // 10ms period
  MsTimer2::start();
  
  // interrupt 1 = pin 3
  attachInterrupt(1, motionOn, RISING);
  attachInterrupt(1, motionOff, FALLING);

  interrupts();
}

void loop(){
}

void handleLogic() {
  if(logicLock) {
    return;
  }
  logicLock = 1;
  TSL++;
  if(TSL=TIM){
    caw(3);
  } else if (TSL = 2*TIM) {
    caw(4);
    caw(5);
  } else if (TSL = 2*TIM+30) {
    caw(3);
    caw(4);
    caw(5);
    OCC--;
  } else if (TSL = 2*TIM+50) {
    if(OCC<0){
      caw(4);
      caw(5);
      caw(6);
      caw(7);
      barker(6);
    }
  } else if(TSL % (2*TIM+50) == 0) {
    caw(1);
    caw(2);
    OCC--;
  } 
    
  if(TSL > 10000) {
    TSL = 0;
  }
  logicLock = 0;
}

void chooseSpeaker() {
  if(random(5) == 1) {
    activeSpeaker = speakerTwoPin;
  } else {
    activeSpeaker = speakerOnePin;
  }
}

void motionOn() {
  analogWrite(volumePin, 0);
  if(TSL>200) {
    OCC++;
  }
}

void motionOff() {
  analogWrite(volumePin, defaultVolume);
  TSL=0;
  TIM = random(200,400);
}

// Caw sound. eg, 
// caw(1000);
// delay(200000);
void caw(int ll) {
  for(int j=1800; j<1900; j+=1000/ll) {
    if(j<1816){
      analogWrite(volumePin,(j-1815)*4/5+12);
    }
    if(j>1875){
      analogWrite(volumePin,(1875-j)/4+12);
    }
    b++;
    if (b==10){
      bb=1;
      b=0;
    }
    sawWave(j/8+500*bb+random(10));
    bb=0;
  }
}


// Bark sound. eg, 
// barker(random(3));
void barker(int num) {
 for(int u=0;u<num;u++){
 bark(random(50,80)^2/100);
 delay(random(50000,200000)^2/100000);
 }
}
void bark(int ll) {
  
  for(int j=500; j>200; j-=3000/ll) {
    sawWave(j+random(50));
  }

}

// Generic triangle wave sound
void triangleWave(int len) {
  
  for(int i = 0; i < len; i++)  {
    analogWrite(activeSpeaker, i*255/len);
  }
  for(int i = 0; i < len; i++)  {
    analogWrite(activeSpeaker, 255 - i*255/len);
  }
}

// Generic saw wave sound
void sawWave(int len) {
  for(int i = 0; i < len; i++)  {
    analogWrite(activeSpeaker, i);
  }
}