정보나눔

오픈소스하드웨어 프로젝트에 대한 다양한 정보를 나누는 공간입니다.

아두이노 버튼제어
njmp0304 | 2020-07-08
 

1. 프로젝트 사용한 보드 종류

  (EX : 오렌지보드, 오렌지 BLE보드, 아두이노 UNO 등)

 

  아두이노 UNO

2. 사용한 개발 프로그램명

  (EX : 아두이노 IDE, 스크래치, mBlock 등)

 

 아두이노 IDE

3. 사용한 센서 모델명

  (센서 모델명을 자세하게 적어 주실 경우 더 정확하게 확인할 수 있습니다.)

 

 

4. 연결한 회로 설명 (또는 이미지)

 

 

5. 소스코드 (주석 필수)

  (

 

#include <Adafruit_NeoPixel.h>
#define NUM_STRIP  5 //연결한 네오픽셀스트립의 갯수
#define HSVCOLOR  65536

int pin[] = {6, 7, 8, 9, 10};
int num_pixels[] = {12, 15, 12, 12, 15};

Adafruit_NeoPixel strip[] = {
  Adafruit_NeoPixel(num_pixels[0], pin[0], NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(num_pixels[1], pin[1], NEO_GRB + NEO_KHZ800),
  Adafruit_NeoPixel(num_pixels[2], pin[2], NEO_GRB + NEO_KHZ800),
};

int sensorPin[] = {A0, A1, A2};
int sensorVal[] = {0, 0, 0, 0, 0, 0};


int reading[3];
int count[2];
int button_pin[] = {2, 3};
int buttonState[2];
int lastButtonState[2];
unsigned long lastDebounceTime[2];
unsigned long debounceDelay = 50;

 

void setup() {
  Serial.begin(9600);
  for (int i = 0; i < 2; i++) {
    pinMode(button_pin[i], INPUT);
  }
  for (int i = 0; i < NUM_STRIP; i++) {
    strip[i].begin();
  }
}


void loop() {
  analogReader();
  buttonCounter();
  //analogPrint();
  //buttonPrint();
  float h = map(sensorVal[0], 0, 1023, 0, HSVCOLOR);
  float s = map(sensorVal[1], 0, 1023, 0, 255);
  float v = map(sensorVal[2], 0, 1023, 0, 255);

 

  switch (count[0] % 4) {

    case 0:
      switch (count[1] % 4) {
        case 0:

          CylonBounce(0, h, s, v, 4, 10, 1000);
          CylonBounce(4, h, s, v, 4, 10, 1000);

          break;

        case 1:
          CylonBounce(1, h, s, v, 4, 10, 1000);
          CylonBounce(2, h, s, v, 4, 10, 1000);
          CylonBounce(3, h, s, v, 4, 10, 1000);

          break;

        case 2:

          CylonBounce(0, h, s, v, 4, 10, 1000);
          CylonBounce(1, h, s, v, 4, 10, 1000);
          CylonBounce(2, h, s, v, 4, 10, 1000);
          CylonBounce(3, h, s, v, 4, 10, 1000);
          CylonBounce(4, h, s, v, 4, 10, 1000);

          break;

        case 3:

          setALL(0, 0, 0, 0);

          break;
      }

    case 1:
      Sparkle(count[1] % 2, h, s, v, 50);
      break;
      switch (count[1] % 4) {
        case 0:
          Sparkle(0, h, s, v, 50);
          Sparkle(4, h, s, v, 50);
          break;
        case 1:
          Sparkle(1, h, s, v, 50);
          Sparkle(2, h, s, v, 50);
          Sparkle(3, h, s, v, 50);
          break;
        case 2:
          Sparkle(0, h, s, v, 50);
          Sparkle(1, h, s, v, 50);
          Sparkle(2, h, s, v, 50);
          Sparkle(3, h, s, v, 50);
          Sparkle(4, h, s, v, 50);
          break;
        case 3:
          setALL(1, 0, 0, 0);

          break;
      }
    case 2:
      Fire(count[1] % 2, 55, 120, 15);
      break;
      switch (count[1] % 4) {
        case 0:
          Fire(0, 55, 120, 15);
          Fire(4, 55, 120, 15);
          break;
        case 1:
          Fire(1, 55, 120, 15);
          Fire(2, 55, 120, 15);
          Fire(3, 55, 120, 15);
          break;
        case 2:
          Fire(0, 55, 120, 15);
          Fire(1, 55, 120, 15);
          Fire(2, 55, 120, 15);
          Fire(3, 55, 120, 15);
          Fire(4, 55, 120, 15);
          break;
        case 3:
          setALL(2, 0, 0, 0);
          break;
      }
    case 3:
      meteorRain(count[1] % 2, h, s, v, 10, 64, true, 30);
      break;
      switch (count[1] % 4) {
        case 0:
          meteorRain(0, h, s, v, 10, 64, true, 30);
          meteorRain(4, h, s, v, 10, 64, true, 30);
          break;
        case 1:
          meteorRain(1, h, s, v, 10, 64, true, 30);
          meteorRain(2, h, s, v, 10, 64, true, 30);
          meteorRain(3, h, s, v, 10, 64, true, 30);
          break;
        case 2:
          meteorRain(0, h, s, v, 10, 64, true, 30);
          meteorRain(1, h, s, v, 10, 64, true, 30);
          meteorRain(2, h, s, v, 10, 64, true, 30);
          meteorRain(3, h, s, v, 10, 64, true, 30);
          meteorRain(4, h, s, v, 10, 64, true, 30);
          break;
        case 3:
          setALL(3, 0, 0, 0);
          break;
      }
      
    default:
      // statements
      break;
  }

}


/////////////////////////////////////////////////
////////////////////////////////////////////////
unsigned long meteorRain_pM[NUM_STRIP];
int meteorRain_index[NUM_STRIP];

void meteorRain(int which, float red, float green, float blue, byte meteorSize, byte meteorTrailDecay, boolean meteorRandomDecay, int SpeedDelay) {
  unsigned long cM = millis();


  if (cM - meteorRain_pM[which] > SpeedDelay) {
    meteorRain_pM[which] = cM;

    for (int j = 0; j < num_pixels[which]; j++) {
      if ( (!meteorRandomDecay) || (random(10) > 5) ) {
        fadeToBlack(which, j, meteorTrailDecay );
      }
    }
    // draw meteor
    for (int j = 0; j < meteorSize; j++) {
      if ( (  meteorRain_index[which] - j < num_pixels[which]) && ( meteorRain_index[which] - j >= 0) ) {
        setPixel(which,  meteorRain_index[which] - j, red, green, blue);
      }
    }

    meteorRain_index[which]++;
    if (meteorRain_index[which] > num_pixels[which] + num_pixels[which]) {
      meteorRain_index[which] = 0;
      setAll(which, 0, 0, 0);
    }
  }
  showStrip();
}

void fadeToBlack(int which, int ledNo, byte fadeValue) {
  uint32_t oldColor;
  uint8_t r, g, b;
  int value;

  oldColor = strip[which].getPixelColor(ledNo);
  r = (oldColor & 0x00ff0000UL) >> 16;
  g = (oldColor & 0x0000ff00UL) >> 8;
  b = (oldColor & 0x000000ffUL);

  r = (r <= 10) ? 0 : (int) r - (r * fadeValue / 256);
  g = (g <= 10) ? 0 : (int) g - (g * fadeValue / 256);
  b = (b <= 10) ? 0 : (int) b - (b * fadeValue / 256);

  strip[which].setPixelColor(ledNo, r, g, b);
}

/////////////////////////////////////////////////
////////////////////////////////////////////////
unsigned long Fire_pM[NUM_STRIP];


void Fire(int which, int Cooling, int Sparking, int SpeedDelay) {
  byte heat[num_pixels[which]];
  int cooldown;
  unsigned long cM = millis();
  if (cM - Fire_pM[which] > SpeedDelay) {
    Fire_pM[which] = cM;

    // Step 1.  Cool down every cell a little
    for ( int i = 0; i < num_pixels[which]; i++) {
      cooldown = random(0, ((Cooling * 10) / num_pixels[which]) + 2);

      if (cooldown > heat[i]) {
        heat[i] = 0;
      } else {
        heat[i] = heat[i] - cooldown;
      }
    }

    // Step 2.  Heat from each cell drifts 'up' and diffuses a little
    for ( int k = num_pixels[which] - 1; k >= 2; k--) {
      heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
    }

    // Step 3.  Randomly ignite new 'sparks' near the bottom
    if ( random(255) < Sparking ) {
      int y = random(7);
      heat[y] = heat[y] + random(160, 255);
      //heat[y] = random(160,255);
    }

    // Step 4.  Convert heat to LED colors
    for ( int j = 0; j < num_pixels[which]; j++) {
      setPixelHeatColor(which, j, heat[j] );
    }
  }
  showStrip();

}

void setPixelHeatColor (int which, int Pixel, byte temperature) {
  // Scale 'heat' down from 0-255 to 0-191
  byte t192 = round((temperature / 255.0) * 191);

  // calculate ramp up from
  byte heatramp = t192 & 0x3F; // 0..63
  heatramp <<= 2; // scale up to 0..252

  // figure out which third of the spectrum we're in:
  if ( t192 > 0x80) {                    // hottest
    setPixelRGB(which, Pixel, 255, 255, heatramp);
  } else if ( t192 > 0x40 ) {            // middle
    setPixelRGB(which, Pixel, 255, heatramp, 0);
  } else {                               // coolest
    setPixelRGB(which, Pixel, heatramp, 0, 0);
  }
}


/////////////////////////////////////////////////
////////////////////////////////////////////////
unsigned long Sparkle_pM[NUM_STRIP];
int Sparkle_Pixel[NUM_STRIP];

void Sparkle(int which, float red, float green, float blue, int SpeedDelay) {
  unsigned cM = millis();

  if (cM - Sparkle_pM[which] > SpeedDelay) {
    Sparkle_pM[which] = cM;

    setPixel(which, Sparkle_Pixel[which], 0, 0, 0);
    Sparkle_Pixel[which] = random(num_pixels[which]);
    setPixel(which, Sparkle_Pixel[which], red, green, blue);

  }
  showStrip();
}
/////////////////////////////////////////////////
////////////////////////////////////////////////
unsigned long pM[NUM_STRIP], pMr[NUM_STRIP];
int CylonBounce_delta[NUM_STRIP] = {1, 1};
int CylonBounce_index[NUM_STRIP];
boolean go[NUM_STRIP], rtn[NUM_STRIP];

void CylonBounce(int which, float hue, float saturation, float value, int EyeSize, int SpeedDelay, int ReturnDelay) {
  unsigned long cM = millis();
  if (!go[which]) {
    if (cM - pM[which] > SpeedDelay) {
      pM[which] = cM;

      setAll(which, 0, 0, 0);
      setPixel(which, CylonBounce_index[which], hue, saturation, value / 10);
      for (int j = 1; j <= EyeSize; j++) {
        setPixel(which, CylonBounce_index[which] + j, hue, saturation, value);
      }
      setPixel(which, CylonBounce_index[which] + EyeSize + 1, hue, saturation, value / 10);


      CylonBounce_index[which] += CylonBounce_delta[which];
      if ( CylonBounce_index[which] > num_pixels[which] - EyeSize - 2 || CylonBounce_index[which] < 0) {
        CylonBounce_delta[which] *= -1;
        pMr[which] = cM;
        go[which] = true;
        rtn[which] = true;
      }
    }
  }

  if (rtn) {
    if (cM - pMr[which] > ReturnDelay) {
      //pM[which] = cM;
      rtn[which] = false;
      go[which] = false;
    }
  }

  showStrip();
}

 

//////////////////////////////////////////////////
/////////////////////////////////////////////////
void analogPrint() {
  for (int i = 0; i < 6; i++) {
    Serial.print("sensor A");
    Serial.print(i);
    Serial.print(" : ");
    Serial.print(sensorVal[i]);
    Serial.print(", ");
  }
  Serial.println("");
}
void buttonPrint() {
  for (int i = 0; i < 2; i++) {
    Serial.print("count");
    Serial.print(i);
    Serial.print(" : ");
    Serial.print(count[i]);
    Serial.print(", ");
  }
  Serial.println("");
}
void analogReader() {
  for (int i = 0; i < 6; i++) {
    sensorVal[i] = analogRead(sensorPin[i]);
  }
}
void buttonCounter() {
  for (int i = 0; i < 2; i++) {
    reading[i] = digitalRead(button_pin[i]);
    if (reading[i] != lastButtonState[i]) {
      lastDebounceTime[i] = millis();
    }

    if ((millis() - lastDebounceTime[i]) > debounceDelay) {
      if (reading[i] != buttonState[i]) {
        buttonState[i] = reading[i];
        if (buttonState[i] == HIGH) {
          count[i]++;
          for (int i = 0; i < NUM_STRIP; i++) {
            strip[i].clear();
          }
          showStrip();
        }
      }
    }
    lastButtonState[i] = reading[i];
  }
}


void showStrip() {
  for (int i = 0; i < NUM_STRIP; i++) {
    strip[i].show();
  }
}

void setPixel(int which, int Pixel, float hue, float saturation, float value) {
  uint32_t rgbcolor = strip[which].ColorHSV(hue, saturation, value);
  strip[which].setPixelColor(Pixel, rgbcolor);
}
void setPixelRGB(int which, int Pixel, float red, float green, float blue) {
  strip[which].setPixelColor(Pixel, strip[which].Color(red, green, blue));
}

void setAll(int which, float hue, float saturation, float value) {
  for (int i = 0; i < num_pixels[which]; i++ ) {
    setPixel(which, i, hue, saturation, value);
  }
  showStrip();
}

 

6. 문제점 및 에러 내용

 

 

'setALL' was not declared in this scope

라고 자꾸뜹니다. 그전의 오류는 어떻게 어떻게 고쳐봤는데

이건 어떻게 해야할지 모르겠어서 여쭈어봅니다

 

 

이전글   |    fatal error: RTClib.h: No such file or directory ... 2020-07-07
다음글   |    아두이노를 이용한 mp3플레이어 만드는것좀 도와주세요 ... 2020-07-12