Go Down

Topic: Control a LEGO NXT motor with arduino UNO (Read 1 time) previous topic - next topic

xari

Apr 03, 2013, 03:22 pm Last Edit: Apr 04, 2013, 05:19 pm by xari Reason: 1
I'm on a project in which i try to control a LEGO NXT motor with arduino UNO,i found many useful things in this forum-arduino site-playground e.t.c but lately i got a 'code' problem.In specific,i use the PID v1 library and use 2 interrupts for the sensors which work great BUT by the time i added a button with which i want to stop the motor at a position so as to calibrate it.

1st the motor doesn't completely stop and if it stops goes straight to the PID but it moves really slow.
2nd tried to use the do...while calibration example but things were worse.
For the Setpoint of the desired position of the motor i use a potentiometer connected on A0,mapped 0-180.I also used bigger delays again nothing.I only post the VOID loop cause i think you will understand the rest,if not i ll post the whole code.

i apologize if my post is bad but it's my 1st one,thanks in advance even if no one replies this forum has helped me a lot to do what i ve already done.Here's the code:[/pre]
Code: [Select]
 void loop()
             {
                                                           
                   int reading = digitalRead(12);                 // read the state of the switch into a local variable:
               
                   if (reading != lastbuttonState1)               // check to see if you just pressed the button (i.e. the input went from LOW to HIGH),  and you've waited
                                                                 // long enough since the last press to ignore any noise If the switch changed, due to noise or pressing:
                   {
                      lastDebounceTime = millis();              // reset the debouncing timer
                   }
                   
                   if ((millis() - lastDebounceTime) > debounceDelay)     // whatever the reading is at, it's been there for longer than the debounce delay, so take it as the actual current state:
                   {                      
                     buttonstate1 = reading;
                   }  
                 if (buttonstate1==0)              
                  {
                                        digitalWrite(motorPin0, 1);
                                        digitalWrite(motorPin1, 0);
                                        analogWrite(5,90);
                                         delay(1000);
              }
               else
                      {                digitalWrite(motorPin0, 1);
                                       digitalWrite(motorPin1, 1);
                                     
     
                                       delay(1000);

                     }
        lastbuttonState1 = reading;   // save the reading.  Next time through the loop,
                                                   // it'll be the lastButtonState:
            Setpoint = map(analogRead(A0), 0, 1023, 0, 180);
            Input=angle;
               
                myPID.Compute();
               err=Setpoint-Input;
              Pwm1_val=Output;
             
              if (Pwm1_val>=0)
                {
                digitalWrite(motorPin0,1);
                digitalWrite(motorPin1,0);
                }
              if (Pwm1_val<0)
                {
                digitalWrite(motorPin0,0);
                digitalWrite(motorPin1,1);
                Pwm1_val=abs(Pwm1_val);
                }
                      QPWM=Pwm1_val;    
                      analogWrite(5,QPWM);

MarkT

Please read the guidelines and edit your post to use
Code: [Select]
// code tags for code
[ I won't respond to messages, use the forum please ]

Go Up