Uživatel:ThermoDream: Porovnání verzí
Značka: editace z Vizuálního editoru |
|||
Řádek 35: | Řádek 35: | ||
CINCH kabel 3.5mm + rozdvojka | CINCH kabel 3.5mm + rozdvojka | ||
− | === Odkazy na knihovnu arduina | + | === Odkazy na knihovnu arduina === |
+ | |||
+ | === Vlastní zdrojový kód === | ||
+ | #include <TinkerKit.h> | ||
+ | |||
+ | #include <TaskScheduler.h> | ||
+ | |||
+ | #include "DHT.h" | ||
+ | |||
+ | |||
+ | void dhtReadCallback(); | ||
+ | |||
+ | void sampleCallback(); | ||
+ | |||
+ | |||
+ | DHT dht(3, DHT22); //DHT22 connected to pin 3 | ||
+ | |||
+ | |||
+ | Task taskDht(15000, TASK_FOREVER, &dhtReadCallback); | ||
+ | |||
+ | Task taskSample(1, TASK_FOREVER, &sampleCallback); | ||
+ | |||
+ | |||
+ | Scheduler ts; | ||
+ | |||
+ | |||
+ | int lastState = 0; | ||
+ | |||
+ | |||
+ | void dhtReadCallback() { | ||
+ | |||
+ | // Read temperature as Celsius (the default) | ||
+ | |||
+ | float t = dht.readTemperature(); | ||
+ | |||
+ | // Check if any reads failed and exit early (to try again). | ||
+ | |||
+ | if (!isnan(t)) { | ||
+ | |||
+ | int currentState = map(t, 15.00, 35.00, 1, 5); | ||
+ | |||
+ | if (lastState != currentState) { | ||
+ | |||
+ | Serial.println(currentState); | ||
+ | |||
+ | lastState = currentState; | ||
+ | |||
+ | } | ||
+ | |||
+ | Serial.println(t); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | float bassFilter(float sample) { | ||
+ | |||
+ | static float xv[3] = {0, 0, 0}, yv[3] = {0, 0, 0}; | ||
+ | |||
+ | xv[0] = xv[1]; xv[1] = xv[2]; | ||
+ | |||
+ | xv[2] = (sample) / 3.f; // change here to values close to 2, to adapt for stronger or weeker sources of line level audio | ||
+ | |||
+ | |||
+ | |||
+ | yv[0] = yv[1]; yv[1] = yv[2]; | ||
+ | |||
+ | yv[2] = (xv[2] - xv[0]) | ||
+ | |||
+ | + (-0.7960060012f * yv[0]) + (1.7903124146f * yv[1]); | ||
+ | |||
+ | return yv[2]; | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | float envelopeFilter(float sample) { //10hz low pass | ||
+ | |||
+ | static float xv[2] = {0, 0}, yv[2] = {0, 0}; | ||
+ | |||
+ | xv[0] = xv[1]; | ||
+ | |||
+ | xv[1] = sample / 50.f; | ||
+ | |||
+ | yv[0] = yv[1]; | ||
+ | |||
+ | yv[1] = (xv[0] + xv[1]) + (0.9875119299f * yv[0]); | ||
+ | |||
+ | return yv[1]; | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | // 1.7 - 3.0hz Single Pole Bandpass IIR Filter | ||
+ | |||
+ | float beatFilter(float sample) { | ||
+ | |||
+ | static float xv[3] = {0, 0, 0}, | ||
+ | |||
+ | yv[3] = {0, 0, 0}; | ||
+ | |||
+ | xv[0] = xv[1]; xv[1] = xv[2]; | ||
+ | |||
+ | xv[2] = sample / 2.7f; | ||
+ | |||
+ | yv[0] = yv[1]; yv[1] = yv[2]; | ||
+ | |||
+ | yv[2] = (xv[2] - xv[0]) | ||
+ | |||
+ | + (-0.7169861741f * yv[0]) + (1.4453653501f * yv[1]); | ||
+ | |||
+ | return yv[2]; | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | #define TRESHOLD 60.0 | ||
+ | |||
+ | |||
+ | int map2(float value, float min, float max) { | ||
+ | |||
+ | if (value<min) return 0; | ||
+ | |||
+ | if (value>max) return 255; | ||
+ | |||
+ | return map(value,min,max,0,255); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | void sampleCallback() { | ||
+ | |||
+ | float sample = (float)analogRead(0) - 503.f; | ||
+ | |||
+ | float value = bassFilter(sample); | ||
+ | |||
+ | if (value < 0)value = -value; | ||
+ | |||
+ | value = envelopeFilter(value); | ||
+ | |||
+ | if ((taskSample.getRunCounter() % 20) == 0) { | ||
+ | |||
+ | value = beatFilter(value); | ||
+ | |||
+ | |||
+ | if (value < 0)value = 0; | ||
+ | |||
+ | if (value > TRESHOLD)value = TRESHOLD; | ||
+ | |||
+ | |||
+ | int pwm = map(value, 0, TRESHOLD, 0, 255); | ||
+ | |||
+ | analogWrite(O0, pwm); | ||
+ | |||
+ | |||
+ | analogWrite(9, map2(value, 0, TRESHOLD * 0.3333)); | ||
+ | |||
+ | analogWrite(6, map2(value, TRESHOLD * 0.3333, TRESHOLD * 0.6666)); | ||
+ | |||
+ | analogWrite(5, map2(value, TRESHOLD * 0.6666, TRESHOLD)); | ||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | // defines for setting and clearing register bits | ||
+ | |||
+ | #ifndef cbi | ||
+ | |||
+ | #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) | ||
+ | |||
+ | #endif | ||
+ | |||
+ | #ifndef sbi | ||
+ | |||
+ | #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) | ||
+ | |||
+ | #endif | ||
+ | |||
+ | |||
+ | void setup() { | ||
+ | |||
+ | // Set ADC to 77khz, max for 10bit | ||
+ | |||
+ | sbi(ADCSRA, ADPS2); | ||
+ | |||
+ | cbi(ADCSRA, ADPS1); | ||
+ | |||
+ | cbi(ADCSRA, ADPS0); | ||
+ | |||
+ | |||
+ | pinMode(O0, OUTPUT); | ||
+ | |||
+ | analogWrite(O0, 0); | ||
+ | |||
+ | |||
+ | Serial.begin(115200); | ||
+ | |||
+ | dht.begin(); | ||
+ | |||
+ | ts.init(); | ||
+ | |||
+ | ts.addTask(taskDht); | ||
+ | |||
+ | ts.addTask(taskSample); | ||
+ | |||
+ | taskDht.enable(); | ||
+ | |||
+ | taskDht.forceNextIteration(); | ||
+ | |||
+ | taskSample.enable(); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | void loop() { | ||
+ | |||
+ | ts.execute(); | ||
+ | |||
+ | } | ||
+ | |||
=== Popis vývoje a konečné verze vlastního produktu === | === Popis vývoje a konečné verze vlastního produktu === |
Verze z 30. 3. 2018, 18:23
Obsah
Party teploměr
Tým
Matěj Čermák
Jakub Hein
Julie Klimentová
Darya Tsitová
Popis
Teploměr, co měří teplotu v místnosti, kde se odehrává party. Čím nižší teplota, tím vyšší rychlost hudby, aby se lidé zahřáli. To samé platí pro opačný případ - čím vyšší teplota, tím je hudba klidnější. Je nutné otestovat citlivost stupnice - podle jak vysokého teplotního skoku (0,5/0,25/0,10) se má hudba měnit.Teploměr by mohl fungovat jako DJ modul a být napojen na světla - v naší mini verzi by se mohla rozsvítit různá LED světýlka.
Použitý hardware a software
Arduino komponenty
Arduino UNO
TinkerKit Sensor Shield
TinkerKit PowerLED
BreadBoard
Teploměr DHT22
Green LED
Red LED
Yellow LED
Jiné komponenty
CINCH kabel 3.5mm + rozdvojka
Odkazy na knihovnu arduina
Vlastní zdrojový kód
- include <TinkerKit.h>
- include <TaskScheduler.h>
- include "DHT.h"
void dhtReadCallback();
void sampleCallback();
DHT dht(3, DHT22); //DHT22 connected to pin 3
Task taskDht(15000, TASK_FOREVER, &dhtReadCallback);
Task taskSample(1, TASK_FOREVER, &sampleCallback);
Scheduler ts;
int lastState = 0;
void dhtReadCallback() {
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (!isnan(t)) {
int currentState = map(t, 15.00, 35.00, 1, 5);
if (lastState != currentState) {
Serial.println(currentState);
lastState = currentState;
}
Serial.println(t);
}
}
float bassFilter(float sample) {
static float xv[3] = {0, 0, 0}, yv[3] = {0, 0, 0};
xv[0] = xv[1]; xv[1] = xv[2];
xv[2] = (sample) / 3.f; // change here to values close to 2, to adapt for stronger or weeker sources of line level audio
yv[0] = yv[1]; yv[1] = yv[2];
yv[2] = (xv[2] - xv[0])
+ (-0.7960060012f * yv[0]) + (1.7903124146f * yv[1]);
return yv[2];
}
float envelopeFilter(float sample) { //10hz low pass
static float xv[2] = {0, 0}, yv[2] = {0, 0};
xv[0] = xv[1];
xv[1] = sample / 50.f;
yv[0] = yv[1];
yv[1] = (xv[0] + xv[1]) + (0.9875119299f * yv[0]);
return yv[1];
}
// 1.7 - 3.0hz Single Pole Bandpass IIR Filter
float beatFilter(float sample) {
static float xv[3] = {0, 0, 0},
yv[3] = {0, 0, 0};
xv[0] = xv[1]; xv[1] = xv[2];
xv[2] = sample / 2.7f;
yv[0] = yv[1]; yv[1] = yv[2];
yv[2] = (xv[2] - xv[0])
+ (-0.7169861741f * yv[0]) + (1.4453653501f * yv[1]);
return yv[2];
}
- define TRESHOLD 60.0
int map2(float value, float min, float max) {
if (value<min) return 0;
if (value>max) return 255;
return map(value,min,max,0,255);
}
void sampleCallback() {
float sample = (float)analogRead(0) - 503.f;
float value = bassFilter(sample);
if (value < 0)value = -value;
value = envelopeFilter(value);
if ((taskSample.getRunCounter() % 20) == 0) {
value = beatFilter(value);
if (value < 0)value = 0;
if (value > TRESHOLD)value = TRESHOLD;
int pwm = map(value, 0, TRESHOLD, 0, 255);
analogWrite(O0, pwm);
analogWrite(9, map2(value, 0, TRESHOLD * 0.3333));
analogWrite(6, map2(value, TRESHOLD * 0.3333, TRESHOLD * 0.6666));
analogWrite(5, map2(value, TRESHOLD * 0.6666, TRESHOLD));
}
}
// defines for setting and clearing register bits
- ifndef cbi
- define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
- endif
- ifndef sbi
- define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
- endif
void setup() {
// Set ADC to 77khz, max for 10bit
sbi(ADCSRA, ADPS2);
cbi(ADCSRA, ADPS1);
cbi(ADCSRA, ADPS0);
pinMode(O0, OUTPUT);
analogWrite(O0, 0);
Serial.begin(115200);
dht.begin();
ts.init();
ts.addTask(taskDht);
ts.addTask(taskSample);
taskDht.enable();
taskDht.forceNextIteration();
taskSample.enable();
}
void loop() {
ts.execute();
}