• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Sterownik schodowy
#1
Witam potrzebuję przerobić program tak aby przy zapaleniu i gaszeniu każdego stopnia powoli się rozświetlał i powoli gasną
Kod:
// Edit by Serge Niko June 2015

#include <Adafruit_NeoPixel.h>

#define PIN 10

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)

Adafruit_NeoPixel strip = Adafruit_NeoPixel(150, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

// Set up Variables
unsigned long timeOut=60000; // timestamp to remember when the PIR was triggered.
int downUp = 0;              // variable to rememer the direction of travel up or down the stairs
int alarmPinTop = 3;        // PIR at the top of the stairs
int alarmPinBottom = 6;      // PIR at the bottom of the stairs
int alarmValueTop = LOW;    // Variable to hold the PIR status
int alarmValueBottom = LOW; // Variable to hold the PIR status
int ledPin = 13;           // LED on the arduino board flashes when PIR activated
int colourArray[350];      // An array to hold RGB values
int change = 1;            // used in 'breathing' the LED's
int breathe = 0;           // used in 'breathing' the LED's


void setup() {
   strip.begin();
   strip.show(); // Initialize all pixels to 'off'
   Serial.begin (9600);  // only requred for debugging
   pinMode(ledPin, OUTPUT);  // initilise the onboard pin 13 LED as an indicator
   pinMode(alarmPinTop, INPUT_PULLUP);     // for PIR at top of stairs initialise the input pin and use the internal restistor
   pinMode(alarmPinBottom, INPUT_PULLUP);  // for PIR at bottom of stairs initialise the input pin and use the internal restistor
   delay (30000); // it takes the sensor 30 seconds to scan the area around it before it can
   //detect infrared presence.
   for (int i=0 ;i < 350; i++)  { // initilise the colourArray to zero
   colourArray[i]=0;
   }
}

void loop() {
  
  if (timeOut+15700 < millis()) {        // idle state - 'breathe' the top and bottom LED to show program is looping
     breathe = breathe + change;
     strip.setPixelColor(0,0,0,breathe);
     strip.setPixelColor(34,0,0,breathe);        //***blauw knipperen****
     strip.setPixelColor(35,0,0,breathe);         //***blauw knipperen****
     strip.setPixelColor(69,0,0,breathe);
     strip.setPixelColor(70,0,0,breathe);
     strip.setPixelColor(104,0,0,breathe);
     strip.setPixelColor(105,0,0,breathe);
     strip.setPixelColor(139,0,0,breathe);
     strip.setPixelColor(140,0,0,breathe);
     strip.setPixelColor(174,0,0,breathe);
     strip.setPixelColor(175,0,0,breathe);
     strip.setPixelColor(209,0,0,breathe);
     strip.setPixelColor(210,0,0,breathe);
     strip.setPixelColor(244,0,0,breathe);
     strip.setPixelColor(245,0,0,breathe);
     strip.setPixelColor(279,0,0,breathe);
     strip.setPixelColor(280,0,0,breathe);
     strip.setPixelColor(314,0,0,breathe);
     strip.setPixelColor(315,0,0,breathe);
     strip.setPixelColor(349,0,0,breathe);
     strip.setPixelColor(350,0,0,breathe);
     strip.setPixelColor(384,0,0,breathe);
     strip.setPixelColor(385,0,0,breathe);
     strip.setPixelColor(419,0,0,breathe);
     strip.setPixelColor(420,0,0,breathe);
     strip.setPixelColor(454,0,0,breathe);
     strip.setPixelColor(455,0,0,breathe);
     strip.setPixelColor(489,0,0,breathe);
     strip.setPixelColor(490,0,0,breathe);
     strip.setPixelColor(524,0,0,breathe);
     strip.setPixelColor(525,0,0,breathe);
     strip.setPixelColor(559,0,0,breathe);
     strip.show();
     if (breathe == 25 || breathe == 0) change = -change;      // breathe the LED from 0 = off to 100 = fairly bright
     if (breathe == 25 || breathe == 0); delay (100);           // Pause at beginning and end of each breath
     delay(10);
    
  }

  //if  {      

    alarmValueTop = digitalRead(alarmPinTop);    // Constantly poll the PIR at the top of the stairs
    //Serial.println(alarmPinTop);
    alarmValueBottom = digitalRead(alarmPinBottom);  // Constantly poll the PIR at the bottom of the stairs
    //Serial.println(alarmPinBottom);
  
    if (alarmValueTop == HIGH && downUp != 2)  {      // the 2nd term allows timeOut to be contantly reset if one lingers at the top of the stairs before decending but will not allow the bottom PIR to reset timeOut as you decend past it.
      timeOut=millis();  // Timestamp when the PIR is triggered.  The LED cycle wil then start.
      downUp = 1;
      //clearStrip();
      topdown();         // lights up the strip from top down
      delay(300);
    }

    if (alarmValueBottom == HIGH && downUp != 1)  {    // the 2nd term allows timeOut to be contantly reset if one lingers at the bottom of the stairs before decending but will not allow the top PIR to reset timeOut as you decend past it.
      timeOut=millis();    // Timestamp when the PIR is triggered.  The LED cycle wil then start.
      downUp = 2;
      //clearStrip();
      bottomup();         // lights up the strip from bottom up
    }
  //}
    if (timeOut+10000 < millis() && timeOut+15000 < millis()) {    //switch off LED's in the direction of travel.
       if (downUp == 1) {
          colourWipeDown(strip.Color(0, 0, 0), 100); // Off
                 }
       if (downUp == 2)  {
        colourWipeUp(strip.Color(0, 0, 0), 100);   // Off
       }
      downUp = 0;
          for (int i=0 ;i < 35; i++)  {          // Depending on your preference you may want to include this loop to clear out the colourArray
          colourArray[i]=0;
      }
    }
          
    if (timeOut+15000 < millis() && timeOut+15999 > millis()) fade();   // Fade/switch off LED's

}

void topdown() {
    Serial.println ("detected top");                  // Helpful debug message
    colourWipeDown(strip.Color(255,255,102), 25 );  // Warm White ****  kleur keuze blauw *****
      //for(int i=0; i<3; i++) {                        // Helpful debug indication flashes led on Arduino board twice
      //digitalWrite(ledPin,HIGH);
      //delay(200);
      //digitalWrite(ledPin,LOW);
      //delay(200);
    //}
}


void idlePattern(uint32_t c, uint16_t wait){
  for (uint16_t i=0; i<strip.numPixels(); i = i+35){        //nope
    strip.setPixelColor(i,c);
    strip.show();
    delay(wait);
    strip.setPixelColor(i,(0,0,0));
  }
  for (uint16_t j= strip.numPixels(); j>0; j = j-35){       //nope
    strip.setPixelColor(j-1,c);
    strip.show();
    delay(wait);
    strip.setPixelColor(j-1,(0,0,0));
  }

}

void bottomup() {
    Serial.println ("detected bottom");            // Helpful debug message
    colourWipeUp(strip.Color(255,255,102), 25);  // Warm White **** kleur keuze blauw ****
    //for(int i=0; i<3; i++) {                     // Helpful debug indication flashes led on Arduino board twice
      //digitalWrite(ledPin,HIGH);
      //delay(200);
      //digitalWrite(ledPin,LOW);
      //delay(200);
    //}
  }

// Fade light each step strip
void colourWipeDown(uint32_t c, uint16_t wait) {

  for (uint16_t j = 0; j < 16; j++){
  int start = strip.numPixels()/16 *j;
    //for(float b=0; b<=1; b = b+0.5){
        for (uint16_t i = start; i < start + 35; i++){
    strip.setPixelColor(i, c);
      strip.show();
        }
    //delay(5);
    //}

  delay(wait);
  }

}

void clearStrip(){
  for (int l=0; l<strip.numPixels(); l++){
    strip.setPixelColor(l, (0,0,0));
  }
      
}
// Fade light each step strip
void colourWipeUp(uint32_t c, uint16_t wait) {
   for (uint16_t j = 16; j > 0; j--){
   int start = strip.numPixels()/16 *j;
      //start = start-1;
      //for(float b=0; b<=1; b = b+0.5){
          for (uint16_t i = start; i > start - 35; i--){
          strip.setPixelColor(i-1, c);
            strip.show();
         }
      //delay(5);
      //}

  //Serial.println(j);
  delay(wait);
  }

}

// Play the LED fade out
void fade(){
for (int j = 0; j <70; j++) {
   for(int i=350; i>-1; i--) {
     colourArray[i]=colourArray[i]-1;      // reduce intensity of light by 1
     if (colourArray[i] <= 0 ) colourArray[i] = 0;
   }
   for (int k=0; k<351; k=k+3)   {
     uint32_t c = strip.Color(colourArray[k],colourArray[k+1],colourArray[k+2]);
     strip.setPixelColor(((k+3)/3)-1,c);
   }
  strip.show();
  delay(60);
  }
breathe = 0;
change = 1;
}
 
Odpowiedź
#2
znalazłem jeszcze coś takiego
Kod:
const int fadeMax = 100; // 100 step fade
Kod:
// Fade up the pixels on one step
void fadeUpStep(int stepNumber, uint8_t red, uint8 green, uint8_t blue, uint16_t wait)
{
   // calculate the address of the first & last pixels in the step
   int start = strip.numPixels()/10 * stepNumber;  
   int end = start+10;  
     
   for(int brightness = 0; step <= fadeMax, step++)  // Fade over fadeMax increments
   {
      // calculate the faded colors based on the brightness
      fadeRed = red * brightness / fadeMax;
      fadeGreen = green * brightness / fadeMax;
      fadeBlue = green * brightness / fadeMax;
     
      // Now fade them
      for (uint16_t pixel = start; pixel > end; pixel++)
      {
         strip.setPixelColor(i-1, strip.Color(fadeRed, fadeGreen, fadeBlue));
      }
      strip.show();
      delay(wait);
   }
}
Kod:
void bottomup()
{
    Serial.println ("detected bottom");            // Helpful debug message
   for (int step = 0; step < 10; step++)
   {
      fadeUpStep(int step, 255,255,102, 25);  // Warm White **** kleur keuze blauw ****
   }
}
tylko jak to poukładać Undecided
 
Odpowiedź
#3
(13-12-2020, 16:50)#include napisał(a): #define PIN 10

Adafruit_NeoPixel strip = Adafruit_NeoPixel(150, PIN, NEO_GRB + NEO_KHZ800);

unsigned long timeOut = 60000;
int downUp = 0;
int alarmPinTop = 3;
int alarmPinBottom = 6;
int alarmValueTop = LOW;
int alarmValueBottom = LOW;
int ledPin = 13;
int colourArray[350];
int change = 1;
int breathe = 0;

void setup() {
  strip.begin();
  strip.show();
  Serial.begin (9600);
  pinMode(ledPin, OUTPUT);
  pinMode(alarmPinTop, INPUT_PULLUP);
  pinMode(alarmPinBottom, INPUT_PULLUP);
  delay (30000);
  for (int i = 0; i < 350; i++) {
      colourArray[i] = 0;
  }
}

void loop() {
  if (timeOut + 15700 < millis()) {
    breathe = breathe + change;
    breathe = constrain(breathe, 0, 255);
    for (int i = 0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, strip.Color(0, 0, breathe));
    }
    strip.show();
    if (breathe == 255 || breathe == 0) change = -change;
    delay(100);
    delay(10);
  }

  alarmValueTop = digitalRead(alarmPinTop);
  alarmValueBottom = digitalRead(alarmPinBottom);

  if (alarmValueTop == HIGH && downUp != 2) {
    timeOut = millis();
    downUp = 1;
    topdown();
    delay(300);
  }

  if (alarmValueBottom == HIGH && downUp != 1) {
    timeOut = millis();
    downUp = 2;
    bottomup();
  }

  if (timeOut + 10000 < millis() && timeOut + 15000 < millis()) {
    if (downUp == 1) {
      colourWipeDown(strip.Color(0, 0, 0), 100);
    }
    if (downUp == 2) {
      colourWipeUp(strip.Color(0, 0, 0), 100);
    }
    downUp = 0;
    for (int i = 0; i < 35; i++) {
      colourArray[i] = 0;
    }
  }

  if (timeOut + 15000 < millis() && timeOut + 15999 > millis()) fade();
}

