Go Down

Topic: Need help with code for a robot (Read 2621 times) previous topic - next topic

kaiserking

Dec 10, 2013, 04:48 pm Last Edit: Dec 10, 2013, 04:52 pm by kaiserking Reason: 1
Hello there, I am a newbie for adruino programming. I am attempting to do a simple robot that has an arm that moves from 0 to 180 degrees.with an Arduino UNO. The thing is that I want to add an on/off switch for the robot. The button I have is a simple push button (like the one in the reset button). My code doesn't seem to work as the robot only moves a few angles with just one press of the button and stops instead of running the program until I press the button again. Could anyone help me please? Here is my code, thank you.
Code: [Select]
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position
const int buttonPin = 2;     // the number of the pushbutton pin
// variables will change:
int buttonState;         // variable for reading the pushbutton status
boolean check = 0; //while check is 1 the robot arm should move, determines the on/off

void setup()
{
myservo.attach(9);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);
 if (buttonState == HIGH)
 {
 check= !check;
 buttonState = 0;
 }  
 
 if (check == 1)
 {    
       for(pos = 0; pos < 180; pos+= 1)  // goes from 0 degrees to 180 degrees
       {                                  
         myservo.write(pos);              // tell servo to go to position in variable 'pos'
         delay(10);                       // waits 15ms for the servo to reach the position
       }
       for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees
       {                                
        myservo.write(pos);              // tell servo to go to position in variable 'pos'
        delay(10);                       // waits 15ms for the servo to reach the position
       }
     
 }

}

EDIT: If it helps, the button is connected on the 5v Power like in this diagram: http://arduino.cc/en/uploads/Tutorial/button_schem.png
while the servo motor is connected like this : http://ninad243.weebly.com/uploads/1/2/5/5/12557204/1839348_orig.png

groundFungus

How is the servo powered?  How is your switch wired? 

kaiserking


How is the servo powered?  How is your switch wired? 
Well for the wiring of the button I used this: http://arduino.cc/en/uploads/Tutorial/button_schem.png
and added this one for the servo motor: http://ninad243.weebly.com/uploads/1/2/5/5/12557204/1839348_orig.png

For the power, I am currently using the USB connection to power the card and the servo.

groundFungus

Powering the servo from the Arduino (USB) can cause problems.  Try powering the servo from an external (like 4AA batteries).  Servos can draw enough current to cause a dip in the power to the Arduino to reset it (and then it will wait for a new button press). 

Leathel

I think your problem is here

Quote
Code: [Select]
void loop()
{
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH)
  {
  check= !check;
  buttonState = 0;
  }
if (check == 1) 



you declared button state to be an integer but in the if statement you are checking to see if its high or low when it can only be a number. Also you have
Code: [Select]
check= !check; which to me doesnt make sense, you should have it as
Code: [Select]
check = false; or 
Code: [Select]
check = !true;. You should stick to just true and false for boolean variables, because right now you have buttonState as high and 0. Your for loops are fine. Work on your code grammar and it should work fine

Hope this helps

PaulS

Quote
you declared button state to be an integer but in the if statement you are checking to see if its high or low when it can only be a number.

Psst. HIGH and LOW are numbers. 1 and 0 to be exact.

Quote
Also you have ... which to me doesnt make sense,

That's just you, then, It makes perfect sense to the compiler, and is perfectly reasonable.
The art of getting good answers lies in asking good questions.

Go Up