Arduino rotary encoder
Arduino rotary encoder
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
- Príspevky: 4661
- Registrovaný: 23 mar 2005, 23:00
- Bydlisko: ZA <-> TN
- Kontaktovať používateľa:
Re: Arduino rotary encoder
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.
Re: Arduino rotary encoder
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)
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
- Príspevky: 4661
- Registrovaný: 23 mar 2005, 23:00
- Bydlisko: ZA <-> TN
- Kontaktovať používateľa:
Re: Arduino rotary encoder
Mal by som podozrenie, ze ten do/while cyklus trva prilis dlho a tak ostatny kod niekedy nezareaguje na otocenie encodera.