Working code stopped working? Wth?

Hello. I made an obstacle avoiding robot a while back. I turned it on today for the first time in a while and noticed it wasn't working quite as well as it was. So I checked out my old code and decided to lower the collide distance so that it will get closer to objects before it turns. It was seeing walls and furniture that wasn't close. I lowered it by 10, uploaded the code, and now it won't even move. The motors don't even move now. So I thought, "what the hell?" and re-uploaded the original code. Still not moving. How can this be? The code was working fine until I uploaded it again. I disconnected the motors and tested them to make sure I didn't fry them somehow. Checked all the wires. They're fine. Checked the driver even though the led and servo still work. The serial monitor shows that the HC-SR04 distance sensor is working correctly, and it even states when it should be turning or moving forward. Just no response from the motors at all. No idea what the problem is. Any help would is appreciated. I just need to know how an identical code being uploaded could cause it to stop working.

I don't really think it's necessary to upload the code since the code wasn't the problem. If you think you need it I'll upload it. Thanks.

I just need to know how an identical code being uploaded could cause it to stop working.

Come on, you can't really be asking that. I'm not being sarcastic, but at 48 posts you should know the answer to that also.

Post your code, re-check your wires.

I should know huh? Yea cause we’re all born with knowledge of arduino. Can’t you just answer the question without trying to make feel stupid? Lol every time I post here. No help just power tripping ego maniacs. Obviously you know what the problem is, but instead of just helping a noob (the intended use of this forum, I thought) you just dangle it in front of me?

What’s with the hatred of noobs on this forum? Yes 48 posts of responding to comments like this. 48 posts of responding to unhelpful people that can never seem to get to the point. I’m not asking you to write the code for me and even if I was why would that be so bad? What if someone bought arduino to build one specific thing and then never intended to use it again? You gonna make them learn on their own with no help at all? No advice even? For what reason? Why is it so bad to just share your expertise? Really feels like you guys who are more experienced are somehow threatened by newcomers and try to snuff them out. Don’t you want more skilled people to contribute to the community? Whatever…keep jerking me around I guess, I’m sure I’ll figure it out eventually.

I am not an arduino master. I made this bot for my little bro. Enough with the shame game.
Here’s the code. Now…help, or don’t. But nothing in between please. I apologize for this heated response. I stopped posting here months ago because of this and I didn’t really intend to come back. So this just kinda pissed me off. You really weren’t as disrespectful as you could have been. So thanks for that I guess. But jeeeez. This forum has a huge ego problem. You’re not special snowflakes just because you know C+.

#include <AFMotor.h>
#include <Servo.h>
#include <NewPing.h>
int trigpin = 8;
int echopin = 9;
#define maxdist 200
#define maxspeed 180
#define colldist 40
NewPing sonar(trigpin, echopin, maxdist);
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
Servo servo;
const int led = 15;
int leftDistance, rightDistance;
int curDist;
int speedSet = 0;

void setup() {
Serial.begin(9600);
pinMode(led,OUTPUT);
servo.attach(9);
servo.write(90);
delay(500);

}

void loop() {
Serial.println(readPing());
digitalWrite(led,HIGH);
Serial.println(“START FORWARD”);
accelerateForward();
if (readPing()<colldist){
Serial.println(“Collision Detected”);
moveStop();
changePath();
compareDistance();
accelerateForward();
}

}

void changePath() {
servo.write(20);
delay(600);
rightDistance = readPing();
delay(600);
servo.write(90);
delay(600);
servo.write(160);
delay(600);
leftDistance = readPing();
delay(600);
servo.write(90);
delay(600);
}

void compareDistance()
{
if (leftDistance>rightDistance)
{
turnLeft();
}
else if (rightDistance>leftDistance)
{
turnRight();
}
else
{
turnAround();
}
}

int readPing() {
delay(70);
unsigned int uS = sonar.ping();
int cm = uS/US_ROUNDTRIP_CM;
Serial.print(cm);
Serial.println(" cm");
return cm;
}

