[아두이노 부품] Arduino Adafruit NeoPixel control with Softpot 소프트팟으로 네오픽셀 조작

2017. 8. 7. 01:41Arduino






자동차 핸들을 잡은 곳을 LED로 표시하기 위해서,


380mm 지름의 기본 승용차 핸들에 커버를 씌우고, 그 위에 500mm softpot membrane potentiometer를 붙였다.


지름이 38cm라면 핸들의 원주 길이는 약 120cm라고 하고, 위 아래로 10cm씩의 공간을 남기고 50cm의 softpot을 양쪽으로 붙였다.


500mm softpot은기존의 50mm의 softpot보다 훨씬 길어서 좀 더 세밀하게 잡고있는 부분을 출력할 수 있었다.





Softpot이 양쪽으로 두 개가 있기 때문에, A0 아날로그 핀은 왼쪽 softpot으로 설정했고, A1 아날로그 핀은 오른쪽 softpot으로 설정해서 동시에 좌우의 값을 반환할 수 있게 구현했다.


Softpot을 터치했는지 안 했는지는 digitalRead로 true false 형식으로 받으려고 했으나, Softpot은 analog 값을 반환해서 digital로 받아오는 것보다 다른 방법으로 터치여부를 판단했다.


간단하게 digitalRead(A0)의 반환값이 HIGH이지 LOW인지 파악하려고 했으나 원하는 값이 출력이 되지않고 값이 왔다갔다 했음.


그래서 터치를 안 했을 때, 1000이상의 값을 계속 반환하니까 1000이상일 때는 터치를 안 했다고 가정하고 1000 이하일 때는 터치를 했다고 설정을 했고,,, 혹시나 1000이상의 스팟을 터치를 하고 있을 수도 있으니까 1020 이상의 값을 반환하면 터치를 했다고 출력을 하게 했다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif
 
#define PIN 6
 
Adafruit_NeoPixel strip = Adafruit_NeoPixel(36, PIN, NEO_RGBW + NEO_KHZ800);
 
void setup() {
  Serial.begin(9600);
  digitalWrite(A0, HIGH);
  digitalWrite(A1, HIGH);
 
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}
 
void loop() {
  int softpotLeft = analogRead(A0);
  int softpotRight = analogRead(A1);
 
  boolean touchedLeft = false;
  boolean touchedRight = false;
 
  Serial.print(softpotLeft);
  Serial.print("   ");
  Serial.println(softpotRight);
 
  if (softpotLeft < 990 || softpotLeft > 1020){
    touchedLeft = true;
  }
  else{
    touchedLeft = false;
  }
 
  if (softpotRight < 990 || softpotRight > 1020){
    touchedRight = true;
  }
  else{
    touchedRight = false;
  }
 
  SetLeftLight(softpotLeft, touchedLeft);
  SetRightLight(softpotRight, touchedRight);
 
}
 
void SetLeftLight(int softpotReading, boolean touched){
  
   if(touched){
    
    if(softpotReading < 256){
      for(int i=0;i<4;i++){
          strip.setPixelColor(i,255,0,0,0);
          strip.show();
      }
    }
    else if(softpotReading >= 256 && softpotReading < 512){
      for(int i=4;i<9;i++){
          strip.setPixelColor(i,255,0,0,0);
          strip.show();
      }
    }
    else if(softpotReading >= 512 && softpotReading < 768){
      for(int i=9;i<14;i++){
          strip.setPixelColor(i,255,0,0,0);
          strip.show();
      }
    }
    else{
      for(int i=14;i<18;i++){
          strip.setPixelColor(i,255,0,0,0);
          strip.show();
      }
    }
  }
  
  else{
    for(int i=0;i<18;i++){
        strip.setPixelColor(i,0,0,0,0);
        strip.show();
    }
  }
  
}
 
void SetRightLight(int softpotReading, boolean touched){
 
   if(touched){
    
    if(softpotReading < 256){
      for(int i=32;i<36;i++){
          strip.setPixelColor(i,255,0,0,0);
          strip.show();
      }
    }
    else if(softpotReading >= 256 && softpotReading < 512){
      for(int i=27;i<32;i++){
          strip.setPixelColor(i,255,0,0,0);
          strip.show();
      }
    }
    else if(softpotReading >= 512 && softpotReading < 768){
      for(int i=22;i<27;i++){
          strip.setPixelColor(i,255,0,0,0);
          strip.show();
      }
    }
    else{
      for(int i=18;i<22;i++){
          strip.setPixelColor(i,255,0,0,0);
          strip.show();
      }
    }
  }
  
  else{
    for(int i=18;i<36;i++){
        strip.setPixelColor(i,0,0,0,0);
        strip.show();
    }
  }
  
}
 
cs



LED 총 36개를 좌우 18개씩으로 나누고,


왼쪽 softpot의 0부터 1023까지 값을 4개의 스팟으로 구분해서, 손으로 잡은 구간의 LED를 출력했다.


0부터 17번의 LED를 첫 번째 구간은 4개의 LED를 할당하고, 두 번째 구간은 5개, 세 번째 구간은 5개, 네 번째는 4개의 LED를 할당해서 출력했다.


오른쪽도 왼쪽과 마찬가지로, 18부터 36번의 LED를 4개의 구간으로 나눠서 출력했다.


touchedLeft와 touchedRight의 boolean 변수를 둬서 터치를 했는지 여부를 파악하고, 누른 구간의 LED를 출력하는 방식이다.


아래의 첫 번째 영상은 핸들을 아래에서 위로, 다시 위에서 아래로 잡았을 때 잡은 구간을 출력해주는 시리얼 모니터를 보여준다.





두 번째 영상은 손으로 잡은 구간에 LED가 출력되는 모습을 볼 수 있다.