...for someone, but perhaps not for me.
I have been working on a program for a system that is very close to projects i've sorted out before, but i can't seem to get this one. The system is:
Arduino mega; three inputs (one optocoupler as input, two rocker switches), one DC motor through h bridge.
The basic steps to the program are:
upon first input of Signal A, motor runs forward until input from limiting switch 1.
upon second input of signal A, motor runs backward until input from limiting switch 2.
repeat.
Signal A will always come in pairs, so at the end of each paired set of motor actions, the device will be back at the starting position.
Here is the code I have so far. Currently, once the first signal A is received (main_val) it will not stop the motor with input from the limiting switch. I have used very similar code in the past, but for projects with five or six sequenced actions. Don't know why it isn't working here.
(cleaned up the code a little, but still not working right)
int motor1 = 26; //declares the first pin for the motor
int motor2 = 27; //declares the other pin for the motor
int motorpmw = 9; // this is the pmw that will set how much battery power the motor is 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 simply are declaring them as 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 already achieved the right functions here by losing the two rocker switches and using a stepper instead, but i would like a smoother motion. If there is a completely different code structure i need here please let me know.
All switch/motor physical functions, pin assignments, etc., have been troubleshot.
thanks!