H Bridge Direction button
hello have simple h-bridge set (attached) , here code have far.
int enablepin = 11;
int in1pin = 10;
int in2pin = 9;
int switchpin = 7;
int potpin = 0;
void setup()
{
pinmode(in1pin, output);
pinmode(in2pin, output);
pinmode(enablepin, output);
pinmode(switchpin, input_pullup);
}
void loop()
{
int speed = analogread(potpin) / 4;
boolean reverse = digitalread(switchpin);
setmotor(speed, reverse);
}
void setmotor(int speed, boolean reverse)
{
analogwrite(enablepin, speed);
digitalwrite(in1pin, ! reverse);
digitalwrite(in2pin, reverse);
}
im using reed switch push button changes direction when being held down. how can make switch direction permanently until pushed again?
int enablepin = 11;
int in1pin = 10;
int in2pin = 9;
int switchpin = 7;
int potpin = 0;
void setup()
{
pinmode(in1pin, output);
pinmode(in2pin, output);
pinmode(enablepin, output);
pinmode(switchpin, input_pullup);
}
void loop()
{
int speed = analogread(potpin) / 4;
boolean reverse = digitalread(switchpin);
setmotor(speed, reverse);
}
void setmotor(int speed, boolean reverse)
{
analogwrite(enablepin, speed);
digitalwrite(in1pin, ! reverse);
digitalwrite(in2pin, reverse);
}
im using reed switch push button changes direction when being held down. how can make switch direction permanently until pushed again?
code: [select]
boolean reverse, laststate; // set global, @ top of code
.
.
.
//inside loop function
if(digitalread(switchpin) == low && digitalread(switchpin) != laststate) // looking see if button state low , not equal last state.
{
reverse = !reverse; // change when button state low
}
laststate = digitalread(switchpin);
had make slight edit.
Arduino Forum > Using Arduino > Programming Questions > H Bridge Direction button
arduino
Comments
Post a Comment