Any views expressed within media held on this service are those of the contributors, should not be taken as approved or endorsed by the University, and do not necessarily reflect the views of the University in respect of any particular issue.

Arduino code sample

//1. 呼吸灯-》sensor椅子触发-》固定颜色-》场景1灯光关闭,离开触发下一个
#include <FastLED.h>
const int switch_pin[5] = {25,33,32,35,34};
int switch_state[5][5];
// How many leds in your strip?
#define NUM_LEDS 10
// For led chips like WS2812, which have a data line, ground, and power, you just
// need to define DATA_PIN.  For led chipsets that are SPI based (four wires – data, clock,
// ground, and power), like the LPD8806 define both DATA_PIN and CLOCK_PIN
// Clock pin only needed for SPI based chipsets when not using hardware SPI
#define DATA_PIN 12  //需要连接12号引脚,在板子上 你
#define CLOCK_PIN 13
CRGB leds[NUM_LEDS];
int time_period = 1000; // 1000ms
// 刷新值
#define time_num  30
int led_values[time_num] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
// define variables
long duration;
int distance,distance_last;
// define mode array
// 0 is off, 1 is breathe, 2 is specific color
int led_mode[5] = {1,0,0,0,0};
int time_m, time_last;
int time_j = 0;
void setup() {
  // initialize serial communication
  Serial.begin(115200);
  pinMode(25, INPUT);
  pinMode(33, INPUT);
  pinMode(32, INPUT);
  pinMode(35, INPUT);
  pinMode(34, INPUT);
  pinMode(18,OUTPUT);
  digitalWrite(18,HIGH);
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);  // GRB ordering is assumed
  time_last = time_m;
  time_m = millis();
}
void loop() {
  // sample
  for (int i = 0; i < 5; i++)
  {
    for (int j = 0; j < 4; j++)
    {
      switch_state[i][j] = switch_state[i][j+1];
    }
    switch_state[i][4] = digitalRead(switch_pin[i]);
  }
  Serial.print(switch_state[0][0]);
  Serial.print(“,”);
  Serial.print(switch_state[0][1]);
  Serial.print(“,”);
  Serial.print(switch_state[0][2]);
  Serial.print(“,”);
  Serial.print(switch_state[0][3]);
  Serial.print(“,”);
  Serial.print(switch_state[0][4]);
  // when light breath, and button press
  if (led_mode[0] == 1 && switch_state[0][0] == 1 && switch_state[0][1] == 1 && switch_state[0][2] == 1 && switch_state[0][3] == 1 && switch_state[0][4] == 1)
  {
    led_mode[0] = 2;
  }
  else if(led_mode[0] == 2 && switch_state[0][0] == 0 && switch_state[0][1] == 0 && switch_state[0][2] == 0 && switch_state[0][3] == 0 && switch_state[0][4] == 0)
  {
    led_mode[0] = 0;
    led_mode[1] = 1;
  }
  Serial.print(“,”);
  Serial.print(led_mode[0]);
  Serial.println();
  // LED1
  if (led_mode[0] == 0)
  {
    for (int i = 0; i <NUM_LEDS;  i++)
    {
      int r = 0;
      int g = 0;
      int b = 0;
      leds[i] = CRGB(g,b,r);
    }
  }
  else if (led_mode[0] == 1)
  {
    // led on, rgb
    if (millis()-time_m > time_period/time_num)
    {
      time_m = millis();
      time_j += 1;
      if (time_j >= time_num)
      {
        time_j = 0;
      }
      for (int i = 0; i <NUM_LEDS;  i++)
      {
          int r = led_values[time_j]*10;
          int g = led_values[time_j]*10;
          int b = led_values[time_j]*10;
          leds[i] = CRGB(g,b,r);
          Serial.println(r);
      }
      FastLED.show();
    }
  }
  else if(led_mode[0] == 2)
  {
    // led specific
    for (int i = 0; i <NUM_LEDS;  i++)
    {
      int r = 10;
      int g = 0;
      int b = 0;
      leds[i] = CRGB(g,b,r);
    }
  }
  FastLED.show();
}

Leave a Reply

Your email address will not be published. Required fields are marked *

css.php

Report this page

To report inappropriate content on this page, please use the form below. Upon receiving your report, we will be in touch as per the Take Down Policy of the service.

Please note that personal data collected through this form is used and stored for the purposes of processing this report and communication with you.

If you are unable to report a concern about content via this form please contact the Service Owner.

Please enter an email address you wish to be contacted on. Please describe the unacceptable content in sufficient detail to allow us to locate it, and why you consider it to be unacceptable.
By submitting this report, you accept that it is accurate and that fraudulent or nuisance complaints may result in action by the University.

  Cancel