Page 1 of 1

Motor rotation confusing.

PostPosted: Sat Mar 07, 2015 12:42 am
by alung92
my coding was regarding about a rain sensor to active the rotation of motor with 2 limit switch connecting to same digital inout , but i facing the problem which 2 condition can interact both condition (if.....else). the motor start confuse , keep reverse and forward. plz someone help me solve this problem for me . i need end the first condition statement before proceed to next statement. Even thou i used while(1), it's didn't working.



CODE: SELECT_ALL_CODE
int rainsensor=1;
int rainvalue=0;
int rain_sensitivity=800;
int buttonPin0 = 0;

int E1 = 6; 
int M1 = 7;                       
 
void setup()
{

pinMode(buttonPin0,INPUT);

pinMode(M1, OUTPUT);

}

void loop()

{

buttonPin0 = digitalRead(2);

rainvalue = analogRead(rainsensor);

if (rainvalue < rain_sensitivity && buttonPin0 == HIGH) {
     digitalWrite(M1,HIGH );       
    analogWrite(E1, 225);   
    delay(100); }
  else if (rainvalue < rain_sensitivity && buttonPin0 == LOW) {
    analogWrite(E1, 0);   
    delay(100);                                     
   }

// I want end the process above, to avoid interact next statement at below.


if (rainvalue > rain_sensitivity && buttonPin0 ==HIGH) {
    digitalWrite(M1,LOW );       
    analogWrite(E1, 225);   
    delay(100); }
  else if (rainvalue > rain_sensitivity && buttonPin0 == LOW) {
    analogWrite(E1, 0);   
    delay(100);

}     
}


Re: Motor rotation confusing.

PostPosted: Mon Mar 09, 2015 9:18 am
by Idris
Hi alung92,
- your rainsensor pin is not analog. You need to set it as analog pin (e.g. A0).
- set E1 pin as output too.
- you can try using while statement to run one by one condition.

Re: Motor rotation confusing.

PostPosted: Mon Mar 09, 2015 9:27 am
by ober
alung92 WROTE:my coding was regarding about a rain sensor to active the rotation of motor with 2 limit switch connecting to same digital inout , but i facing the problem which 2 condition can interact both condition (if.....else). the motor start confuse , keep reverse and forward. plz someone help me solve this problem for me . i need end the first condition statement before proceed to next statement. Even thou i used while(1), it's didn't working.


Agreed with Idris, try part by part, narrow down the possible error. When you combine everything, it is difficult to troubleshoot.
Make sure 1 part is working correct, add a little small part and make sure the additional part is working on top of the previous part. When the output is not working as expected, troubleshoot the additional part.