第2回

Assignment1

光を操る(ランダムで光らせる)
  1. #include <Adafruit_NeoPixel.h>
  2. #define PIN 9 //Arduinoで使うピン
  3. #define NUMPIXELS 5//LEDの数。
  4. int brightness=50;//明るさ
  5. int red_value=255;//赤要素
  6. int red_target=255;//赤要素の変化_目標値
  7. int green_value=255;//緑要素
  8. int green_target=255;//緑要素の変化_目標値
  9. int blue_value=255;//青要素
  10. int blue_target=255;//青要素の変化_目標値
  11. boolean disp=true;
  12. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  13. void setup()
  14. {
  15.   pixels.begin(); // NeoPixel出力ピンの初期化
  16.   pixels.setBrightness(brightness);
  17.   pinMode(PIN,OUTPUT);
  18.   red_value=255;
  19.   red_target=random(0,255);
  20.   green_value=0;
  21.   green_target=random(0,255);
  22.   blue_value=180;
  23.   blue_target=random(0,255);
  24.   for(int i=0; i < NUMPIXELS; i++){
  25.     pixels.setPixelColor(i,pixels.Color(red_value,green_value,blue_value));
  26.   }
  27.   pixels.show();
  28. }
  29. void loop(){
  30.   if (disp==true){
  31.       // 赤要素の変化 -----------------------
  32.         if (red_target>red_value){
  33.           //目標値より値が小さかったら、値を増やす
  34.           red_value++;
  35.         }else if(red_target<red_value){
  36.           //目標値より値が大きかったら、値を減らす
  37.           red_value--;
  38.         }else if(red_target==red_value){
  39.           red_value=red_target;
  40.           //目標値になったら、目標値を再設定
  41.           red_target=random(0,255);
  42.         }
  43.       //緑要素の変化------------------------
  44.         if (green_target>green_value){
  45.           green_value++;
  46.         }else if(green_target<green_value){
  47.           green_value--;
  48.         }else if(green_target==green_value){
  49.           green_value=green_target;
  50.           green_target=random(0,255);
  51.         }
  52.       //青要素------------------------
  53.         if (blue_target>blue_value){
  54.           blue_value++;
  55.         }else if(blue_target<blue_value){
  56.           blue_value--;
  57.         }else if(blue_target==blue_value){
  58.           blue_value=blue_target;
  59.           blue_target=random(0,255);
  60.         }
  61.       for(int i=0; i < NUMPIXELS; i++){
  62.         pixels.setPixelColor(i,pixels.Color(red_value,green_value,blue_value));
  63.       }
  64.         pixels.show();
  65.         delay(50);
  66.   }
  67. }

Assignment2(POV)