void moveStop() {
Serial.println(“Stopped”);
motor1.run(RELEASE);
motor2.run(RELEASE);
delay(800);
}

void accelerateForward() {
Serial.println(“Start Forward”);
motor1.run(FORWARD);
motor2.run(FORWARD);
for (speedSet = 0; speedSet < maxspeed; speedSet +=2)
{
motor1.setSpeed(speedSet);
motor2.setSpeed(speedSet);
}
}

void turnRight() {
Serial.println(“Turn Right”);
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(500);
}

void turnLeft() {
Serial.println(“Turn Left”);
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(500);
}

void turnAround() {
Serial.println(“Turn Around”);
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(1000);
}

Can't you just answer the question without trying to make feel stupid? Lol every time I post here. No help just power tripping ego maniacs. Obviously you know what the problem is, but instead of just helping a noob (the intended use of this forum, I thought) you just dangle it in front of me?

I know what the problem is, at least generally. It is either the code or the wiring. If you really have uploaded the code that was working previously then that only leaves the wiring. How is it wired ? Breadboard, shield, soldered or what ? How is the vehicle powered ? Could the batteries powering the motors be flat ?

Hi,
What arduino and shields are you using.
If you are using a motor shield, some have jumpers that need to be setup, check that none are missing if you used them.

Tom… :slight_smile:

PS, can you go back to your last post and replace quote with code tags please.
They are made with the </> icon in the reply Menu.

Or maybe you did not upload the latest code, the one that worked. It happened to me too.

I just need to know how an identical code being uploaded could cause it to stop working.

I don't have an answer, and I don't actually believe it. There is no ego involved, you do however seem to have an attitude.

 if (leftDistance>rightDistance) 
  {
    turnLeft();
  }
  else if (rightDistance>leftDistance)
  {
    turnRight();
  }
  else 
  {
    turnAround();
  }

turnAround() will never be called here.

I lowered it by 10, uploaded the code, and now it won't even move. The motors don't even move now.

accelerateForward() seems to be called unconditionally, so you should get something from your motors if the wiring and power to them is fine. How are they powered? are the batteries flat?

It's not being "special" or having an ego or being a master of "C+"; it's simple common-sense and basic fault-finding.

Imagine turning up at the garage saying "My car won't start; tell me what's wrong with it, and by the way I left it at home"

This is the "Programming" section, so a fairly early expectation is that we see some code and an accurate description of the problem. If possible, we'd like to see the code posted according to the clearly-posted forum guidelines.

If the code really has not changed, and there is no problem with the hardware, it is very likely it never really worked right at all. Rather, due to dumb luck, it simply appeared to be working. That would mean you have a logic error, timing problem, or other issue that allows it to seem to work sometimes, providing conditions are exactly right. Unless it was exhaustively tested, you have no factual basis for claiming it "worked" before, just because it seemed to do what you expected for a short period of time.

Regards, Ray L.

I know what the problem is, at least generally. It is either the code or the wiring. If you really have uploaded the code that was working previously then that only leaves the wiring. How is it wired ? Breadboard, shield, soldered or what ? How is the vehicle powered ? Could the batteries powering the motors be flat ?

Don’t think it’s the wiring. It was working fine. I changed one thing on the code and it stopped working. Changed it back and it didn’t fix it. I did check the wiring several times thinking maybe I pulled a wire out or something. Pretty basic wiring. No soldering just jumper wires. And it’s powered with 6 AA batteries. And I checked the voltages and they seemed a bit low so I replaced them. No luck.

Hi,
What arduino and shields are you using.
If you are using a motor shield, some have jumpers that need to be setup, check that none are missing if you used them.

Tom… :slight_smile:

I’m using a mega 2560 and a seeedstudio motor driver.

I don’t have an answer, and I don’t actually believe it. There is no ego involved, you do however seem to have an attitude.

