Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Arduino ILI9341 TFT Display Pitanja
#24
Code:
// include library
#include <LiquidCrystal.h>
#include <Encoder.h>

// LCD interface pins
int RS = A4, EN = A5, D4 = A3, D5 = A2, D6 = A1, D7 = A0;
LiquidCrystal lcd(RS, EN, D4, D5, D6, D7);

// ROTARY encoder pins
int switchPin = 0;                    // button pin
int switchState = HIGH;               // button value

int pinA = 2;                         // rotary encoder PinA
int pinB = 3;                         // rotary encoder PinB

int tempSet = 0;                      // temperature set value

void setup() {
 // Enable the switchPin as input with a PULLUP resistor
 pinMode(switchPin, INPUT_PULLUP);

 // Set PinA and PinB as input
 pinMode(pinA, INPUT_PULLUP);        // turn on pull-up resistor        
 pinMode(pinB, INPUT_PULLUP);        // turn on pull-up resistor

 // Attach a CHANGE interrupt to PinB and execute the doEncoder function when this change occurs
 attachInterrupt(digitalPinToInterrupt(pinB), doEncoder, FALLING);
 
 // LCD number of columns and rows
 lcd.begin(16, 2);
 lcd.clear();
 lcd.print("Hello, World!");
}

void loop() {
 // Read the digital value of the switch (LOW/HIGH)
 switchState = bitRead(PIND, switchPin);

 // If the switch is pressed (LOW), execute MENU function
 if (switchState == LOW) {
   lcd.clear();
   lcd.print("SWITCH PRESSED...");
 }
}

// Rotary Encoder - increase/decrease temp by 5°C
void doEncoder() {
 // If there is a minimal movement of 1 step
 if (bitRead(PIND, pinA) == bitRead(PIND, pinB)) {
     // Increase set temperature by 5°C
     tempSet = tempSet + 5;
 } else {
     // Decrease set temperature by 5°C
     tempSet = tempSet - 5;
 }
 
 lcd.clear();
 lcd.print(tempSet);
}

Navedeni gore kod treba da na LCD ispisuje brojku od 0 do npr 450 (to cu kasnije staviti MAX i MIN brojača tempSet).
Rotary Encoder je spojeni na arduino pro mini na pinove 2 i 3 (A(INT0) i B(INT1)) i switch button na pin0.
Zadatak je da kad se okreće lijevo enkoder da tempSet se smanjuje za 5 a kad se okreće desno da se povećava do 5 i ispisuje vrijednost na LCD-u.

Kod mene se događa da kada idem polako okretat desno nekad trebam okrenuti dva puta, nekad jednom(dakle to je OK), nekad tri puta da se poveca varijabla za 5. Isto vrijedi i za lijevo samo tamo se smanjuje za 5, ali nekada se smanji za jedan okretaj(sto je OK), nekad za dva a nekad za tri.

Radim preko FALLING edge što po googlu kazu da je najsigurniji način...no kod mene su bugovi...

Čak mi se događa da mi se brojač kad lovi iz prvog okretaja polako povećava za 10 a trebao bi za 5....pa me zanima gdje griješim gore u kodu? Čitao sam google primjere i library ali očito sam nešto propustio....
Reply


Messages In This Thread
Arduino ILI9341 TFT Display Pitanja - by ronovar - 10-08-2018, 12:48 PM
RE: Arduino ILI9341 TFT Display Pitanja - by ronovar - 04-12-2019, 09:02 PM

Forum Jump:


Users browsing this thread: 7 Guest(s)