Arduino rotary encoder

Ak potrebujete pomoc alebo poradiť, píšte sem (len PC, elektronika atď.)...
snopy
Light Star
Light Star
Príspevky: 225
Registrovaný: 03 okt 2008, 19:43
Kontaktovať používateľa:

Arduino rotary encoder

Príspevok od používateľa snopy »

Zdravím vedel by mi niekto poradiť kto sa vyzná v arduine ? takže problem je že ked programujem rotary encoder na klasický I2C display tak funguje bez problemov ale ked vymením display za ST7920 tak už nefuguje správne väčšinou sa hodnota len zväčšuje sem tam sa ubere ... ak by niekto vedel potom sem hodím aj jednotlive kody ďakujem :)
Mek
Addict
Addict
Používateľov profilový obrázok
Príspevky: 4661
Registrovaný: 23 mar 2005, 23:00
Bydlisko: ZA <-> TN
Kontaktovať používateľa:

Re: Arduino rotary encoder

Príspevok od používateľa Mek »

V arduine sa sice nevyznam, ale typickym neduhom jeho kniznic je, ze pouzivaju navzajom tie iste porty alebo timery a tak si "lezu do kapusty". Skontroluj, ci tam nemas takyto konflikt.
snopy
Light Star
Light Star
Príspevky: 225
Registrovaný: 03 okt 2008, 19:43
Kontaktovať používateľa:

Re: Arduino rotary encoder

Príspevok od používateľa snopy »

knižnicu používam len pre display .. ale pre istotu som ju pozrel ale nič som tam nenašiel žeby bol nejaký problém...
tu su kody (prvy funguje druhy nie)

Kód: Vybrať všetko

#include <LiquidCrystal_I2C.h> 
 LiquidCrystal_I2C lcd(0x27,16,2); 
// defines pins numbers
 #define stepPin 10 
 #define dirPin  9
 #define outputA 3
 #define outputB 2
 int counter = 0;
 int angle = 0; 
 int aState;
 int aLastState;  
 
void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  pinMode (outputA,INPUT);
  pinMode (outputB,INPUT);
  
  aLastState = digitalRead(outputA);
}
void loop() {
  aState = digitalRead(outputA);
  
  if (aState != aLastState){     
     if (digitalRead(outputB) != aState) { 
       counter ++;
       angle ++;
       rotateCW();  
     }
     else {
       counter--;
       angle --;
       rotateCCW(); 
     }
     if (counter >=30 ) {
      counter =0;
     }
     

     lcd.print(int(angle*(-1)));
     lcd.setCursor(0,0);
     
   }
  aLastState = aState;
}
void rotateCW() {
  digitalWrite(dirPin,LOW);
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(2000); 
}
void rotateCCW() {
  digitalWrite(dirPin,HIGH);
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(2000);   
}

Kód: Vybrať všetko

#include "U8glib.h"
U8GLIB_ST7920_128X64_1X u8g(7, 6, 5 ,8);
// defines pins numbers
 #define stepPin 10 
 #define dirPin  9
 #define outputA 3
 #define outputB 2
 int counter = 0;
 int angle = 0; 
 int aState;
 int aLastState;

void setup() {
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  pinMode (outputA,INPUT);
  pinMode (outputB,INPUT);

  aLastState = digitalRead(outputA); 
}
void loop() {
  aState = digitalRead(outputA);
  
  if (aState != aLastState){     
     if (digitalRead(outputB) != aState) { 
       counter ++;
       angle ++;
       rotateCW();  
     }
     else {
       counter--;
       angle --;
       rotateCCW(); 
     }
     if (counter >=30 ) {
      counter =0;
     }
     

//     u8g.print(int(angle*(-1)));
//     u8g.setPrintPos(2, 30);
     
   }
  aLastState = aState;

  u8g.firstPage(); 
  do {
    draw();
  } while( u8g.nextPage() );
}

   void draw(void) {
    u8g.setPrintPos(2, 30);
    u8g.setFont(u8g_font_courB14); 
    u8g.print(int(angle*(-1)));
    u8g.print("C");
   }

void rotateCW() {
  digitalWrite(dirPin,LOW);
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(2000); 
}
void rotateCCW() {
  digitalWrite(dirPin,HIGH);
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(2000);   
}
Mek
Addict
Addict
Používateľov profilový obrázok
Príspevky: 4661
Registrovaný: 23 mar 2005, 23:00
Bydlisko: ZA <-> TN
Kontaktovať používateľa:

Re: Arduino rotary encoder

Príspevok od používateľa Mek »

Mal by som podozrenie, ze ten do/while cyklus trva prilis dlho a tak ostatny kod niekedy nezareaguje na otocenie encodera.
Napísať odpoveď