Sure do. At least when it’s called for. I don’t show respect to those who don’t show it to me. Sorry. Just how I am. Act like a decent person and I’ll be the nicest guy in the world. And…what don’t you believe?

turnAround() will never be called here.

Haha yeah I noticed that it never turns around. Thanks. I’ll take that out.

Imagine turning up at the garage saying “My car won’t start; tell me what’s wrong with it, and by the way I left it at home”

This is the “Programming” section, so a fairly early expectation is that we see some code and an accurate description of the problem. If possible, we’d like to see the code posted according to the clearly-posted forum guidelines.

Imagine turning up at the garage saying “My car won’t start; tell me what’s wrong with it, and by the way I left it at home”

This is the “Programming” section, so a fairly early expectation is that we see some code and an accurate description of the problem. If possible, we’d like to see the code posted according to the clearly-posted forum guidelines.

I said in my first post that I would provide the code if it was asked of me. And I did. I see your point about posting in programming when it isn’t about the code but like I said I have no idea what the problem is so I didn’t know where to post. And I don’t know if this stuff falls under the common sense category. And what is your job here? Accelerate the drama? Last time I got into it with someone you took down my post and when I tried to calmly explain myself you ignored me. You’ve never offered any help you just re-kindle the drama…I’m over it. You should be too. Let’s move on.

If the code really has not changed, and there is no problem with the hardware, it is very likely it never really worked right at all. Rather, due to dumb luck, it simply appeared to be working. That would mean you have a logic error, timing problem, or other issue that allows it to seem to work sometimes, providing conditions are exactly right. Unless it was exhaustively tested, you have no factual basis for claiming it “worked” before, just because it seemed to do what you expected for a short period of time.

Perhaps. But I don’t think so. It’s been working the correct way for months. It just now stopped. And even back when I had trouble with it responding to distances incorrectly the motors at least worked. It just ran into walls and stuff. Now the motors don’t run at all. All I changed was colldist from 40 to 30. Then it stopped working so I changed it back to 40 and it still doesnt work. But yes in the past before I “perfected” it, it did seem to work sometimes and not work other times. But I fixed that. So I don’t believe that’s the problem. I wouldn’t have posted here if it was a simple wiring slipup or something easily noticed. It’s probably just something I’m overlooking but I can’t seem to figure it out. I thought maybe the board got fried but everything but the motors work.

No, my job is to prevent drama, by trying to instill in people the need to remember that we can't see what they can see, and that they need to pause and think sometimes.

It's a Sisyphean task, it seems sometimes.

Think the most important thing to remember is its not anybodies job to be here, let alone help. Yet that is what a lot of people here do, help that is.

Try a simple sketch that only spins the motors, one direction, and see if they turn. If they don't, then its time to look at the motor wiring.

Floyd555: Don't think it's the wiring. It was working fine. I changed one thing on the code and it stopped working. Changed it back and it didn't fix it.

You need to think about probabilities.

How often does working code stop working - almost NEVER - it is certainly a much smaller probability than 1 in a million.

How often do electro-mechanical things screw-up - including loose connections and oxidized wiring. Far more likely.

Successful fault finding involves simplifying the problem and focusing on the most likely causes. So write some short test code that will exercise the electro-mechanical system.

...R

It’s a Sisyphean task, it seems sometimes.

Sisyphean. Learned a new word today. Lol thanks for that.

Successful fault finding involves simplifying the problem and focusing on the most likely causes. So write some short test code that will exercise the electro-mechanical system.

Thanks for the idea. I wrote a small code to run just the motors as you suggested.

#include <AFMotor.h>
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);

void setup(){
  
 delay(500);
}

void loop(){
  motor1.run(FORWARD);
  motor2.run(FORWARD);
}

Motors did not work. It seems somehow the wiring must be bad. As I stated before, I unhooked the motors and tested them with a battery. They worked like that but not on my bot. Did I damage the motors somehow possibly? Would they still work with a battery even though they’re bad? It’s just weird that they stopped working because I changed the code. That’s why it seems unlikely to me. The ends of the wires are a bit scraggly looking though. Maybe I’ll solder in new wires and try that.

