Not sure how to get If statement working.

Hi--

I am running a DC motor controlled with a 10k potentiometer. I want to get the motor to only run when one of two push buttons/snap action switches are pushed. I have tried what other recommended by putting my potentiometer analog read and write into an if statement when the push button is low. However, I am not sure what I am doing wrong. When i upload my current code:

int analogInPin = A0;
int transistorPin = 3;
int sensorValue = 0;
int outputValue = 0;
int switch1 = 11;
int switch2 = 10;

void setup()

{

Serial.begin(9600);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(transistorPin, OUTPUT);
pinMode(switch1, INPUT_PULLUP);
pinMode(switch2, INPUT_PULLUP);

}

void loop()
{int val1 = digitalRead(switch1);
 int val2 = digitalRead(switch2);

 if (val1 == LOW)
 {
int sensorValue = analogRead(analogInPin)/4;
int outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(transistorPin, sensorValue);}
 
 if (val2 == LOW)
 {
int sensorValue = analogRead(analogInPin)/4;
int outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(transistorPin, sensorValue);}
}

it still only runs as if there is only a potentiometer. I am pretty sure everything is wired correctly.
Thanks!

davidjcshennn:
it still only runs as if there is only a potentiometer. I am pretty sure everything is wired correctly.
Thanks!

I suspect a wiring issue. Please provide a wiring diagram.

I would recommend forgetting about the motor and just try to detect the buttons being pressed by using a Serial.println debug statement.

Thanks for the response.

When I do Serial.Print (val) and push the buttons for both push buttons they are being detected fine in the serial monitor.

Do you realise you have 3 different variables each called sensorValue and another 3 called outputValue with different scopes?

Remove the 'int' from the ones in the if statement blocks. Then the global versions will be used everywhere.

Steve

Thanks for the response Steve.

So I removed int from all of the sensor value and output values and the circuit still wasn't working correctly.

I am not sure what I was doing but I got it to work by adding if statements and either switching the transistorPin or sensorValue in the analogWrite variables to 0 in the if statements. The else statements were my original if statements.

the circuit works as planned when either push button is pressed, the motor runs.

The code is attached here:

int analogInPin = A0;
int transistorPin = 3;
int sensorValue = 0;
int outputValue = 0;
int switch1 = 11;
int switch2 = 10;

void setup()

{

Serial.begin(9600);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(transistorPin, OUTPUT);
pinMode(switch1, INPUT_PULLUP);
pinMode(switch2, INPUT_PULLUP);

}

void loop()
{int val1 = digitalRead(switch1);
 int val2 = digitalRead(switch2);

 if (val1 == LOW)
 {
sensorValue = analogRead(analogInPin)/4;
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(transistorPin, 0);}
else

{ 
sensorValue = analogRead(analogInPin)/4;
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(transistorPin, sensorValue);}
 
 if (val2 == LOW)
 {
sensorValue = analogRead(analogInPin)/4;
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(0, 0);}
else
{ 
sensorValue = analogRead(analogInPin)/4;
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(transistorPin, sensorValue);}


if (sensorValue >= 160)

{
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
}

else

{ 
digitalWrite(9, HIGH);
digitalWrite(8, LOW);
}
delay(10); 
}

Can someone please explain why this works? I can attach a complete circuit and wiring diagram. Thanks1

A circuit diagram might indeed help.

Your original code didn't do anything at all with pins 8 and 9 whatever they might be connected to. The current version looks like you're driving something like an L298 but you've never mentioned any such thing nor what the 'transistorPin' does.

Steve

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.