728x90
반응형
LED 5개
push 버튼 1개 -- 모드 변경, mode 1 -> mode 2 -> mode 3 -> mode1 ... (버튼은 모두 pull-down 구조)
push 버튼 1개 -- On/Off, 3초 이상 길게 누를 시에 mode 1로 초기화(동작이안되는것처럼 보여야함)
다이얼 1개 -- 기능 조절
Mode 1 => 5개 LED 모두 진 상태로 다이얼로 밝기 조절(min 꺼짐, max 최대 밝기)
Mode 2 => (1, 3, 5) (2, 4) 순서대로 점멸, 다이얼로 점멸 속도 조절 (min 0.1초, max 2초)
Mode 3 => (1->2->3->4->5->1) 순서대로 이동, 다이얼로 이동속도 조절( min 0.1초, max 1초)
const int BtnMode = 7;
const int BtnSw = 8;
const int LED[5] = {3, 5, 6, 10, 11};
void AdjLed(const int* led, int n);
void SeqLed(const int* led, int n, int(*fp)(void));
void MovLed(const int* led, int n, int(*fp)(void));
int Dial(void);
int BtnState = 1;
void setup()
{
Serial.begin(9600);
pinMode(BtnMode, INPUT);
pinMode(BtnSw, INPUT);
for (int i = 0; i < 5; i++)
{
pinMode(LED[i], OUTPUT);
}
}
void loop()
{
int number = digitalRead(BtnMode);
BtnState += number;
delay(200);
Serial.println(BtnState);
if (BtnState == 4)
{
BtnState = 1;
}
switch(BtnState)
{
case 0:
for(int i = 0; i < 5; i++)
{
digitalWrite(LED[i], LOW);
}
break;
case 1:
/* Mode 1 */
while(1)
{
AdjLed(LED, 5);
delay(100);
if (digitalRead(BtnMode))
{
break;
}
}
Serial.println(BtnState);
break;
case 2:
/* Mode 2 */
while(1)
{
SeqLed(LED, 5, Dial);
delay(100);
if (digitalRead(BtnMode))
{
break;
}
}
Serial.println(BtnState);
break;
case 3:
/* Mode 3 */
MovLed(LED, 5, Dial);
delay(100);
Serial.println(BtnState);
break;
}
}
/* Mode 1: 밝기 조절 */
void AdjLed(const int* led, int n)
{
int val = analogRead(A0)/4;
for (int i = 0; i < n; i++)
{
analogWrite(LED[i], val);
}
}
/* Mode 2: 순차 점멸 (1, 3, 5) (2, 4) */
void SeqLed(const int* led, int n, int(*fp)(void))
{
for (int i = 0; i < n; i++)
{
if (i % 2 == 1)
{
digitalWrite(LED[i], HIGH);
}
else
{
digitalWrite(LED[i], LOW);
}
}
delay(fp());
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
digitalWrite(LED[i], HIGH);
}
else
{
digitalWrite(LED[i], LOW);
}
}
delay(fp());
}
/* Mode 3: 순차 점멸 이동 (1, 2, 3, 4, 5) */
void MovLed(const int* led, int n, int(*fp)(void))
{
int i = n -1;
while(1)
{
for (int i = 0; i < n; i++)
{
digitalWrite(LED[i], LOW);
}
digitalWrite(LED[i], HIGH);
delay(fp());
i -= 1;
if (i == -1)
{
i = n -1;
}
if (digitalRead(BtnMode))
{
break;
}
}
}
/* 다이얼 기능 */
int Dial(void)
{
int temp = analogRead(A0);
temp = map(temp, 0, 2000, 0, 1023);
return temp;
}
/* */
void temp()
{
}
const int BtnMode = 7;
const int BtnSw = 8;
const int LED[5] = {3, 5, 6, 10, 11};
void AdjLed(const int* led, int n);
void SeqLed(const int* led, int n, int(*fp)(void));
void MovLed(const int* led, int n, int(*fp)(void));
int Dial(void);
int BtnState = 1;
boolean Power = 0;
void setup()
{
Serial.begin(9600);
pinMode(BtnMode, INPUT);
pinMode(BtnSw, INPUT);
for (int i = 0; i < 5; i++)
{
pinMode(LED[i], OUTPUT);
}
}
void loop()
{
Power = digitalRead(BtnSw);
while (Power == true)
{
/* Mode Button */
int number = digitalRead(BtnMode);
BtnState += number;
if (BtnState == 4)
{
BtnState = 1;
}
delay(100);
Serial.println(BtnState);
/* SWITCH */
switch(BtnState)
{
case 0:
for(int i = 0; i < 5; i++)
{
digitalWrite(LED[i], LOW);
}
case 1:
/* Mode 1 */
while(1)
{
AdjLed(LED, 5);
delay(100);
if (digitalRead(BtnMode))
{
break;
}
}
break;
case 2:
/* Mode 2 */
while(1)
{
SeqLed(LED, 5, Dial);
if (digitalRead(BtnMode))
{
break;
}
}
break;
case 3:
/* Mode 3 */
MovLed(LED, 5, Dial);
delay(50);
break;
}
}
}
/* Mode 1: 밝기 조절 */
void AdjLed(const int* led, int n)
{
int val = analogRead(A0)/4;
for (int i = 0; i < n; i++)
{
analogWrite(led[i], val);
}
}
/* Mode 2: 순차 점멸 (1, 3, 5) (2, 4) */
void SeqLed(const int* led, int n, int(*fp)(void))
{
for (int i = 0; i < n; i++)
{
if (i % 2 == 1)
{
digitalWrite(led[i], HIGH);
}
else
{
digitalWrite(led[i], LOW);
}
}
delay(fp());
for (int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
digitalWrite(led[i], HIGH);
}
else
{
digitalWrite(led[i], LOW);
}
}
delay(fp());
}
/* Mode 3: 순차 점멸 이동 (1, 2, 3, 4, 5) */
void MovLed(const int* led, int n, int(*fp)(void))
{
int i = n -1;
while(1)
{
for (int i = 0; i < n; i++)
{
digitalWrite(led[i], LOW);
}
digitalWrite(led[i], HIGH);
delay(fp());
i -= 1;
if (i == -1)
{
i = n -1;
}
if (digitalRead(BtnMode))
{
break;
}
}
}
/* 다이얼 기능 */
int Dial(void)
{
int temp = analogRead(A0);
temp = map(temp, 0, 2000, 0, 1023);
return temp;
}
3차 수정
const int BtnMode = 7;
const int BtnSw = 8;
const int LED[5] = {3, 5, 6, 10, 11};
void AdjLed(const int* led, int n, int* state);
void SeqLed(const int* led, int n, int(*fp)(void), int* state);
void MovLed(const int* led, int n, int(*fp)(void), int* state);
int Dial(void);
void Mode(int* btnp);
void Off(const int* led, int n, int* state);
int BtnState = 1;
void setup()
{
Serial.begin(9600);
pinMode(BtnMode, INPUT);
pinMode(BtnSw, INPUT);
for (int i = 0; i < 5; i++)
{
pinMode(LED[i], OUTPUT);
}
}
void loop()
{
Serial.print("BtnState(Loop): ");
Serial.println(BtnState);
/* SWITCH */
switch(BtnState)
{
case 0:
Off(LED, 5,&BtnState);
break;
/* Mode 1 */
case 1:
AdjLed(LED, 5, &BtnState);
delay(100);
break;
/* Mode 2 */
case 2:
SeqLed(LED, 5, Dial, &BtnState);
delay(100);
break;
/* Mode 3 */
case 3:
MovLed(LED, 5, Dial, &BtnState);
delay(100);
break;
}
if (BtnState == 4)
{
BtnState = 1;
}
}
/* Mode 1: 밝기 조절 */
void AdjLed(const int* led, int n, int* state)
{
while(digitalRead(BtnMode) == 0)
{
int val = analogRead(A0)/4;
for (int i = 0; i < n; i++)
{
analogWrite(led[i], val);
}
}
*state += 1;
}
/* Mode 2: 교대 점멸 (1, 3, 5) (2, 4) */
void SeqLed(const int* led, int n, int(*fp)(void), int* state)
{
while(digitalRead(BtnMode) == 0)
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < n; j++)
{
if (j % 2 == i)
{
digitalWrite(led[j], HIGH);
}
else
{
digitalWrite(led[j], LOW);
}
}
delay(fp());
}
}
*state += 1;
}
/* Mode 3: 순차 점멸 (1, 2, 3, 4, 5) */
void MovLed(const int* led, int n, int(*fp)(void), int* state)
{
int i = n -1;
while(digitalRead(BtnMode) == 0)
{
for (int i = 0; i < n; i++)
{
digitalWrite(led[i], LOW);
}
digitalWrite(led[i], HIGH);
delay(fp());
i -= 1;
if (i == -1)
{
i = n -1;
}
}
*state += 1;
}
/* 다이얼 기능 */
int Dial(void)
{
int temp = analogRead(A0);
temp = map(temp, 0, 2000, 0, 1023);
return temp;
}
/* 전원 기능 */
void Off(const int* led, int n, int* state)
{
int i = 0;
while(digitalRead(BtnSw) == 1)
{
digitalWrite(led[i], LOW);
}
*state = 1;
}
4차 수정
const int BtnMode = 7;
const int BtnSw = 8;
const int LED[5] = {3, 5, 6, 10, 11};
void AdjLed(const int* led, int n);
void SeqLed(const int* led, int n, int(*fp)(void));
void MovLed(const int* led, int n, int(*fp)(void));
int Dial(void);
void Mode(int* btnp);
void Off(const int* led, int n, int* state);
int p_BtnState = 0;
int BtnState = 1;
int temp = 0;
boolean Power = false;
void setup()
{
Serial.begin(9600);
pinMode(BtnMode, INPUT);
pinMode(BtnSw, INPUT);
for (int i = 0; i < 5; i++)
{
pinMode(LED[i], OUTPUT);
}
}
void loop()
{
Serial.print("Power(Start): ");
Serial.println(Power);
if (Power == true)
{
Off(LED, 5);
}
Serial.print("BtnState(Loop): ");
Serial.println(BtnState);
/* SWITCH */
switch(BtnState)
{
case 0:
Off(LED, 5);
break;
/* Mode 1 */
case 1:
AdjLed(LED, 5);
delay(100);
break;
/* Mode 2 */
case 2:
SeqLed(LED, 5, Dial);
delay(100);
break;
/* Mode 3 */
case 3:
MovLed(LED, 5, Dial);
delay(100);
break;
}
if (Power == false)
{
BtnState += 1;
}
if (BtnState == 4)
{
BtnState = 1;
}
}
/* Mode 1: 밝기 조절 */
void AdjLed(const int* led, int n)
{
while(digitalRead(BtnMode) == 0)
{
int val = analogRead(A0)/4;
for (int i = 0; i < n; i++)
{
analogWrite(led[i], val);
}
if(digitalRead(BtnSw) == 1)
{
Power = true;
break;
}
}
}
/* Mode 2: 교대 점멸 (1, 3, 5) (2, 4) */
void SeqLed(const int* led, int n, int(*fp)(void))
{
while(digitalRead(BtnMode) == 0)
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < n; j++)
{
if (j % 2 == i)
{
digitalWrite(led[j], HIGH);
}
else
{
digitalWrite(led[j], LOW);
}
}
delay(fp());
}
if (digitalRead(BtnSw) == 1)
{
Power = true;
break;
}
}
}
/* Mode 3: 순차 점멸 (1, 2, 3, 4, 5) */
void MovLed(const int* led, int n, int(*fp)(void))
{
int i = n -1;
while(digitalRead(BtnMode) == 0)
{
for (int i = 0; i < n; i++)
{
digitalWrite(led[i], LOW);
}
digitalWrite(led[i], HIGH);
delay(fp());
i -= 1;
if (i == -1)
{
i = n -1;
}
if (digitalRead(BtnSw) == 1)
{
Power = true;
break;
}
}
}
/* 다이얼 기능 */
int Dial(void)
{
int temp = analogRead(A0);
temp = map(temp, 0, 1023, 100, 2000);
return temp;
}
/* 전원 기능 */
void Off(const int* led, int n)
{
int i = 0;
while(digitalRead(BtnSw) == 0)
{
digitalWrite(LED[i], LOW);
i+=1;
if (i == 5)
{
i = 0;
}
}
Power = false;
}
Switch case 문 제거 version (loop 흐름제어)
const int BtnMode = 7;
const int BtnSw = 8;
const int LED[5] = {3, 5, 6, 10, 11};
/* Power */
bool Power = true;
bool Curr_Power = true;
bool Prev_Power = true;
/* Mode */
int ModeState = 1;
/* Time */
long current_millis;
bool isActive =false;
const int check_interval = 100;
const int button_interval = 3000;
long button_pressed_millis;
/* Function */
void ControlPower(void);
int Dial(void);
void AdjLed(const int* led, int n);
void SeqLed(const int* led, int n, int(*fp)(void));
void MovLed(const int* led, int n, int(*fp)(void));
void OddEven(const int* led, int n, int(*fp)(void));
void setup()
{
Serial.begin(9600);
pinMode(BtnMode, INPUT);
pinMode(BtnSw, INPUT);
for (int i = 0; i < 5; i++)
{
pinMode(LED[i], OUTPUT);
}
button_pressed_millis = 0;
}
void loop()
{
Serial.print("Power: ");
Serial.println(Power);
Serial.print("Mode: ");
Serial.println(ModeState);
/* 전원 */
if(digitalRead(BtnSw))
{
ControlPower();
}
//ControlPower();
if(digitalRead(BtnMode))
{
ModeState += 1;
}
if(ModeState == 4)
{
ModeState = 1;
}
if(ModeState == 1)
{
AdjLed(LED, 5);
}
if(ModeState == 2)
{
SeqLed(LED, 5, Dial);
}
if(ModeState == 3)
{
MovLed(LED, 5, Dial);
}
/*
if(ModeState == 4)
{
OddEven(LED, 5, Dial);
}
*/
if(Power == false)
{
for (int i = 0; i < 5; i++)
{
digitalWrite(LED[i], LOW);
}
}
}
/* 전원부 제어 */
void ControlPower(void)
{
current_millis = millis();
Serial.print("millis() : ");
Serial.println(current_millis);
long pressedTime = current_millis - button_pressed_millis;
if(digitalRead(BtnSw) == HIGH && pressedTime > button_interval)
{
button_pressed_millis = current_millis;
isActive = (isActive)? false : true;
if(!isActive)
{
ModeState = 1;
}
else
{
ModeState = ModeState;
}
delay(check_interval);
}
else if(digitalRead(BtnSw) == HIGH)
{
int BtnValue = digitalRead(BtnSw);
if(BtnValue != Prev_Power)
{
Curr_Power = BtnValue;
if (BtnValue == HIGH)
{
Power = !Power;
}
}
Prev_Power = !true;
}
}
/* 밝기 및 속도 제어 */
int Dial(void)
{
int temp = analogRead(A0);
temp = map(temp, 0, 1023, 0, 255);
return temp;
}
/* 전체 점등 */
void AdjLed(const int* led, int n)
{
int val = analogRead(A0) / 4;
if(Power == true)
{
for (int i = 0; i < n; i++)
{
analogWrite(led[i], val);
}
}
}
/* 교대 점멸 */
void SeqLed(const int* led, int n, int(*fp)(void))
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < n; j++)
{
if(j % 2 == i)
{
digitalWrite(led[j], Power);
}
else
{
digitalWrite(led[j], LOW);
}
}
delay(fp());
}
}
/* 순차 점멸 */
void MovLed(const int* led, int n, int(*fp)(void))
{
for (int i = 0; i <= n; i++)
{
for (int j = i; j <= n; j++)
{
digitalWrite(led[i], Power);
digitalWrite(led[j], LOW);
digitalWrite(led[j-i],LOW);
}
delay(fp());
}
}
/* 홀짝 점멸 */
void OddEven(const int* led, int n, int(*fp)(void))
{
for (int i = 1; i <= n; i+=2)
{
for (int j = 2; j <= n; i+=2)
{
digitalWrite(led[i], HIGH);
digitalWrite(led[j], LOW);
}
delay(fp());
}
}
exam
// C++ code
//
// mode change
// pin number
int POTENTIOMETER_PIN = A0;
int BUTTON_PIN1 = 7; //mode변경
int BUTTON_PIN2 = 8; //on,off 리셋
// const
int LEDs[] = {11,10,6,5,3};
int MAX_LED = 5;
int MAX_MODE = 4;
// variable
int preButtonState1 = 0; //mode변경
int preButtonState2 = 0; //on,off 리셋
int button1PushedTime = 0;
bool changedButton1State = false;
int ledOn = 1;
int mode = 0;
//int mode = 1;
int nextOnLed = 0;
int ledState = 0;
void setup()
{
//pinMode(LED_BUILTIN, OUTPUT);
pinMode(POTENTIOMETER_PIN, INPUT);
pinMode(BUTTON_PIN1, INPUT);
pinMode(BUTTON_PIN2, INPUT);
for (int i=0; i<MAX_LED; i++){
pinMode(LEDs[i], OUTPUT);
}
Serial.begin(9600);
}
void loop()
{
bool reset = false;
//------- Reset/On/Off
//Reset
int buttonState1 = digitalRead(BUTTON_PIN2);
if (buttonState1 == HIGH) {
//0->1 라이징
if (preButtonState1 == LOW) {
button1PushedTime = millis();
//off 상태일때 눌림으로만 on
if (!ledOn) {
ledOn = 1;
changedButton1State = true;
}
}
//누르고 있는 상태
int pushDeltaTime = millis() - button1PushedTime;
//Serial.println(pushDeltaTime);
if (changedButton1State == false && ledOn && pushDeltaTime >= 3000) {
reset = true;
changedButton1State = true;
button1PushedTime = millis();
modeReset();
}
}
//On/Off
//1->0 폴링
//if (buttonState1 - preButtonState1 < 0) {
if (buttonState1 == LOW && preButtonState1 == HIGH) {
if (changedButton1State == true) {
changedButton1State = false;
}else{
ledOn = 1 - ledOn;
}
}
preButtonState1 = buttonState1;
if (!ledOn) {
ledAllOff(true);
return;
}
//------ 모드변경
int buttonState2 = digitalRead(BUTTON_PIN1);
//Serial.println(buttonState);
//0->1 라이징
//if (buttonState2 - preButtonState2 > 0) {
if (buttonState2 == HIGH && preButtonState2 == LOW) {
mode = (mode + 1) % MAX_MODE;
nextOnLed = 0;
ledState = 0;
}
preButtonState2 = buttonState2;
//Serial.print("mode: ");
//Serial.println(mode);
//Serial.print("ledOn: ");
//Serial.println(ledOn);
int potentiometerVal = analogRead(POTENTIOMETER_PIN);
//Serial.println(potentiometerVal);
switch(mode) {
case 0:
//mode1(potentiometerVal / 4);
mode1(map(potentiometerVal, 0,1023, 0,255));
break;
case 1:{
int sensor = map(potentiometerVal, 0,1023, 100,2000);
//int sensor2 = mapping(potentiometerVal, 0,1023, 100,2000);
mode2(sensor);
break;
}
case 2:
mode3(map(potentiometerVal, 0,1023, 100,1000));
break;
case 3:
mode4(map(potentiometerVal, 0,1023, 100,500));
break;
default:
break;
}
}
long mapping(long val, long iMin, long iMax, long oMin, long oMax) {
return (val - iMin) * (oMax - oMin) / (iMax - iMin) + oMin;
}
void ledAllOff(bool reset) {
for (int i=0; i<MAX_LED; i++){
digitalWrite(LEDs[i], LOW);
}
if (reset) {
nextOnLed = 0;
ledState = 0;
}
}
void modeReset() {
Serial.println("modeReset()");
nextOnLed = 0;
ledState = 0;
mode = 0;
}
//5개 LED 모두 켜진 상태로 디이얼로 밝기 조절
void mode1(int light) {
//Serial.println("mode1()");
/*
if (ledState == light) {
return;
}
ledState = light;
Serial.println(light);
*/
for (int i=0; i<MAX_LED; i++){
analogWrite(LEDs[i], light);
}
delay(50);
}
//(1,3,5)->(2,4) ... 순서대로 점멸, 다이얼로 점멸 속도 조절
void mode2(int delayMS) {
//Serial.println("mode2()");
ledAllOff(false);
for (int i = 0+nextOnLed; i<MAX_LED; i+=2) {
digitalWrite(LEDs[i], HIGH);
}
//(1,3,5)->(2,4)
nextOnLed = 1 - nextOnLed;
/*
if (ledState >= 1) {
//(1,3,5)->(2,4)
nextOnLed = 1 - nextOnLed;
}
ledState = 1 - ledState;
*/
delay(delayMS);
}
//1->2->3->4->5->1 ... 순서대로 이동, 다이얼로 이동속도 조절
void mode3(int delayMS) {
//Serial.println("mode3()");
ledAllOff(false);
digitalWrite(LEDs[nextOnLed], HIGH);
//1->2->3->4->5->1
nextOnLed = (nextOnLed + 1) % MAX_LED;
/*
digitalWrite(LEDs[nextOnLed], ledState);
if (ledState) {
//1->2->3->4->5->1
nextOnLed = (nextOnLed + 1) % MAX_LED;
}
ledState = 1 - ledState;
*/
delay(delayMS);
}
//1->3->5->2->4->1 ...
void mode4(int delayMS) {
ledAllOff(false);
digitalWrite(LEDs[nextOnLed], HIGH);
//1->3->5->2->4->1 ...
nextOnLed = (nextOnLed + 2) % MAX_LED;
/*
digitalWrite(LEDs[nextOnLed], ledState);
if (ledState) {
//1->3->5->2->4->1 ...
nextOnLed = (nextOnLed + 2) % MAX_LED;
}
ledState = 1 - ledState;
*/
delay(delayMS);
}
728x90
반응형
'Firmware & Embedded > Components' 카테고리의 다른 글
map() / constrain() function (0) | 2022.11.23 |
---|---|
How to Program / Upload Code to ESP32-CAM AI-Thinker (Arduino IDE) (0) | 2022.11.23 |
Interrupt control (0) | 2022.11.21 |
Pull-up & Push-down (0) | 2022.11.21 |
BlueTooth Module(HC-05, 06) (0) | 2022.11.17 |