If you have any other suggestions I’ll try them. Thanks.

It's very unlikely you damage the motors; usually, those things can take a lot of abuse

Have you got a multi meter or a way of checking continuity? If so, check the wires from the shield to the motors.

If those are fine I would check that your motor shield is getting signals from your arduino, then if it is, then I'd check the out pins on the shield to the motors.

Usually a damaged motor emits noxious smoke and will be hot - the windings burn off their insulation if overloaded. Its hard to miss this!

Hi, Can you post a picture of your robot please.

Tom.... :)

Floyd555: I wrote a small code to run just the motors as you suggested. ....SNIP.... If you have any other suggestions I'll try them. Thanks.

If you are confident that that code should cause the motors to run you need to concentrate your fault finding in other areas until you can get them to run with that code.

Check ALL the wiring very carefully - perhaps with a mgnifiying glass. Check continuity with a multimeter.

Is it possible your motor driver has been damaged? That is much more likely than damage to the motor.

Is your power supply adequate - correct volts and amps ?

...R

Still not exactly sure what the problem was but it’s working now. I just took the wires out and unplugged the motor shield from the arduino and then I cut and stripped the ends of the motor wires and put everything back together. Also switched my arduino with another one. Same kind. It still didn’t work and I was messing with wires for a little bit after that just making sure everything was making contact. Then I pushed the reset button on the motor shield for the hell of it. The motors kicked on about 10 seconds after that. I didn’t think the reset button would do anything except restart the arduino. I don’t know if that’s what did the trick or not but it’s working now.
I don’t think it was switching out the arduino because the servo and LED were still working on the bot just not the motors. If it were a bad arduino nothing would work right? And the ends of the wires worked fine before I cut them with a 9v battery, just not in the motor shield. So I have no idea what happened, which kinda sucks cause I can’t learn from it lol.

Anyway thanks for the help everyone.

Have you got a multi meter or a way of checking continuity? If so, check the wires from the shield to the motors.

If those are fine I would check that your motor shield is getting signals from your arduino, then if it is, then I’d check the out pins on the shield to the motors.

I’ll do this. No idea why I didn’t try that already. I used the multimeter on the batteries but not the motor driver wires. -facepalm-

Usually a damaged motor emits noxious smoke and will be hot - the windings burn off their
insulation if overloaded. Its hard to miss this!

Good to know. I’ll keep that in mind.

Hi,
Can you post a picture of your robot please.

Tom… :slight_smile:

Actually I can’t. Sorry. I wish I could cause I’m finally done with it but my computer is crap. It’s a frankenstein computer. My girlfriend’s laptop’s hard drive died and my charging port went out so until I can get the disc for her new hard drive and I can solder in a new port I’m using my samsung hard drive in her toshiba laptop and it’s completely crap. Sound driver isn’t compatible. And when I try to save or upload anything the browser window freezes the laptop. So I bought her a tablet and this is all I have for now. So sorry lol. Maybe I’ll use her tablet to put one up later. You’re not missing much though. I’m proud of it but it’s not all that impressive lol. It’s a little two wheeled box with a box head connected buy a servo. I used project boxes with a little metal shield on one side, a drill, and a dremel to cut the holes for the servo, distance sensor, LED, etc.

I’m currently building a bipedal robot out of the same project boxes and servos but I’m not sure how well it will balance. It’s much cooler looking though, despite it being armless and headless so far. And I’ve only tested its balance so I know the servos can hold the weight but I haven’t started the code yet due to it being incomplete. I’m gonna need to learn more about balance sensors and using multiple servos anyway before I begin. Hopfully it will work. Anyway I’m off topic. But I’ll try to put a pic up soon.

EDIT

I tried to change the code again and now I’m back to the same problem. It’s gotta be the motor shield. It’s reading at 0.