Uživatel:ThermoDream: Porovnání verzí
Značka: editace z Vizuálního editoru |
|||
Řádek 38: | Řádek 38: | ||
=== Vlastní zdrojový kód === | === Vlastní zdrojový kód === | ||
− | + | <code>include <TinkerKit.h></code> | |
− | + | <code>include <TaskScheduler.h></code> | |
− | + | <code>include "DHT.h"</code> | |
− | |||
− | void dhtReadCallback(); | + | <code><br> |
+ | void dhtReadCallback();</code> | ||
− | void sampleCallback(); | + | <code>void sampleCallback();</code> |
− | + | <code><br> | |
− | DHT dht(3, DHT22); //DHT22 connected to pin 3 | + | DHT dht(3, DHT22); //DHT22 connected to pin 3</code> |
+ | <code><br> | ||
+ | Task taskDht(15000, TASK_FOREVER, &dhtReadCallback);</code> | ||
− | + | <code>Task taskSample(1, TASK_FOREVER, &sampleCallback);</code> | |
− | |||
− | Task taskSample(1, TASK_FOREVER, &sampleCallback); | ||
− | + | <code><br> | |
− | Scheduler ts; | + | Scheduler ts;</code> |
+ | <code><br> | ||
+ | int lastState = 0;</code> | ||
− | + | <code><br> | |
− | + | void dhtReadCallback() {</code> | |
− | |||
− | void dhtReadCallback() { | ||
− | // Read temperature as Celsius (the default) | + | <code>// Read temperature as Celsius (the default)</code> |
− | float t = dht.readTemperature(); | + | <code>float t = dht.readTemperature();</code> |
− | // Check if any reads failed and exit early (to try again). | + | <code>// Check if any reads failed and exit early (to try again).</code> |
− | |||
− | |||
− | + | <code>if (!isnan(t)) {</code> | |
− | + | <code>int currentState = map(t, 15.00, 35.00, 1, 5);</code> | |
− | + | <code>if (lastState != currentState) {</code> | |
− | + | <code>Serial.println(currentState);</code> | |
− | + | <code>lastState = currentState;</code> | |
− | + | <code>}</code> | |
− | + | <code>Serial.println(t);</code> | |
− | } | + | <code>}</code> |
+ | <code>}</code> | ||
+ | <code><br> | ||
+ | </code> | ||
− | float bassFilter(float sample) { | + | <code>float bassFilter(float sample) {</code> |
− | static float xv[3] = {0, 0, 0}, yv[3] = {0, 0, 0}; | + | <code>static float xv[3] = {0, 0, 0}, yv[3] = {0, 0, 0};</code> |
− | xv[0] = xv[1]; xv[1] = xv[2]; | + | <code>xv[0] = xv[1]; xv[1] = xv[2];</code> |
− | xv[2] = (sample) / 3.f; // change here to values close to 2, to adapt for stronger or weeker sources of line level audio | + | <code>xv[2] = (sample) / 3.f; // change here to values close to 2, to adapt for stronger or weeker sources of line level audio</code> |
− | |||
+ | <code><br> | ||
+ | </code> | ||
− | yv[0] = yv[1]; yv[1] = yv[2]; | + | <code>yv[0] = yv[1]; yv[1] = yv[2];</code> |
− | yv[2] = (xv[2] - xv[0]) | + | <code>yv[2] = (xv[2] - xv[0])</code> |
− | + (-0.7960060012f * yv[0]) + (1.7903124146f * yv[1]); | + | <code>+ (-0.7960060012f * yv[0]) + (1.7903124146f * yv[1]);</code> |
− | return yv[2]; | + | <code>return yv[2];</code> |
− | } | + | <code>}</code> |
+ | <code><br> | ||
+ | float envelopeFilter(float sample) { //10hz low pass</code> | ||
− | + | <code>static float xv[2] = {0, 0}, yv[2] = {0, 0};</code> | |
− | |||
− | static float xv[2] = {0, 0}, yv[2] = {0, 0}; | ||
− | xv[0] = xv[1]; | + | <code>xv[0] = xv[1];</code> |
− | xv[1] = sample / 50.f; | + | <code>xv[1] = sample / 50.f;</code> |
− | yv[0] = yv[1]; | + | <code>yv[0] = yv[1];</code> |
− | yv[1] = (xv[0] + xv[1]) + (0.9875119299f * yv[0]); | + | <code>yv[1] = (xv[0] + xv[1]) + (0.9875119299f * yv[0]);</code> |
− | return yv[1]; | + | <code>return yv[1];</code> |
− | } | + | <code>}</code> |
+ | <code><br> | ||
+ | // 1.7 - 3.0hz Single Pole Bandpass IIR Filter</code> | ||
− | + | <code>float beatFilter(float sample) {</code> | |
− | |||
− | float beatFilter(float sample) { | ||
− | static float xv[3] = {0, 0, 0}, | + | <code>static float xv[3] = {0, 0, 0},</code> |
− | yv[3] = {0, 0, 0}; | + | <code>yv[3] = {0, 0, 0};</code> |
− | xv[0] = xv[1]; xv[1] = xv[2]; | + | <code>xv[0] = xv[1]; xv[1] = xv[2];</code> |
− | xv[2] = sample / 2.7f; | + | <code>xv[2] = sample / 2.7f;</code> |
− | yv[0] = yv[1]; yv[1] = yv[2]; | + | <code>yv[0] = yv[1]; yv[1] = yv[2];</code> |
− | yv[2] = (xv[2] - xv[0]) | + | <code>yv[2] = (xv[2] - xv[0])</code> |
− | + (-0.7169861741f * yv[0]) + (1.4453653501f * yv[1]); | + | <code>+ (-0.7169861741f * yv[0]) + (1.4453653501f * yv[1]);</code> |
− | return yv[2]; | + | <code>return yv[2];</code> |
− | |||
− | |||
− | |||
− | |||
− | + | <code>}</code> | |
+ | <code><br> | ||
+ | define TRESHOLD 60.0</code> | ||
− | int map2(float value, float min, float max) { | + | <code><br> |
+ | int map2(float value, float min, float max) {</code> | ||
− | if (value<min) return 0; | + | <code>if (value<min) return 0;</code> |
− | if (value>max) return 255; | + | <code>if (value>max) return 255;</code> |
− | return map(value,min,max,0,255); | + | <code>return map(value,min,max,0,255);</code> |
− | } | + | <code>}</code> |
+ | <code><br> | ||
+ | void sampleCallback() {</code> | ||
− | + | <code>float sample = (float)analogRead(0) - 503.f;</code> | |
− | float | + | <code>float value = bassFilter(sample);</code> |
− | + | <code>if (value < 0)value = -value;</code> | |
− | + | <code>value = envelopeFilter(value);</code> | |
− | + | <code>if ((taskSample.getRunCounter() % 20) == 0) {</code> | |
− | + | <code>value = beatFilter(value);</code> | |
− | |||
− | value = beatFilter(value); | ||
+ | <code><br> | ||
+ | if (value < 0)value = 0;</code> | ||
− | + | <code>if (value > TRESHOLD)value = TRESHOLD;</code> | |
− | |||
− | if (value > TRESHOLD)value = TRESHOLD; | ||
+ | <code><br> | ||
+ | int pwm = map(value, 0, TRESHOLD, 0, 255);</code> | ||
− | + | <code>analogWrite(O0, pwm);</code> | |
− | |||
− | analogWrite(O0, pwm); | ||
+ | <code><br> | ||
+ | analogWrite(9, map2(value, 0, TRESHOLD * 0.3333));</code> | ||
− | analogWrite( | + | <code>analogWrite(6, map2(value, TRESHOLD * 0.3333, TRESHOLD * 0.6666));</code> |
− | + | <code>analogWrite(5, map2(value, TRESHOLD * 0.6666, TRESHOLD));</code> | |
− | |||
− | analogWrite(5, map2(value, TRESHOLD * 0.6666, TRESHOLD)); | ||
− | |||
+ | <code><br> | ||
+ | </code> | ||
− | } | + | <code>}</code> |
− | + | <code><br> | |
− | } | + | }</code> |
+ | <code><br> | ||
+ | // defines for setting and clearing register bits</code> | ||
− | / | + | <code>ifndef cbi</code> |
− | + | <code>define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))</code> | |
− | + | <code>endif</code> | |
− | + | <code>ifndef sbi</code> | |
− | + | <code>define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))</code> | |
− | + | <code>endif</code> | |
− | |||
− | |||
− | |||
− | void setup() { | + | <code><br> |
+ | void setup() {</code> | ||
− | // Set ADC to 77khz, max for 10bit | + | <code>// Set ADC to 77khz, max for 10bit</code> |
− | sbi(ADCSRA, ADPS2); | + | <code>sbi(ADCSRA, ADPS2);</code> |
− | cbi(ADCSRA, ADPS1); | + | <code>cbi(ADCSRA, ADPS1);</code> |
− | cbi(ADCSRA, ADPS0); | + | <code>cbi(ADCSRA, ADPS0);</code> |
+ | <code><br> | ||
+ | pinMode(O0, OUTPUT);</code> | ||
− | + | <code>analogWrite(O0, 0);</code> | |
− | |||
− | analogWrite(O0, 0); | ||
+ | <code><br> | ||
+ | Serial.begin(115200);</code> | ||
− | + | <code>dht.begin();</code> | |
− | + | <code>ts.init();</code> | |
− | ts. | + | <code>ts.addTask(taskDht);</code> |
− | ts.addTask( | + | <code>ts.addTask(taskSample);</code> |
− | + | <code>taskDht.enable();</code> | |
− | taskDht. | + | <code>taskDht.forceNextIteration();</code> |
− | + | <code>taskSample.enable();</code> | |
− | + | <code>}</code> | |
− | |||
− | } | ||
+ | <code><br> | ||
+ | void loop() {</code> | ||
− | + | <code>ts.execute();</code> | |
− | |||
− | ts.execute(); | ||
− | } | + | <code>}</code> |
Verze z 30. 3. 2018, 18:26
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();
}