Repository navigation
Expand file tree
/
Copy pathDrumControl.ino
More file actions
70 lines (62 loc) · 2.14 KB
/
Copy pathDrumControl.ino
File metadata and controls
70 lines (62 loc) · 2.14 KB
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
/*
This code is based on Arduino examples. It reads a voltage on A0 and writes high on pin 8 when the voltage jumps.
Modifications copyright Tyler Smith
*/
/*
ReadAnalogVoltage
Reads an analog input on pin 0, converts it to voltage, and prints the result to the Serial Monitor.
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/ReadAnalogVoltage
*/
const int readingcount = 10;
float readings[readingcount];
float previous = 0;
int count = 0;
int timer = 0;
bool hitstate = false;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
//Serial.begin(9600);
pinMode(8, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop routine runs over and over again forever:
// unclear how fast the RedBoard actually runs.
void loop() {
timer++;
if(hitstate){
if(timer > 500){
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(8, LOW);
hitstate = false;
timer = 0;
} else {
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(8, HIGH);
}
}
// read the input on analog pin 0:
int time = micros();
int sensorValue = analogRead(A0);
int dur = micros() - time;
//Serial.println(dur); // Tests show an average latency of about 100 microseconds to read the voltage. Pretty small (less than one millisecond).
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// Serial.print(voltage);
// Serial.print(", ");
// Serial.print(previous);
// Serial.print("\n");
// Signal detection seems to be pretty fast - the LED turns on almost immediately.
// I suspect the problem is with the voltage required for the transistor to activate. I'll set up a separate LED to test this.
if((voltage - previous) > 0.3){
digitalWrite(8, HIGH);
hitstate = true;
digitalWrite(LED_BUILTIN, HIGH);
timer=0;
} else {
}
previous = voltage;
}