Go Down

Topic: Strange behavior with if() statements on Uno using Adafruit motor shield (Read 1 time) previous topic - next topic

vulrath

Okay, I have been tearing my hair out trying to get this project to work for a little over a week.  I am working on an Uno, using the 0022 IDE and drivers in Vista and 7.  I thought it best to put it here because of what it's doing.

I have been trying (and failing) to get my board to play nice with an Adafruit motor shield.  This is supposed to be the driving force (no pun intended) behind a robot that I am trying to build.  I can get one if/else statement to behave beautifully, but if I add anything more than that, and it ignores the conditions of every one but the first, resulting in a ton of garbage coming out (I have been trying to debug it one loop at a time, .  The directional input was originally supposed to be given by means of five momentary switches for simplicity's sake.  I have tried separate if/else statements, if/else if, switch/case arguments, switching pins, and quite frankly I'm at a loss as to what could be going wrong.  The motor that I'm using to test the code is a vibration motor salvaged from an off-branded Nintendo Gamecube controller.

I'll upload the code in a little bit, once I get on to my other computer.

vulrath

Here is the latest attempt at the code.

/*
* most successful attempt yet, out of 5 tries
*/
#include <AFMotor.h>

AF_DCMotor motor1(1);
//AF_DCMotor motor2(2);

void setup()
{
  pinMode(2,INPUT);
  pinMode(5,INPUT);
  pinMode(6,INPUT);
  pinMode(9,INPUT);
  pinMode(10,INPUT);
  Serial.begin(9600);
  motor1.setSpeed(100);
  Serial.println("Motor 1 initialized");
  //motor2.setSpeed(200);
  //Serial.println("Motor 2 initialized");
 
}
void loop()
{
  int forward=digitalRead(2),back=digitalRead(5),left=digitalRead(6),right=digitalRead(9),power=digitalRead(10);
  if(power!=LOW)
  {
    motor1.run(RELEASE);
    //motor2.run(RELEASE);
  }
 
   else if(power==LOW)
  {
    /*if(forward==LOW)
    {
      //Serial.println("Moving forward...");
      motor1.run(FORWARD);
   // motor2.run(FORWARD);
    }
    /*else
    {
      motor1.run(RELEASE);
      //motor2.run(RELEASE);
      Serial.println("Stopping...");
    }*/
    /*else*/ if(back==LOW)
    {
      motor1.run(BACKWARD);
    }
    else
    {
      motor1.run(BRAKE);
    }
  }
  else
  {
  motor1.run(RELEASE);
  }
}

Nick Gammon

#2
Feb 06, 2011, 07:10 am Last Edit: Feb 06, 2011, 07:12 am by Nick Gammon Reason: 1
For a start, omitting the commented-out code it looks like this:

Code: [Select]


 if(power!=LOW)
 {
   motor1.run(RELEASE);
 }

 else if(power==LOW)
 {
   // testing back
 }
 else
 {
   motor1.run(RELEASE);
 }


You have 3 cases here which don't make sense at all:


  • If power is not low

  • If power is low

  • Some third case (power is neither low, nor not low) - this will never happen



I would start by reworking that along these lines:

Code: [Select]


 if (power != LOW)
 {
   motor1.run(RELEASE);
 }

// here if power is low
 else
 {
   if (back==LOW)
    {
     motor1.run(BACKWARD);
    }
   else
    {
     motor1.run(BRAKE);
    }
 }  // end power low


http://www.gammon.com.au/electronics

vulrath

Gave that a try; no dice.  Tried changing pins and switches, as well.  It works fine if I'm checking the values for going forward, but for some reason it is being unnecessarily obtuse when I try to go backward.

I looked at the circuit, and I noticed that if I touched a voltmeter to pin 5, the motor started behaving.  Damnedest thing.  I still haven't begun to figure out how to use that to my advantage.

Edit:  Screw it - I think one of my last experiments may have blown some of the pins.  I already had another Uno coming for a different project, so I'm going to try it with some virgin hardware.

vulrath

Okay, it's something with the board, but not with the ATMega chip.  Fired up the new board, and it presented the same symptoms, so I poked around with the code.

My code calls for digital pins 2,5,6,9,and 10 to be initialized as inputs.  I tried changing the pin that turned the motor on, and discovered that pins 2 and 10 are the only ones that actually behave.  I have no idea how to proceed further.

Nick Gammon

You are using motor 1,  right?

Code: [Select]
AF_DCMotor motor1(1);


As I read the code, motor 1 uses pins 2 and 3:

Code: [Select]
#define MOTOR1_A 2
#define MOTOR1_B 3


But you make pin 2 an input:

Code: [Select]
pinMode(2,INPUT);

I'm no expert on motor drivers, but won't that be a conflict?
http://www.gammon.com.au/electronics

vulrath

Actually, that is the input that I actually can make work, so I'm not entirely sure it's a conflict.  I'm checking now against the list on the Adafruit forums, but I believe that pin is not used.

Okay, so pin 2 isn't used, but pins 5 and 6, which are both supposed to be used for input, are supposed to be used for motors 3 and 4, which aren't used.  I'm wondering if that could cause some kind of funky interference?

vulrath

Okay, I have found one way to fix my problem (going to try turning on the internal pull-up resistors later on in hopes that will solve the original problem) - I just connected the input from pin 5 to pin A0, and added the following line:
Code: [Select]

back=analogRead(A0);


Suddenly, it works now.  It needs some debouncing, if my diagnosing skills are correct, but it works.

Thanks to everyone that tried to help.

Go Up