void topdown() {
    Serial.println("detected top");
    colourWipeDown(strip.Color(255, 255, 102), 50);  // Adjust timing to make fading smooth
}

void bottomup() {
    Serial.println("detected bottom");
    colourWipeUp(strip.Color(255, 255, 102), 50);  // Adjust timing to make fading smooth
}

void colourWipeDown(uint32_t color, uint16_t wait) {
  for (uint16_t j = 0; j < strip.numPixels(); j++) {
    for (float b = 0; b <= 1; b += 0.05) { // Smooth fade effect
      int r = (strip.Color(color) >> 16) & 0xFF;
      int g = (strip.Color(color) >> 8) & 0xFF;
      int b = (strip.Color(color)) & 0xFF;
      strip.setPixelColor(j, r * b, g * b, b * b);
      strip.show();
      delay(wait);
    }
  }
}

void colourWipeUp(uint32_t color, uint16_t wait) {
  for (int j = strip.numPixels() - 1; j >= 0; j--) {
    for (float b = 0; b <= 1; b += 0.05) { // Smooth fade effect
      int r = (strip.Color(color) >> 16) & 0xFF;
      int g = (strip.Color(color) >> 8) & 0xFF;
      int b = (strip.Color(color)) & 0xFF;
      strip.setPixelColor(j, r * b, g * b, b * b);
      strip.show();
      delay(wait);
    }
  }
}

void fade() {
  for (int j = 0; j < 70; j++) {
    for (int i = 0; i < strip.numPixels(); i++) {
      int r = (strip.Color(colourArray[i]) >> 16) & 0xFF;
      int g = (strip.Color(colourArray[i]) >> 8) & 0xFF;
      int b = (strip.Color(colourArray[i])) & 0xFF;
      r = max(r - 1, 0);
      g = max(g - 1, 0);
      b = max(b - 1, 0);
      strip.setPixelColor(i, r, g, b);
    }
    strip.show();
    delay(60);
  }
  breathe = 0;
  change = 1;
}
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości