Incremental Encoder and interrupt problem.
hello everybody.
i found serious big problem small project im making. i'll try explain briefly setup. im using arduino mega drive 12v relay through tip122(pin 54, im using a0 digital output), relay activates ac motor. shaft of motor connected encoder(100steps per rev) connected arduino(ch.a pin 2 , ch. b pin 3). im using keypad input password, if it's correct motor starts , should stop after 500 steps of encoder. motor has electrical brake, stops "almost" after relay goes open state, im using code detect how many steps of encoder takes motor stop. i've detected takes 10 12 more steps after relay open.
the program runs nice few times showing consistent values between 510 , 512 once every 5 times or so, decides stop @ 265 or 267. take -10~12 , seems program stops after step counter hits 255 or 256, interesting because that's byte assume has code, maybe because variable volatile, dont know if has timer0 counter 8bits, (i think timer manages pin 2 on mega timer3 though).
the code this:
so main question
1. why stop happens when counter hits 255?
but want with:
2. next improvement input number of steps through keypad , press enter or key motor moves until gets desired step input, when press key on keypad, should store every key press in array , make integer of it? or there library handles key presses? (i mean, if press 8 4 0, i'll 8 4 0, how manage store 840?)
3. there better option control motor different making program wait while(desiredstepnumber); , way update lcd shows change of step counter?
finally, apologize bad english , in advance help.
jhon.
i found serious big problem small project im making. i'll try explain briefly setup. im using arduino mega drive 12v relay through tip122(pin 54, im using a0 digital output), relay activates ac motor. shaft of motor connected encoder(100steps per rev) connected arduino(ch.a pin 2 , ch. b pin 3). im using keypad input password, if it's correct motor starts , should stop after 500 steps of encoder. motor has electrical brake, stops "almost" after relay goes open state, im using code detect how many steps of encoder takes motor stop. i've detected takes 10 12 more steps after relay open.
the program runs nice few times showing consistent values between 510 , 512 once every 5 times or so, decides stop @ 265 or 267. take -10~12 , seems program stops after step counter hits 255 or 256, interesting because that's byte assume has code, maybe because variable volatile, dont know if has timer0 counter 8bits, (i think timer manages pin 2 on mega timer3 though).
the code this:
code: [select]
#include <password.h>
#include <liquidcrystal.h>
#include <keypad.h>
password password = password( "9" );
liquidcrystal lcd(33, 31, 29, 27, 25, 23); // lcd(rs,e,d4,d5,d6,d7)
const byte rowslcd=2;
const byte colslcd=16;
const byte rowskbd= 4;
const byte colskbd = 4;
char keys[rowskbd][colskbd] = {
{
'1', '2', '3', 'u' }
,
{
'4', '5', '6', 'd' }
,
{
'7', '8', '9', 'e' }
,
{
'.', '0', 'c', 'm' }
};
byte rowspins[rowskbd] = {
53,51,49,47}; // rows 1, 2, 3 y 4
byte colspins[colskbd] = {
45,43,41,39}; // columns 1, 2, 3 y 4
const int motorpinfwd=54;
const int motorpinrev=55;
const int encodercha=2;
const int encoderchb=3;
volatile int encoderpos=0;
keypad kbd=keypad(makekeymap(keys),rowspins,colspins,rowskbd, colskbd);
void setup() {
lcd.begin(colslcd, rowslcd);
pinmode(motorpinfwd, output);
pinmode(motorpinrev, output);
pinmode(13,output);
pinmode(encoderchb, input);
pinmode(encodercha, input);
lcd.setcursor(2,0);
lcd.print("brake test");
delay(2000);
lcd.clear();
attachinterrupt(0,doencoder,change);
//attachinterrupt(1,doencoderb,change);
}
void loop() {
char key = kbd.getkey();
if( key != 0) {
lcd.print(key);
delay(10);
switch (key) {
case 'e':
checkpassword();
delay(1);
break; // enter password
case 'c':
password.reset();
delay(1);
lcd.clear();
break; // clear buffer
default:
password.append(key);
delay(1); // add key password
}
}
}
void checkpassword() {
if (password.evaluate()) {
lcd.setcursor(0,1);
lcd.print("cool");
digitalwrite(motorpinfwd, high);
while(encoderpos<500);
digitalwrite(motorpinfwd, low);
lcd.clear();
delay(100);
lcd.print(encoderpos);
delay(2500);
password.reset();
delay(1);
encoderpos=0;
}
else {
lcd.clear();
lcd.print("not cool");
delay(10);
password.reset();
delay(1);
(int t = 0; t < 12; t++) {
digitalwrite(13, high); // turn red light on
delay(80); // wait .08 seconds
digitalwrite(13, low); // turn off
delay(80); // wait .08 seconds
}
}
lcd.clear();
}
void doencoder() {
if (digitalread(encodercha) == digitalread(encoderchb))
encoderpos++;
else
encoderpos--;
}
so main question
1. why stop happens when counter hits 255?
but want with:
2. next improvement input number of steps through keypad , press enter or key motor moves until gets desired step input, when press key on keypad, should store every key press in array , make integer of it? or there library handles key presses? (i mean, if press 8 4 0, i'll 8 4 0, how manage store 840?)
3. there better option control motor different making program wait while(desiredstepnumber); , way update lcd shows change of step counter?
finally, apologize bad english , in advance help.
jhon.
it might because encoderpos int (16 bit) value , code may read while being changed interrupt. should read value variable , use variable in code. , turn off interrupts while reading encoderpos both parts of int without 1 part being changed.
for example
...r
for example
code: [select]
nointerrupts();
savedencoderpos = encoderpos;
interrupts()
...r
Arduino Forum > Using Arduino > Project Guidance > Incremental Encoder and interrupt problem.
arduino
Comments
Post a Comment