17/11/2567

ปีการศึกษา 2567 ภาคเรียนที่ 2 ครูเต้ง เทคโนฯ NeoPixel - 12

        📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้วงจร

                          

📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้คำสั่งเขียน 

#include <Adafruit_NeoPixel.h>

#ifdef __AVR__

#include <avr/power.h>

#endif

#define PIN 6

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

void setup() {

  #if defined (__AVR_ATtiny85__)

    if (F_CPU == 16000000) clock_prescale_set(clock_div_1);

  #endif

  strip.begin();

  strip.show(); 

}

void loop() {

  colorWipe(strip.Color(255, 0, 0), 50); 

  colorWipe(strip.Color(0, 255, 0), 50); 

  colorWipe(strip.Color(0, 0, 255), 50); 

  theaterChase(strip.Color(127, 127, 127), 50); 

  theaterChase(strip.Color(127, 0, 0), 50); 

  theaterChase(strip.Color(0, 0, 127), 50); 

  rainbow(20);

  rainbowCycle(20);

  theaterChaseRainbow(50);

}

void colorWipe(uint32_t c, uint8_t wait) {

  for(uint16_t i=0; i<strip.numPixels(); i++) {

    strip.setPixelColor(i, c);

    strip.show();

    delay(wait);

  }

}

void rainbow(uint8_t wait) {

  uint16_t i, j;

  for(j=0; j<256; j++) {

    for(i=0; i<strip.numPixels(); i++) {

      strip.setPixelColor(i, Wheel((i+j) & 255));

    }

    strip.show();

    delay(wait);

  }

}

void rainbowCycle(uint8_t wait) {

  uint16_t i, j;

  for(j=0; j<256*5; j++) { 

    for(i=0; i< strip.numPixels(); i++) {

      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));

    }

    strip.show();

    delay(wait);

  }

}

void theaterChase(uint32_t c, uint8_t wait) {

  for (int j=0; j<10; j++) {  

    for (int q=0; q < 3; q++) {

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {

        strip.setPixelColor(i+q, c); 

      }

      strip.show();


      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {

        strip.setPixelColor(i+q, 0);//desativa a cada 3 pixeis

      }

    }

  }

}

void theaterChaseRainbow(uint8_t wait) {

  for (int j=0; j < 256; j++) { 

    for (int q=0; q < 3; q++) {

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {

        strip.setPixelColor(i+q, Wheel( (i+j) % 255)); 

      }

      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {

        strip.setPixelColor(i+q, 0); //desativa a cada 3 pixeis

      }

    }

  }

}

uint32_t Wheel(byte WheelPos) {

  WheelPos = 255 - WheelPos;

  if(WheelPos < 85) {

    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);

  }

  if(WheelPos < 170) {

    WheelPos -= 85;

    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);

  }

  WheelPos -= 130;

  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);

}



ไม่มีความคิดเห็น:

แสดงความคิดเห็น

ครูเต้ง เทคโนฯ