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);
}
}