here's an easy one
...for someone, perhaps not me.
i have been working on program system close projects i've sorted out before, can't seem one. system is:
arduino mega; 3 inputs (one optocoupler input, 2 rocker switches), 1 dc motor through h bridge.
the basic steps program are:
upon first input of signal a, motor runs forward until input limiting switch 1.
upon second input of signal a, motor runs backward until input limiting switch 2.
repeat.
signal come in pairs, @ end of each paired set of motor actions, device @ starting position.
here code have far. currently, once first signal received (main_val) not stop motor input limiting switch. have used similar code in past, projects 5 or 6 sequenced actions. don't know why isn't working here.
(cleaned code little, still not working right)
i have achieved right functions here losing 2 rocker switches , using stepper instead, smoother motion. if there different code structure need here please let me know.
all switch/motor physical functions, pin assignments, etc., have been troubleshot.
thanks!
i have been working on program system close projects i've sorted out before, can't seem one. system is:
arduino mega; 3 inputs (one optocoupler input, 2 rocker switches), 1 dc motor through h bridge.
the basic steps program are:
upon first input of signal a, motor runs forward until input limiting switch 1.
upon second input of signal a, motor runs backward until input limiting switch 2.
repeat.
signal come in pairs, @ end of each paired set of motor actions, device @ starting position.
here code have far. currently, once first signal received (main_val) not stop motor input limiting switch. have used similar code in past, projects 5 or 6 sequenced actions. don't know why isn't working here.
(cleaned code little, still not working right)
code: [select]
int motor1 = 26; //declares first pin motor
int motor2 = 27; //declares other pin motor
int motorpmw = 9; // pmw set how battery power motor getting (speed)
int soundpin = 22;
int stop1 = 24;
int stop2 = 25;
int main_val = 0;
int val1 = 0;
int val2 = 0;
int ctrl = 0;
int ctrl2 = 0;
void setup()
{
pinmode(motor1, output); //
pinmode(motor2, output); // these declaring them outputs
pinmode(soundpin, input);
pinmode(stop1, input);
pinmode(stop2, input);
}
void loop()
{
main_val = digitalread(soundpin);
val1 = digitalread(stop1);
val2 = digitalread(stop2);
delay(10);
if (main_val == high)
{
ctrl = ctrl+1;
}
if (ctrl == 1)
{
digitalwrite(motor1, high);
digitalwrite(motor2, low);
if (val1 == high)
{
digitalwrite(motor1, low);
digitalwrite(motor2, low);
ctrl = 2;
}
}
if (ctrl == 3)
{
digitalwrite(motor1, low);
digitalwrite(motor2, high);
if (val2 == high)
{
digitalwrite(motor1, low);
digitalwrite(motor2, low);
ctrl = 0;
}
}
}
i have achieved right functions here losing 2 rocker switches , using stepper instead, smoother motion. if there different code structure need here please let me know.
all switch/motor physical functions, pin assignments, etc., have been troubleshot.
thanks!
i'd create 2 boolean values - goingforward , goingbackward.
set goingforward true , goingbackward false when main_val high , ctrl == 0. increment ctrl @ same time.
set goingbackward true , goingbackward false when main_val high , ctrl == 1. decrement ctrl @ same time.
then, decide if goingforward true, , if goingbackward true.
there not point in checking backwards limit switch when going forward or stopped, , not point in checking forwards limit switch when going backwards or stopped.
set goingforward true , goingbackward false when main_val high , ctrl == 0. increment ctrl @ same time.
set goingbackward true , goingbackward false when main_val high , ctrl == 1. decrement ctrl @ same time.
then, decide if goingforward true, , if goingbackward true.
there not point in checking backwards limit switch when going forward or stopped, , not point in checking forwards limit switch when going backwards or stopped.
Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > here's an easy one
arduino
Comments
Post a Comment