Код (Text): #define BUZZER_PIN 8 #define switchPin 2 int val=0; void setup() { // put your setup code here, to run once: pinMode(BUZZER_PIN,OUTPUT); pinMode(switchPin,INPUT); } void loop() { val=digitalRead(switchPin); if (val==HIGH) { tone(BUZZER_PIN, 1500, 20); } // put your main code here, to run repeatedly: }
А так? Код (Text): #define BUZZER_PIN 8 #define switchPin 2 int val=0; void setup() { // put your setup code here, to run once: pinMode(BUZZER_PIN,OUTPUT); pinMode(switchPin,INPUT); } void loop() { val=digitalRead(switchPin); if (val==HIGH) { tone(BUZZER_PIN, 1500); } }
работает но наоборот когда я нажимаю перестает пищят а когда отпускаю пищитю а также когда я добавил еще кнопку начал исходить ужасный звук
А так? Код (Text): const byte sound_pin = 8; // вывод 8 Arduino для динамика void setup() { } void loop () { tone(sound_pin, 1915); // воспроизводим сигнал с частотой 1915 Гц delay(1000); // ждём 1000 мс (длительность воспроизведения сигнала) tone(sound_pin, 1700); delay(1000); tone(sound_pin, 1519); delay(1000); tone(sound_pin, 1432); delay(1000); tone(sound_pin, 1275); delay(1000); tone(sound_pin, 1136); delay(1000); tone(sound_pin, 1014); delay(1000); noTone(sound_pin); // выключаем звук }
@temkase, как у вас подключена кнопка? Если к земле - включите встроенную подтяжку pinMode(switchPin,INPUT_PULLUP) и при сранивайте с нулевым значением при нажатии if (val==LOW). Код (C++): #define BUZZER_PIN 8 #define switchPin 2 int val=0; void setup() { // put your setup code here, to run once: pinMode(BUZZER_PIN,OUTPUT); pinMode(switchPin,INPUT_PULLUP); } void loop() { val=digitalRead(switchPin); if (val==LOW) tone(BUZZER_PIN, 750, 500); }