blob: f99737589d0f90da627105c6444c8043ac80851d (
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
|
// play monophon sounds on the tesla coil
//
// receives uint16s from serial which specify the tone length in microseconds (0 for silence)
int pin1 = 4;
int pin2 = 5;
unsigned int tonelength = 0;
float high_factor = .2;
void setup () {
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
while ( true ) {
if ( Serial.available() > 1 ) {
tonelength = Serial.read() << 8;
tonelength |= Serial.read();
Serial.print("New tonelength: ");
Serial.println(tonelength);
}
if ( tonelength == 0 )
delayMicroseconds(10);
else
beep(tonelength);
}
}
void beep(long usecs) {
high();
delayMicroseconds(usecs*high_factor);
// delayMicroseconds(usecs*on_percentage/100);
low();
delayMicroseconds(usecs*(1-high_factor));
// delayMicroseconds(usecs*(100-on_percentage)/100);
}
void high() {
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
}
void low() {
digitalWrite(pin1, LOW);
digitalWrite(pin2, LOW);
}
long freq (double f) {
// convert frequency to tonelength
return 1000000/f;
}
|