micro switch and if statements

Hello everyone,

I have a problem with coding two micro-switches with arduino , I'm using water sensor to operate a signal to the micro-controller, thus; if the sensor readings is within the range it should then rotate the motor which it will keep rotating until the first micro-switch is pressed then it will rotate the opposite direction i.e. towards the second micro switch, and the second micro switch will stop the motor from rotating if pressed.

the problem now is; the first micro-switch does not wok if the i pressed it when the sensor reading within the range.

my code is:
const int buttonPin = 6; //push button number 1 (clockwise) attahced into D6
const int buttonPin2 = 7; //push button number 2 (anti-clockwise) attahced into D7
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState2 = 0;

int motor_R = 8;
int motor_S = 10;
int motor_L = 9;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(motor_R , OUTPUT);
pinMode(motor_S , OUTPUT);
pinMode(motor_L , OUTPUT);

// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);

Serial.begin(9600);
}

void loop() {
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);

int sensorValue = analogRead (A0);

Serial.println (sensorValue);
delay (900);

if (sensorValue <=550){
digitalWrite(ledPin, HIGH);
digitalWrite (motor_R,HIGH);
delay(500);
digitalWrite (motor_L,HIGH);

if(buttonState2 == HIGH)
{
digitalWrite(ledPin, LOW);
digitalWrite (motor_R,LOW);
delay(500);
digitalWrite (motor_L,LOW);

}
}
/***********UPWORD/
if (buttonState2 == HIGH) {
// turn LED on:
digitalWrite(ledPin, LOW);
digitalWrite (motor_R,LOW);
delay(500);
digitalWrite (motor_L,LOW);

}

if (buttonState == HIGH) {
digitalWrite (motor_S,HIGH);

}

}

const int buttonPin = 6;     //push button number 1 (clockwise) attahced into D6
const int buttonPin2 = 7;    //push button number 2 (anti-clockwise) attahced into D7

If you are going to number one variable in a series, number ALL of them.

How ARE the switches wired? Why are you not using the internal pullup resistors?

thanx for reply. i m using pull up resisters. they work fine when the reading is out of the range.

could u explain more plz

i m using pull up resisters. they work fine when the reading is out of the range.

Your indenting makes it nearly impossible to follow the code.

Use Tools + Auto Format to make the indenting reasonable. Consistent placement of the { would help, too. Always on a new line is my preference. But sometimes on a new line and sometimes on the same line is just too hard to deal with.

You are testing the value of buttonState2 and take action when it is HIGH both if the sensor value is in range and without checking the sensor value. That does not seem right.