Differences
This shows you the differences between two versions of the page.
— |
hu:arduino:switch [2022/04/21 15:02] () |
||
---|---|---|---|
1: | 1: | ||
+ | {{ : | ||
+ | ====== Kapcsolók, nyomógombok ====== | ||
+ | ===== Kapcsolók, nyomógombok hardveres bekötései ===== | ||
+ | |||
+ | ==== 5V HI aktív kapcsoló ==== | ||
+ | {{anchor: | ||
+ | {{: | ||
+ | |||
+ | A fenti képen egy HI aktív kapcsolóbekötés látható. Az áramkör nyitott, nyugalmi helyzetében az Arduino egy 10 kΩ-os (lehúzó) ellenálláson keresztül a grundra van kötve, így " | ||
+ | |||
+ | {{: | ||
+ | forrás: https:// | ||
+ | |||
+ | A lenti példaprogram a nyomógomb állapota alapján a beépített LED-et kapcsolgatja. Alatta a példaprogram az egyszerű befűzéshez (copy-paste): | ||
+ | < | ||
+ | /* | ||
+ | Button | ||
+ | |||
+ | Turns on and off a light emitting diode(LED) connected to digital pin 13, | ||
+ | when pressing a pushbutton attached to pin 2. | ||
+ | |||
+ | The circuit: | ||
+ | - LED attached from pin 13 to ground | ||
+ | - pushbutton attached to pin 2 from +5V | ||
+ | - 10K resistor attached to pin 2 from ground | ||
+ | |||
+ | - Note: on most Arduinos there is already an LED on the board | ||
+ | attached to pin 13. | ||
+ | |||
+ | created 2005 | ||
+ | by DojoDave < | ||
+ | modified 30 Aug 2011 | ||
+ | by Tom Igoe | ||
+ | |||
+ | This example code is in the public domain. | ||
+ | |||
+ | http:// | ||
+ | */ | ||
+ | |||
+ | // constants won't change. They' | ||
+ | const int buttonPin = 2; // the number of the pushbutton pin | ||
+ | const int ledPin = 13; // the number of the LED pin | ||
+ | |||
+ | // variables will change: | ||
+ | int buttonState = 0; // variable for reading the pushbutton status | ||
+ | |||
+ | void setup() { | ||
+ | // initialize the LED pin as an output: | ||
+ | pinMode(ledPin, | ||
+ | // initialize the pushbutton pin as an input: | ||
+ | pinMode(buttonPin, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | // read the state of the pushbutton value: | ||
+ | buttonState = digitalRead(buttonPin); | ||
+ | |||
+ | // check if the pushbutton is pressed. If it is, the buttonState is HIGH: | ||
+ | if (buttonState == HIGH) { | ||
+ | // turn LED on: | ||
+ | digitalWrite(ledPin, | ||
+ | } else { | ||
+ | // turn LED off: | ||
+ | digitalWrite(ledPin, | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Mindez tömörebben (copy-paste-hez): | ||
+ | < | ||
+ | const int buttonPin = 2; // the number of the pushbutton pin | ||
+ | |||
+ | // variables will change: | ||
+ | int buttonState = 0; // variable for reading the pushbutton status | ||
+ | |||
+ | void setup() { | ||
+ | // initialize the pushbutton pin as an input: | ||
+ | pinMode(buttonPin, | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | // read the state of the pushbutton value: | ||
+ | buttonState = digitalRead(buttonPin); | ||
+ | |||
+ | if (buttonState == HIGH) { | ||
+ | // button state on: | ||
+ | ; | ||
+ | } else { | ||
+ | // button state off: | ||
+ | ; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | ==== 5V LO aktív kapcsoló ==== | ||
+ | |||
+ | {{: | ||
+ | |||
+ | A fenti képen egy LO aktív kapcsolóbekötés látható. Az áramkör nyitott, nyugalmi helyzetében az Arduino egy 10 kΩ-os (felhúzó) ellenálláson keresztül a tápra (ajánlott egy 100Ω-oes ellenállást azért elékötni) van kötve, így " | ||
+ | |||
+ | ==== Belső felhúzóellenállás ==== | ||
+ | {{anchor: | ||
+ | Az Arduino-kben található belső felhúzóellenállás is, mely board-típusonként változó, az Uno-nál ez 20kΩ - 50kΩ, míg a Due-nél 50kΩ - 150kΩ között változik ennek az éréke. Ez a belső ellenállás kétféle módon kapcsolható be: | ||
+ | |||
+ | Hivatalosan így: | ||
+ | < | ||
+ | pinMode(pin, | ||
+ | </ | ||
+ | de ez nem minden esetben működik, több board-nál a fordító hibát dod rá. Ilyenkor a megoldás: | ||
+ | < | ||
+ | pinMode(pin, | ||
+ | digitalWrite(pin, | ||
+ | </ | ||
+ | |||
+ | Nyilván ezzel a módszerrel legalább egy ellenállás megspórolható az [[hu: | ||
+ | |||
+ | ==== 12V/24V HI aktív kapcsoló ==== | ||
+ | |||
+ | {{: | ||
+ | |||
+ | Az [[hu: | ||
+ | |||
+ | 24V-os táppal a kapcsolás: \\ | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ==== 5V 8 párhuzamos kapcsoló ==== | ||
+ | A lenti példán 8 nyomógomb beolvasása látható egy analóg bemeneten keresztül. A nyomógombok bemeneti ellenállásai 100kΩ-mal vannak egymástól elválasztva, | ||
+ | |||
+ | {{: | ||
+ | |||
+ | A lenti kódban a fényképnek megfelelően a lenyomott nyomógomb kódját kiírja a kétsoros I2C LCD-kijelző és a szerial-monitor is. | ||
+ | |||
+ | Pontatlan működés esetén a nyomógombokhoz tartozó minimális analóg-értékeket (mins array) be kell állítani! | ||
+ | |||
+ | Alapértelmezett kimenet (gombnyomás nélkül): 9. | ||
+ | |||
+ | {{: | ||
+ | === Példaprogram === | ||
+ | < | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | |||
+ | #define I2C_ADDR | ||
+ | #define BACKLIGHT_PIN | ||
+ | #define En_pin | ||
+ | #define Rw_pin | ||
+ | #define Rs_pin | ||
+ | #define D4_pin | ||
+ | #define D5_pin | ||
+ | #define D6_pin | ||
+ | #define D7_pin | ||
+ | |||
+ | int sw = 0; | ||
+ | int mins[] = {1024, 1000, 800, 740, 640, 560, 520, 480, 400, -100}; | ||
+ | |||
+ | LiquidCrystal_I2C | ||
+ | |||
+ | int sensorPin = A0; | ||
+ | int sensorValue = 0; | ||
+ | int ledPin = 13; // select the pin for the LED | ||
+ | |||
+ | void setup() { | ||
+ | pinMode(ledPin, | ||
+ | Serial.begin(9600); | ||
+ | while (!Serial) ; // wait for serial | ||
+ | delay(200); | ||
+ | lcd.begin (16,2); | ||
+ | | ||
+ | // Switch on the backlight | ||
+ | lcd.setBacklightPin(BACKLIGHT_PIN, | ||
+ | lcd.setBacklight(HIGH); | ||
+ | lcd.home (); // go home | ||
+ | lcd.print(" | ||
+ | lcd.setCursor ( 0, 1 ); // go to the 1nd line | ||
+ | lcd.print(" | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | // read the value from the sensor: | ||
+ | sensorValue = analogRead(sensorPin); | ||
+ | sw = 0; | ||
+ | for (int i = 1; sw == 0; i++) { | ||
+ | if (sensorValue > mins[i]) { | ||
+ | sw = i; | ||
+ | } | ||
+ | } | ||
+ | Serial.print (sensorValue); | ||
+ | Serial.print (" >> button pressed: "); | ||
+ | Serial.print (sw); | ||
+ | Serial.println (" (9: none) " | ||
+ | | ||
+ | lcd.setCursor ( 0, 1 ); // go to the 1nd line | ||
+ | lcd.print(sw); | ||
+ | |||
+ | delay(1000); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== 5V 3 állású kiválasztókapcsoló ==== | ||
+ | A háromállású kapcsolót az egyik programhoz akartam beolvasni, és nem egyet, hanem rögtön négyet, de nem volt már ennyi szabad digitális pin. Viszont volt még 4 szabad analóg bemenet, ezek tökéletesen megfeleltek a célnak. Az alábbi bekötést követően a grund-ra kötött bemenet 0-t, az 5V-ra 1024-et, a középső állás pedig nagyjából 600-at ad eredményként: | ||
+ | |||
+ | {{: |