bogdan546 Posted June 13, 2020 Share Posted June 13, 2020 (edited) Cum pot face sa imi afiseze starea butonului pe display ? Am incercat ceva, dar nu stiu cum sa citesc starea butonului (apasat sau nu ) adica sa imi citeasca pinul de iesire la led sau daca a aparut o intrerupere sa pot sa o afisez pe display. #include <Wire.h> // include Arduino wire library (required for I2C devices) #include <Adafruit_GFX.h> // include Adafruit graphics library #include <Adafruit_SSD1306.h> // include Adafruit SSD1306 OLED display driver #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels #define OLED_RESET 4 // define display reset pin Adafruit_SSD1306 display(OLED_RESET); const int ledpin = 6; const int interruptPin = 2; volatile boolean ledOn = false; void setup(void) { pinMode (ledpin, OUTPUT); attachInterrupt(digitalPinToInterrupt(interruptPin),function1, FALLING); delay(1000); // wait a second // initialize the SSD1306 OLED display with I2C address = 0x3c display.begin(SSD1306_SWITCHCAPVCC, 0x3c); // clear the display buffer. display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE, BLACK); display.setCursor(20, 0); display.print("button"); display.setCursor(15, 15); display.print(ledOn); display.display(); // update the display display.setTextSize(1); // text size = 1 } void loop() { } void function1() { if (ledOn) { ledOn = false; digitalWrite(ledpin, LOW); delay (200); } else { ledOn = true; digitalWrite(ledpin, HIGH); delay (200); } } // end of code. Edited June 13, 2020 by bogdan546 Quote Link to comment Share on other sites More sharing options...
Liviu M Posted June 13, 2020 Share Posted June 13, 2020 Nu-i nevoie sa citesti starea pinului (nici nu stiu daca poti citi o iesire), poti folosi variabila ledOn. Tot ce ai de facut e sa afisezi variabila respectiva in loop: loop{ display.setCursor(15, 15); display.print(ledOn); display.display(); // update the display } Fara void in fata lui loop, da? Eu as "scurta" codul de tratare a intreruperii si as muta aprinderea/stingerea ledului in loop: As mai defini o variabila care sa spuna cand ai apasat butonul boolean bApasareNoua = false; As rescrie functia void function1() { ledOn ^= 1; //inverseaza valoarea lui ledOn bApasareNoua = true; //marcheaza apasare noua } Si as face toate celelalte prelucrari in loop: loop{ if(bApasareNoua){ //apasare noua bApasareNoua = false; //sterge memoria digitalWrite(ledPin, ledOn); //comanda led-ul display.setCursor(15, 15); //afiseaza starea led-ului - pozitie pe ecran display.print(ledOn); // valoare display.display(); // update the display delay (200); } } Quote Link to comment Share on other sites More sharing options...
bogdan546 Posted June 13, 2020 Author Share Posted June 13, 2020 (edited) Nu prea am inteles. Adica modific functia1 cu noua functie, mai introduc o variabila la inceput langa variabila ledOn si transform void loop in loop unde scriu codul atasat ? Edited June 13, 2020 by bogdan546 Quote Link to comment Share on other sites More sharing options...
Liviu M Posted June 14, 2020 Share Posted June 14, 2020 Daca vrei sa faci toate modificarile propuse, da. Daca vrei modificari minime in codul pe care-l ai, modifici doar functia loop cu cea din prima bucata de cod pe care am postat-o. Quote Link to comment Share on other sites More sharing options...
bogdan546 Posted June 14, 2020 Author Share Posted June 14, 2020 daca modific denumirea buclei imi da eroarea : 'loop' does not name a type Quote Link to comment Share on other sites More sharing options...
vijelie02 Posted June 14, 2020 Share Posted June 14, 2020 Pune void in fata functiei loop. Quote Link to comment Share on other sites More sharing options...
Liviu M Posted June 14, 2020 Share Posted June 14, 2020 Asa-mi trebuie daca vorbesc din amintiri. Are dreptate @vijelie02 (si era corect codul tau), trebuie un void in fata lui loop. Quote Link to comment Share on other sites More sharing options...
bogdan546 Posted June 14, 2020 Author Share Posted June 14, 2020 da, dar da alta eroare si anume ca variabila ledon nu este folosita in acest scop Quote Link to comment Share on other sites More sharing options...
Liviu M Posted June 14, 2020 Share Posted June 14, 2020 ledOn, sau ledPin? Vezi c-am scris ledPin ci P si trebuia cu p: digitalWrite(ledpin, ledOn); //comanda led-ul Quote Link to comment Share on other sites More sharing options...
bogdan546 Posted June 14, 2020 Author Share Posted June 14, 2020 Merci. Acum functioneaza . Nu mai stiu daca era ledOn sau ledpin deoarece lasesem si linia de cod in setup. Quote Link to comment Share on other sites More sharing options...
Gilbert Sparios Posted June 21, 2020 Share Posted June 21, 2020 sper ca acest cod e doar o jucărie pe care sa înveți programare sau de genul. pentru ca in realitate codul e foarte neoptimizat. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.