Motor rotation confusing.

LINIX Brushless, VEXTA Brushless, RC Servo, DC Geared, Linear, Stepper, Tamiya.....

Motor rotation confusing.

Postby alung92 » Sat Mar 07, 2015 12:42 am

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

}     
}

alung92
Novice
 
Posts: 17
Joined: Thu Aug 21, 2014 12:57 pm

Re: Motor rotation confusing.

Postby Idris » Mon Mar 09, 2015 9:18 am

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.
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 409
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Re: Motor rotation confusing.

Postby ober » Mon Mar 09, 2015 9:27 am

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.
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm


Return to DC Motor

Who is online

Users browsing this forum: No registered users and 5 guests

cron