Push button doesn't response well.
hello, trying control motor 4 push buttons.
button 3 , 4 working well.
but button 1 , 2 not working first next press.
it need pressed twice move motor again.
any idea it's problem is?
button 3 , 4 working well.
but button 1 , 2 not working first next press.
it need pressed twice move motor again.
any idea it's problem is?
code: [select]
#define dstart_point 0 //start position
#define dend_point 20000 // target position
#define ddelay_fast 25 // min delay value
#define ddelay_slow 100 // max delay value
#define dswitch_pin_1 8 //button one
#define dswitch_pin_2 9 //button two
#define dswitch_pin_3 10 //button three
#define dswitch_pin_4 11 //button four
enum
{
emotor_stop = 0,
emotor_run,
};
enum
{
eswitch_state_up = 0, // normal state
eswitch_state_down, // when pressed
};
int m_motorindex = 0;//motor position
int m_motordir = high;//motor dir
int m_motorstate = emotor_stop;//motor state
int m_switch1state = eswitch_state_up; //button 1 state
int m_switch2state = eswitch_state_up; //button 2 state
int m_switch3state = eswitch_state_up; //button 3 state
int m_switch4state = eswitch_state_up; //button 4 state
int m_motordelay= ddelay_slow; //motor delay
void setup()
{
pinmode(6, output); // motor dir
pinmode(5, output); // motor step
pinmode(13, output);
pinmode(dswitch_pin_1, input);
pinmode(dswitch_pin_2, input);
pinmode(dswitch_pin_3, input);
pinmode(dswitch_pin_4, input);
serial.begin(9600);
digitalwrite(6, high);
m_motorindex = 0;
}
void movemotor()
{
digitalwrite(5, high);
delaymicroseconds(m_motordelay);
digitalwrite(5, low);
delaymicroseconds(m_motordelay);
if(m_motordir == high)
{m_motorindex ++;}
else
{
digitalwrite(13, high);
m_motorindex --;}
}
// 1 condition: detact motor state , respose
// 2 condition: sopeed controll
// 3 condition: trap motor in moving area
void moterproc()
{
if(m_motorstate == emotor_stop) {digitalwrite(5, low);}
else if(m_motorstate == emotor_run && m_motorindex <= dend_point && m_motorindex >= dstart_point)
{movemotor();
if( (dend_point - m_motorindex) <= (ddelay_slow - ddelay_fast)) // increase speed
{
if(m_motordelay < ddelay_slow)
m_motordelay +=2;
}
else if( (m_motorindex - dstart_point) <= (ddelay_slow - ddelay_fast)) // reduce spped
{
if(m_motordelay < ddelay_slow)
m_motordelay +=2;
}
else
{
if(m_motordelay > ddelay_fast)
m_motordelay -=2;
}
}
if(m_motorindex >= dend_point || m_motorindex <= dstart_point)
{
m_motorstate = emotor_stop;
digitalwrite(5, low);
m_motordelay = ddelay_slow;
}
else
{
digitalwrite(13,low);
}
}
void pushswitch1or2()
{
if( m_motorstate == emotor_run)
{
m_motorstate = emotor_stop;
}
else if(m_motorstate == emotor_stop && (m_motorindex == dend_point || m_motorindex < 10000))
{
digitalwrite(6, high);
m_motordir = high;
m_motorstate = emotor_run;
movemotor();
}
else if(m_motorstate == emotor_stop && (m_motorindex == dstart_point || m_motorindex > 10000))
{
digitalwrite(6, low);
m_motordir = low;
m_motorstate = emotor_run;
movemotor();
}
}
void pushswitch3()
{
if(m_motorstate == emotor_stop && (m_motorindex == dend_point || m_motorindex > dstart_point))
{
digitalwrite(6, low); //
m_motordir = low;
movemotor();
}
else
{
m_motorstate = emotor_stop;
}
}
void pushswitch4()
{
if(m_motorstate == emotor_stop && (m_motorindex == dstart_point || m_motorindex < dend_point))
{
digitalwrite(6, high); //
m_motordir = high;
movemotor();
}
else
{
m_motorstate = emotor_stop;
}
}
void loop()
{
//digitalwrite(6, high); // turn left
if (digitalread(dswitch_pin_1) == high) //?? 1 ??? ?? ?
{
if(m_switch1state == eswitch_state_up)
{
m_switch1state = eswitch_state_down; //?? 1? ??? ?? ??? ??
pushswitch1or2();
}
}
else if (digitalread(dswitch_pin_1) == low)
{
m_switch1state = eswitch_state_up; //?? 1? ??? ?? ??? ??
}
if (digitalread(dswitch_pin_2) == high)
{
if(m_switch2state == eswitch_state_up)
{
m_switch2state = eswitch_state_down; //?? 2? ??? ?? ??? ??
pushswitch1or2();
}
}
else if (digitalread(dswitch_pin_2) == low)
{
m_switch2state = eswitch_state_up;
}
if (digitalread(dswitch_pin_3) == high)
{
pushswitch3();
}
else if (digitalread(dswitch_pin_3) == low)
{
if(m_switch3state == eswitch_state_down)
{
m_switch3state = eswitch_state_up;
m_motorstate = emotor_stop;
}
}
if (digitalread(dswitch_pin_4) == high)
{
pushswitch4();
}
else if (digitalread(dswitch_pin_4) == low)
{
if(m_switch3state == eswitch_state_down)
{
m_switch3state = eswitch_state_up;
m_motorstate = emotor_stop;
}
}
moterproc();
}
i'm happy put code in code tags, format too?
anyway, sounds me debounce issue, can't sure because code difficult read.
anyway, sounds me debounce issue, can't sure because code difficult read.
Arduino Forum > Using Arduino > Programming Questions > Push button doesn't response well.
arduino
Comments
Post a Comment