Skip to content

Instantly share code, notes, and snippets.

@smukkejohan
Last active September 25, 2019 15:23
Show Gist options
  • Save smukkejohan/e92d06a30e4b8ec711032e5ebb0e540d to your computer and use it in GitHub Desktop.
Save smukkejohan/e92d06a30e4b8ec711032e5ebb0e540d to your computer and use it in GitHub Desktop.
Sinuskurver til animation over tid uden delays
#include <Arduino.h>
//#define FASTLED_RGBW
//#include <FastLED.h>
#include <Adafruit_NeoPixel.h>
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN 6
// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 24
// NeoPixel brightness, 0 (min) to 255 (max)
#define BRIGHTNESS 50
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRBW + NEO_KHZ800);
unsigned long lastElapsedTimeMillis = 0;
float animationTime = 0;
int potPin = 32;
// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
strip.setPixelColor(i, color); // Set pixel's color (in RAM)
strip.show(); // Update strip to match
delay(wait); // Pause for a moment
}
}
void fadeLedUpDownWithDelay() {
for(int i=0; i<255; i++) {
analogWrite(4, i);
delay(30);
}
}
void setup() {
// put your setup code here, to run once:
//FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
//FastLED.setBrightness(128);
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
}
void loop() {
//int potVal = analogRead(potPin);
//int speed = map(potVal, 0, 1023, 10, 200);
// Med colorWipe sætter vi programmet i stå under hver animation
// derfor kan vi ikke lytte til knapper, inputs mm. imens
//colorWipe(strip.Color(255, 0, 0) , speed); // Red
//colorWipe(strip.Color(0, 255, 0) , speed); // Green
// I stedet kan vi lave en animation som en funktion af tiden
// ved hjælp af en sinuskurve
unsigned long elapsedTimeMillis = millis();
unsigned long deltaTime = elapsedTimeMillis - lastElapsedTimeMillis;
lastElapsedTimeMillis = elapsedTimeMillis;
int potVal = analogRead(potPin);
float frequency = potVal * 0.00001;
animationTime += deltaTime * frequency;
float offset = M_PI / strip.numPixels();
for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
float intensity = ((sin( animationTime + (offset*i) ) +1) * 0.5 ) * 255.0;
// sin ( animation tid + (offset * nummer i række) + 1 ) * 05 * range vi skal bruge
// sin returnere altid en værdi mellem -1 og 1 derfor vi lægger 1 til og ganger med 0.5 så det bliver mellem 0 og 1
// kunne også se sådan ud
// float intensity = sin(animationTime + (offset*i));
// intensity = map(intensity, -1, 1, 0, 255);
strip.setPixelColor(i, strip.Color(intensity, intensity, intensity)); // Set pixel's color (in RAM)
}
// fadeLedUpDownWithDelay(); // den her vil vi ikke have da den sætter programmet i stå
// Istedet kan vi fade 1 LED uden delay
analogWrite(4, ((sin(animationTime) +1) * 0.5) * 255.0);
strip.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment