Stap 5: Drukknop in het project toe te voegen
Nu zijn we het toevoegen van knoppen
Ik gebruik vier knop kunt u het volgens voorschrift
#include <SoftwareSerial.h> // SoftwareSerial mySerial(1, 0); // 1-RX, 0-TX - #include <LiquidCrystal.h>int ch1 = A0; int ch2 = A1; int ch3 = A2; int ch4 = A3; LiquidCrystal lcd(12, 11, 5, 4, 3, 2);;//(rs, enable, d4, d5, d6, d7) int switchPin[] = {6,7,8,9}; void setup() { mySerial.begin(9600); pinMode(ch1, OUTPUT); pinMode(ch2, OUTPUT); pinMode(ch3, OUTPUT); pinMode(ch4, OUTPUT);lcd.begin(16, 2);pinMode(switchPin[0], INPUT); digitalWrite(switchPin[0], HIGH); pinMode(switchPin[1], INPUT); digitalWrite(switchPin[1], HIGH); pinMode(switchPin[2], INPUT); digitalWrite(switchPin[2], HIGH); pinMode(switchPin[3], INPUT); digitalWrite(switchPin[3], HIGH); pinMode(switchPin[4], INPUT); digitalWrite(switchPin[4], HIGH); } void loop() { char caracter = mySerial.read(); //--------------------------------ch1--------------------- if(caracter == 'A') { digitalWrite(ch1,HIGH); lcd.setCursor(0, 0); lcd.print("r1 ON");}if(caracter == 'a') { digitalWrite(ch1,LOW); lcd.setCursor(0, 0); lcd.print("r1 OFF"); } //---------------------------------ch1---------------------- //---------------------------------ch2--------------------- if(caracter == 'B') { digitalWrite(ch2,HIGH); lcd.setCursor(8, 0); lcd.print("r2 ON"); }if(caracter == 'b') { digitalWrite(ch2,LOW); lcd.setCursor(8, 0); lcd.print("r2 OFF"); } //---------------------------------ch2---------------------- //---------------------------------ch3--------------------- if(caracter == 'C') { digitalWrite(ch3,HIGH); lcd.setCursor(0, 1); lcd.print("r3 ON"); }if(caracter == 'c') { digitalWrite(ch3,LOW); lcd.setCursor(0, 1); lcd.print("r3 OFF"); } //---------------------------------ch3---------------------- //---------------------------------ch4--------------------- if(caracter == 'D') { digitalWrite(ch4,HIGH); lcd.setCursor(8, 1); lcd.print("r4 ON"); }if(caracter == 'd') { digitalWrite(ch4,LOW); lcd.setCursor(8, 1); lcd.print("r4 OFF"); } //---------------------------------ch4---------------------- if (digitalRead(switchPin[0]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch1, HIGH) ; lcd.setCursor(0, 0); lcd.print("r1 ON"); } if (digitalRead(switchPin[0]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch1, LOW) ; lcd.setCursor(0, 0); lcd.print("r1 OFF"); } if (digitalRead(switchPin[1]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch2, HIGH) ; lcd.setCursor(8, 0); lcd.print("r2 ON"); } if (digitalRead(switchPin[1]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch2, LOW) ; lcd.setCursor(8, 0); lcd.print("r2 OFF"); } if (digitalRead(switchPin[2]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch3, HIGH) ; lcd.setCursor(0, 1); lcd.print("r3 ON"); } if (digitalRead(switchPin[2]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch3, LOW) ; lcd.setCursor(0, 1); lcd.print("r3 OFF"); } if (digitalRead(switchPin[3]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch4, HIGH) ; lcd.setCursor(8, 1); lcd.print("r4 ON");// toggle running variable } if (digitalRead(switchPin[3]) == LOW) { // switch is pressed - pullup keeps pin high normally digitalWrite(ch4, LOW) ; lcd.setCursor(8, 1); lcd.print("r4 OFF"); } delay(0); }