int Buzzer = 7; // 버저 핀을 7번에 연결
int Sensor = 9; // 센서핀은 9번에 연결
int val;
int reading;
void setup () {
Serial.begin(9600);
pinMode(Buzzer, OUTPUT); // 버저를 출력으로 설정
pinMode(Sensor, INPUT); // 센서값을 입력으로 설정
}
void loop () {
val = digitalRead(Sensor); // 센서값 읽어옴
if (val == HIGH) { // 장애물 감지가 안되면
noTone(7); // 버저가 울리지 않는다
delay(100);
} else { // 장애물이 감지되면
tone(7, 220); // 버저가 울린다
delay(100);
Serial.print(val);
}
}
I want to add tact switch in this code.
What should I do with this code if I want to make buzzer sound only when tact switch is pressed?
Use INPUT_PULLUP for the switch input. Connect the switch between GND and the input pin.
jim-p
3
Do you want to use the switch together with the sensor input?
Hello kjg481268
Consider
int Buzzer = 7; // 버저 핀을 7번에 연결
int Sensor = 9; // 센서핀은 9번에 연결
uint8_t tactSwitch {10};
int val;
int reading;
void setup ()
{
Serial.begin(9600);
pinMode(Buzzer, OUTPUT); // 버저를 출력으로 설정
pinMode(Sensor, INPUT); // 센서값을 입력으로 설정
pinMode(tactSwitch, INPUT); // 센서값을 입력으로 설정
}
void loop ()
{
val = digitalRead(Sensor); // 센서값 읽어옴
if (val == HIGH)
{ // 장애물 감지가 안되면
noTone(Buzzer); // 버저가 울리지 않는다
delay(100);
}
else
{ // 장애물이 감지되면
tone(Buzzer, 220); // 버저가 울린다
delay(100);
Serial.print(val);
}
digitalRead(tactSwitch) ? tone(Buzzer, 220) : noTone(Buzzer);
}
hth
Have a nice day and enjoy coding in C++.
I want to make buzzer ring only when switch is pressed.
Ok thx! I'll give it a try.
jim-p
7
// Connect the switch between pin 9 and GND
int Buzzer = 7; // 버저 핀을 7번에 연결
int Tswitch = 9;
int val;
void setup () {
Serial.begin(9600);
pinMode(Buzzer, OUTPUT); // 버저를 출력으로 설정
pinMode(Tswitch, INPUT_PULLUP);
}
void loop () {
val = digitalRead(Tswitch);
if (val == HIGH) { // 장애물 감지가 안되면
noTone(7); // 버저가 울리지 않는다
delay(100);
} else { // 장애물이 감지되면
tone(7, 220); // 버저가 울린다
delay(100);
Serial.print(val);
}
}
Sir, can I ask you how should I combine breadboard??
jim-p
9
Connect your switch betweeh pin 9 and GND
system
Closed
10
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.