Jump to content
ELFORUM - Forumul electronistilor

afisare stare buton pe oled 0.96


bogdan546
 Share

Recommended Posts

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 by bogdan546
Link to comment
Share on other sites

  • Replies 10
  • Created
  • Last Reply

Top Posters In This Topic

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);
  }
}

 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share




×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.Terms of Use si Guidelines