Jump to content


Photo

Arduino uploaded code not working


  • Please log in to reply
26 replies to this topic

#1 kushshah0423

kushshah0423

    Member

  • Members
  • PipPip
  • 15 posts

Posted 10 January 2015 - 07:26 PM

I was doing your arduino simple robot project and I finished the robot but I have trouble uploading the code. I copied and pasted your code and pressed upload. The pin 13 led lit up as well as the tx and rx. But neither the servo nor the sensor work as proposed. This is my first arduino robot so can anyone please help?
  • 0

#2 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 10 January 2015 - 07:32 PM

Hi,

 

Whenever you need to debug a programming problem, the best thing to do is to break it down into smaller problems to make the debugging easier. 

 

So to start, make sure that your code is actually getting uploaded to the Arduino. You can verify this by looking in the output window at the bottom of the IDE and if it was uploaded there should be a message saying the upload was successful (I think -- at the very least there won't be an error).

 

Next, try turning the on-board LED (pin 13) on and off using this example code from the IDE:

int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

Does the LED blink? If so, move on. If not, there's a problem uploading code.

 

Next.. Let's break the problem down and tackle the servos and ultrasonic sensor separately. The servos are easier, so let's start with them. Try uploading the following code to your Arduino and see if the servos turn:

#include <Servo.h>

// create servo objects
Servo leftMotor;
Servo rightMotor;

void setup()
{
    leftMotor.attach(13);
    rightMotor.attach(12);
}


void loop()
{
    leftMotor.write(180);
    rightMotor.write(180);
}

If the servos don't turn, then there's probably a wiring problem (post pictures and I'll take a look).

 

After you get the servos working (or run into trouble getting them working), post back and we can debug the ultrasonic sensor code & wiring.

 

Good luck!


  • 0

#3 kushshah0423

kushshah0423

    Member

  • Members
  • PipPip
  • 15 posts

Posted 11 January 2015 - 05:35 PM

Thanks for replying! Sorry I couldn't get to you yesterday due to a family gathering.
  • 0

#4 kushshah0423

kushshah0423

    Member

  • Members
  • PipPip
  • 15 posts

Posted 11 January 2015 - 05:38 PM

Thanks for replying! Sorry I couldn't reply yesterday due to a family gathering. I uploaded the pictures and I will try the solutions you have provided and get back to you asap!
  • 0

#5 kushshah0423

kushshah0423

    Member

  • Members
  • PipPip
  • 15 posts

Posted 14 January 2015 - 01:45 AM

I managed to get the servos working but the right motor won't turn forward. Whenever I input 0 for left, it just doesn't stop moving backward.


  • 0

#6 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 14 January 2015 - 03:21 AM

Does the left motor work correctly? If so, try swapping the servo cables (hook up the right motor to where the left motor is plugged in) and see if the right motor starts working. Most likely it is either a wiring or programming issue.


  • 0

#7 kushshah0423

kushshah0423

    Member

  • Members
  • PipPip
  • 15 posts

Posted 17 January 2015 - 05:45 AM

I switched the cables like you said and uploaded the code. I changed the left one to 0 and uploaded the code. After about 10 minutes, they both started moving backwards and after about 2 minutes they stopped. So I uploaded the code again but this time changed the right servo to 0 and I waited about 20 minutes but no progress was made. They both were turning in the opposite direction.


  • 0

#8 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 17 January 2015 - 02:36 PM

... After about 10 minutes, they both started moving backwards and after about 2 minutes they stopped...

 

So the servos don't start turning immediately after you upload your code? Changes you make should take affect immediately after uploading.

 

Is the problem that the servos aren't turning, or that they aren't turning in the same direction? Because the servos are facing opposite directions, you have to give them opposite values in order for the wheels to turn in the same direction (so to drive forward, you would set one servo to 0 and the other to 180).

 

If you could post pictures of your wiring and robot on here that would be helpful with debugging.


  • 0

#9 kushshah0423

kushshah0423

    Member

  • Members
  • PipPip
  • 15 posts

Posted 18 January 2015 - 03:30 AM

I uploaded the pictures. When I upload the code the servos(moving opposite directions) stop for one second, and then resume moving in opposite directions. When connected to the computer, I picked up the robot and put it down and after about 10 minutes the code actually started working. I think it's a wiring problem because I can't seem to connect the red and black wires to the battery pack firmly.

Attached Files


  • 0

#10 kushshah0423

kushshah0423

    Member

  • Members
  • PipPip
  • 15 posts

Posted 18 January 2015 - 03:32 AM

Btw, when I connect the robot to the computer, the servos immediately start moving(in opposite directions). I entered the 0 and both started moving forward. What I noticed was the left wheel(which the 0 value was inputed for) turned backwards when I lifted the robot, but when I hovered it above the ground it started moving forwards. I think it is surely a wiring issue. I just can't detect the problem. The only wires that actually affect the wheel behavior are the red and black wires connected to the battery pack. After a while, the wheels started going their separate ways again.


Edited by kushshah0423, 18 January 2015 - 03:42 AM.

  • 0

#11 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 18 January 2015 - 03:53 PM

Instead of sticking the wires on the side of the battery pack, try taking out the battery, putting the wire in the middle of the spring, and then putting the battery back in. That should hold the wire in there pretty firmly and may fix your problem if that's the issue. From the tutorial page:
 

The easiest way I've found to do this is to stick a red wire into the second spring from the left and a black wire into the last spring on the right, which will provide the output voltage we need:
 
DSC_3009-medium.JPG

Note: Pay close attention to the orientation of the battery pack in the above picture. Make sure when you put the red wire into the "second spring from the left" that the battery pack cable is on the bottom left. If you have a multimeter, this would be a great time to use it to verify you're plugging in the wires to the right places.

 
If you're using the program from the tutorial page which uses the ultrasonic sensor to avoid obstacles, then depending on what's in front of the robot the wheels may turn in different directions, then both go the same way, then repeat as long as there's something in front of it. Try using the program I posted above, which I'll put again here:
 

#include <Servo.h>

// create servo objects
Servo leftMotor;
Servo rightMotor;

void setup()
{
    leftMotor.attach(13);
    rightMotor.attach(12);
}


void loop()
{
    leftMotor.write(180);
    rightMotor.write(180);
}

  • 0

#12 kushshah0423

kushshah0423

    Member

  • Members
  • PipPip
  • 15 posts

Posted 19 January 2015 - 05:02 AM

So I tried it again today, and here's what I found: It is indeed a wiring problem. They both were turning in opposite directions before I uploaded the code(inserted 0 for right servo). I messed with the wiring a little bit to find the root problem. I put the red wire on the positive side of the battery before I uploaded the code. After I uploaded the code, I placed the red wire from the positive side, to negative side. The right servo that was moving backwards started moving forwards. But the left servo that was moving forwards, stopped moving. I placed the red wire to positive again to see if it will work. Both servos started moving but in opposite directions. So based on what I know, I can only move the right servo forward if the left one stops.


  • 0

#13 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 19 January 2015 - 03:16 PM

They both were turning in opposite directions before I uploaded the code(inserted 0 for right servo).

 
Did you set the left servo to 0 or 180? In order to make the wheels turn the same direction at full speed, you need to set one motor to 0 and the other to 180.
 

I messed with the wiring a little bit to find the root problem. I put the red wire on the positive side of the battery before I uploaded the code. After I uploaded the code, I placed the red wire from the positive side, to negative side. The right servo that was moving backwards started moving forwards. But the left servo that was moving forwards, stopped moving. I placed the red wire to positive again to see if it will work. Both servos started moving but in opposite directions. So based on what I know, I can only move the right servo forward if the left one stops.


Take a look at the image I posted above. You need to hook up your wires like that. Reversing the polarity (as you described) could potentially damage the servos, so you definitely don't want to do that.
 

Try connecting the wires exactly as shown above and use this example code (it's the same as above EXCEPT that I changed the value the left motor is set to):

#include <Servo.h>

// create servo objects
Servo leftMotor;
Servo rightMotor;

void setup()
{
    leftMotor.attach(13);
    rightMotor.attach(12);
}


void loop()
{
    leftMotor.write(0);
    rightMotor.write(180);
}

If the motors don't turn in the same direction, please post pictures of the wiring and confirm you used the exact code posted above.

 

Good luck!


  • 0

#14 kushshah0423

kushshah0423

    Member

  • Members
  • PipPip
  • 15 posts

Posted 23 January 2015 - 02:03 AM

I plugged in the wires exactly as posted. I uploaded the code you sent above and the motors did not move. The weird thing is, the arduino shows that it received the code every single time, but it the code never runs.

Attached Files


  • 0

#15 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 23 January 2015 - 11:53 AM

This is really strange..

 

Did you try the LED blink program I posted above? Did it work?

 

You're sure the batteries are fully charged, right? Some of the symptoms you've described could be caused by almost-dead batteries.


  • 0

#16 kushshah0423

kushshah0423

    Member

  • Members
  • PipPip
  • 15 posts

Posted 23 January 2015 - 06:21 PM

Yes I uploaded the LED code and it worked perfectly. I too had a doubt about the batteries so I bought new batteries two days ago and installed them in but still the problem persists.
  • 0

#17 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 23 January 2015 - 07:42 PM

Ok, so if the LED code worked, then we can rule out a bad Arduino (I've never had one go bad, but you never know..).

 

Try powering the servos from the 5V and GND pins on the Arduino (remove the wires you put into the battery pack). You won't want to drive your robot around like that, but as a test it's fine. Upload your servo test code and see what happens.

 

If you have a multimeter, check the voltage at the servo connectors to ensure they are actually getting power. It's possible one of your jumper wires is bad, or that one of the tie points on the breadboard is bad.


  • 0

#18 kushshah0423

kushshah0423

    Member

  • Members
  • PipPip
  • 15 posts

Posted 01 February 2015 - 07:37 AM

Sorry for the late reply, I've been busy the last couple of weeks to be able to work on the robot. I tried powering the servos from the 5v and GND pins like you said, but when I uploaded the code, nothing happened. I do not have a multimeter unfortunately, but, just to check if the jumper wires were at fault, I used different ones and no difference detected. However, I think I may have detected the issue. When I checked every wire to see if it was correct and firmly in place, I noticed the servo cables. The 2 cables are on the breadboard but when I moved the servo cables(not the pin but the wire itself), the servos changed direction and one of them stopped working at times whenever I move the cable in one direction. I'm not sure if this is the root of the problem, but that's as far as I got.


  • 0

#19 Nathan House

Nathan House

    Administrator

  • Administrators
  • 122 posts
  • LocationLynchburg, VA

Posted 04 February 2015 - 04:38 AM

Hi,

 

I replied to you earlier, but had a bit of a server issue and had to restore a backup which meant it was lost.

 

Make sure the male header connecting the server wire is securely in the breadboard. You should be using the long header included in the kit, and make sure to push the black plastic piece on it to about the middle to ensure both the breadboard and servo cable both have enough length to plug into.


  • 0

#20 kushshah0423

kushshah0423

    Member

  • Members
  • PipPip
  • 15 posts

Posted 07 February 2015 - 03:09 AM

Is there any way I can send you a video of what's going on like email or something?
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users