add sensor examples and dir structure
This commit is contained in:
parent
ee16f406e4
commit
b7decb5002
@ -8,8 +8,7 @@ unsigned long currentMillis;
|
|||||||
int brightness = 0; // our main variable for setting the brightness of the LED
|
int brightness = 0; // our main variable for setting the brightness of the LED
|
||||||
float velocity = 1.0; // the speed at which we change the brightness.
|
float velocity = 1.0; // the speed at which we change the brightness.
|
||||||
int ledPin = 9; // we use pin 9 for PWM
|
int ledPin = 9; // we use pin 9 for PWM
|
||||||
int p = 0; // use to keep track how often we plot
|
int plotFrequency = 2; // how often we plot, every Nth time.
|
||||||
int plotFrequency = 3; // how often we plot, every Nth time.
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// put your setup code here, to run once:
|
// put your setup code here, to run once:
|
||||||
@ -20,9 +19,9 @@ void setup() {
|
|||||||
void loop() {
|
void loop() {
|
||||||
// put your main code here, to run repeatedly:
|
// put your main code here, to run repeatedly:
|
||||||
compose();
|
compose();
|
||||||
delay(10);
|
|
||||||
analogWrite(ledPin, brightness);
|
analogWrite(ledPin, brightness);
|
||||||
currentMillis = millis(); //store the current time since the program started
|
currentMillis = millis(); //store the current time since the program started
|
||||||
|
delay(10);
|
||||||
}
|
}
|
||||||
|
|
||||||
void compose() {
|
void compose() {
|
||||||
@ -38,7 +37,7 @@ void compose() {
|
|||||||
plot("INCREASING", brightness);
|
plot("INCREASING", brightness);
|
||||||
|
|
||||||
if (brightness > 250){
|
if (brightness > 250){
|
||||||
//ledState = WAVE;
|
ledState = WAVE;
|
||||||
changeState(WAVE);
|
changeState(WAVE);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -54,7 +53,7 @@ void compose() {
|
|||||||
case WAVE:
|
case WAVE:
|
||||||
plot("WAVE", brightness);
|
plot("WAVE", brightness);
|
||||||
|
|
||||||
brightness = sinewave(1000,256,0); // you can tweak the parameters of the sinewave
|
brightness = sinewave(500,256,0); // you can tweak the parameters of the sinewave
|
||||||
analogWrite(ledPin, brightness);
|
analogWrite(ledPin, brightness);
|
||||||
|
|
||||||
if (currentMillis - startMillis >= 5000){ //change state after 5 secs by comparing the time elapsed since we last change state
|
if (currentMillis - startMillis >= 5000){ //change state after 5 secs by comparing the time elapsed since we last change state
|
||||||
@ -105,6 +104,10 @@ int increase_brightness (int brightness, float velocity){
|
|||||||
return brightness = brightness + 1 * velocity;
|
return brightness = brightness + 1 * velocity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int exponential_increase (int brightness, float velocity){
|
||||||
|
return brightness = 1 + brightness * velocity;
|
||||||
|
}
|
||||||
|
|
||||||
int decrease_brightness (int brightness, float velocity){
|
int decrease_brightness (int brightness, float velocity){
|
||||||
return brightness = brightness - 1 * velocity;
|
return brightness = brightness - 1 * velocity;
|
||||||
}
|
}
|
63
sensor_filtering/sensor_filtering.ino
Normal file
63
sensor_filtering/sensor_filtering.ino
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
int brightness = 0; // our main variable for setting the brightness of the LED
|
||||||
|
int ledPin = 9; // we use pin 9 for PWM
|
||||||
|
int sensorPin = A5;
|
||||||
|
|
||||||
|
// smoothing
|
||||||
|
const int numReadings = 10; // higher is smoother but less responsive
|
||||||
|
|
||||||
|
int readings[numReadings]; // the readings from the analog input
|
||||||
|
int readIndex = 0; // the index of the current reading
|
||||||
|
int total = 0; // the running total
|
||||||
|
int average = 0; // the average
|
||||||
|
|
||||||
|
int ldr;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
// put your setup code here, to run once:
|
||||||
|
Serial.begin(9600);
|
||||||
|
pinMode(ledPin, OUTPUT);
|
||||||
|
pinMode(sensorPin, INPUT);
|
||||||
|
|
||||||
|
// set all readings to 0 on start
|
||||||
|
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
|
||||||
|
readings[thisReading] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
ldr = analogRead(sensorPin);
|
||||||
|
// brightness = ldr; // don't do this
|
||||||
|
|
||||||
|
//filtering jitteryness
|
||||||
|
int smoothed = smooth_sensor(ldr);
|
||||||
|
//Serial.println(smoothed);
|
||||||
|
|
||||||
|
//clamping the range
|
||||||
|
// n.b. the actual values, calibrate!
|
||||||
|
brightness = map(ldr, 0, 1023, 0, 255);
|
||||||
|
Serial.println(brightness);
|
||||||
|
|
||||||
|
analogWrite(ledPin, brightness);
|
||||||
|
delay(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int smooth_sensor(int sensor_value) {
|
||||||
|
// subtract the last reading:
|
||||||
|
total = total - readings[readIndex];
|
||||||
|
// read from the sensor:
|
||||||
|
readings[readIndex] = sensor_value;
|
||||||
|
// add the reading to the total:
|
||||||
|
total = total + readings[readIndex];
|
||||||
|
// advance to the next position in the array:
|
||||||
|
readIndex = readIndex + 1;
|
||||||
|
|
||||||
|
// if we're at the end of the array...
|
||||||
|
if (readIndex >= numReadings) {
|
||||||
|
// ...wrap around to the beginning:
|
||||||
|
readIndex = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// calculate the average:
|
||||||
|
average = total / numReadings;
|
||||||
|
return average;
|
||||||
|
}
|
48
sensor_processing/sensor_processing.ino
Normal file
48
sensor_processing/sensor_processing.ino
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
float brightness = 0; // our main variable for setting the brightness of the LED
|
||||||
|
const int ledPin = 9; // we use pin 9 for PWM
|
||||||
|
const int sensorPin = 4; // we use pin D4 for the button
|
||||||
|
|
||||||
|
int buttonState = 0;
|
||||||
|
float counter = 0;
|
||||||
|
// the ratio between counter increment and fade should be tweaked
|
||||||
|
float counter_inc = 1.0;
|
||||||
|
float counter_fade = 0.99;
|
||||||
|
int counter_threshold = 90;
|
||||||
|
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
// put your setup code here, to run once:
|
||||||
|
Serial.begin(9600);
|
||||||
|
pinMode(ledPin, OUTPUT);
|
||||||
|
pinMode(sensorPin, INPUT);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
buttonState = !digitalRead(sensorPin);
|
||||||
|
// digitalWrite(ledPin,buttonState); // don't do this
|
||||||
|
|
||||||
|
Serial.println(counter);
|
||||||
|
|
||||||
|
// fading pattern that requires 'effort'
|
||||||
|
if (buttonState == HIGH) {
|
||||||
|
|
||||||
|
counter += counter_inc;
|
||||||
|
}
|
||||||
|
|
||||||
|
counter = counter*counter_fade;
|
||||||
|
|
||||||
|
if (counter > counter_threshold){
|
||||||
|
brightness ++;
|
||||||
|
// here you could also switchCase(myCase)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
brightness = brightness * 0.99;
|
||||||
|
}
|
||||||
|
|
||||||
|
brightness = constrain(brightness, 0, 255);
|
||||||
|
|
||||||
|
analogWrite(ledPin, brightness);
|
||||||
|
|
||||||
|
delay(5);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user