noob to arduino, need help with simple code

Hi, I can’t seem to get my code/circuit working, can someone please help me with this?
I have a Mega hooked up to a SN754410 motor driver and servo for steering the front two wheels. The motor driver only has 1 motor attached (to the back two wheels). I can’t find anything wrong with the code but then again I am completely new to programming. When I input a ‘f’ or a ‘b’ (see code) nothing happens and when I input ‘r’ or ‘l’ the servo just spins constantly. I have checked and rechecked the circuit and the code multiple times, as well as rebuilding the circuit, all to no avail.
Would someone please point me in the right direction?

//circuit:
//servo power–>6v battery
//servo ground–>arduino gnd
//servo signal–>pwm pin 4
//SN754410 1A–>pwm pin 3
//SN754410 2A–>pwm pin 2
//SN754410 VCC1&1,2EN–>arduino 5v
//SN754410 ground–>arduino gnd
//SN754410 1Y–>motor (+)
//SN754410 2Y–>motor (-)
//SN754410 VCC2–>6v battery

#include <Servo.h>

#define motorAp 3 //to SN754410 1A
#define motorAd 2 //to SN754410 2A
#define SteeringServo 4 //to servo signal pin
#define SerialBaud 9600
#define Buffer 16

Servo steering;
char inputBuffer[Buffer];
void setup()
{
steering.attach(SteeringServo);
pinMode(motorAp, OUTPUT);
pinMode(motorAd, OUTPUT);
Serial.begin(SerialBaud);
}
void loop()
{
if (Serial.available()>0)
{
int (inByte)=Serial.read();
switch (inByte)
{
case ‘f’: //motor Forwards
analogWrite(motorAp, HIGH);
digitalWrite(motorAd, HIGH);
break;
case ‘b’: //motor Backwards
analogWrite(motorAp, HIGH);
digitalWrite(motorAd, LOW);
break;
case ‘r’: //steering servo move 10 degrees
steering.write(10);
case ‘l’: //steering servo move 10 degrees the opp direc.
steering.write(-10);
break;
case ‘s’: //stop all motor activity
analogWrite(motorAp, LOW);
digitalWrite(motorAd, LOW);
break;
default:
break;
}
}
}

Thanks in advance for any guidance/help :slight_smile:

  analogWrite(motorAp, HIGH);
          digitalWrite(motorAd, HIGH);

HIGH has a value of 1, so "analogWrite(motorAp, HIGH);" gives a PWM duty-cycle of just 1%. You may see it on a scope, but I doubt it would move a motor.

case 'l': //steering servo move 10 degrees the opp direc. steering.write(-10);

Servo writes are absolute - I'm not sure what a negative angle would give you.

The Servo.write function will attempt to move the servo to the specified position. Generally, servos are designed to move between 0 and 180 degrees. So, -10 is outside the range of a normal servo.

You haven't given us any information about the servo. If it spins constantly, when you write a value to it, it is not a servo, by definition. It may have been modified to be a continuous rotation servo, which really isn't a servo.

    int (inByte)=Serial.read();

Why is inByte in parentheses?

If the drive motor fails to spin when you set the direction and enable pins HIGH or LOW, then clearly the motor driver is not connected correctly. You need to tell us about how it is connected.

Why is inByte in parentheses?

Why is it there at all? ;)

AWOL: I didn't have much hope in the (-10) degrees. What you said about the analogwrite function makes sense, so I changed the value to be written to be like 1000 or so. Still nothing.

PaulS: I removed the parentheses around inByte but it didn't make any difference. I'm not sure about the continuous rotation servo thing, the servo worked fine with the knob and sweep examples in arduino 0018. Here is where I got the servo: http://www.sparkfun.com/commerce/product_info.php?products_id=9347 After looking at this again, you are right, its a full rotation one, but it did work with the two examples so thats confusing. In the beginning of the code I have written up how I hooked up the motor driver.

Thanks

so I changed the value to be written to be like 1000 or so

1000 … out of 255?
http://arduino.cc/en/Reference/AnalogWrite

[edit]Oops, re-read my own post - make the HIGH write just 0.4%

Also, if yours is an ex-servo, the “stop” point should be around the 90 “degree” mark, so to move one way or the other, you need to write, say, 80 or 100.[/edit]

Don't I need the inByte option? Is there another better way to do it? What do you mean by ex-servo? I tried 255, still nothing.

Don't I need the inByte option

You don't need to read it into a variable if you're not using it anywhere else, you could just put the "Serial.read" in the "switch". (I was only slightly joking, hence the wink)

What do you mean by ex-servo?

I mean it used to be a servo, but it isn't anymore. It is a geared DC motor with a convenient single wire direction and possible speed control.

I tried 255, still nothing.

Check your wiring. Maybe add some debug prints to make sure your program is going where you think it is.

this will sound very noobish, but what do you mean by debug prints? also, i went and got a new servo motor, but every time i put power to the arduino the servo turns a couple degrees. ideas?

You are reading data from the serial port. You can write to the serial port, too. Add some Serial.print() and/or Serial.println() statements to the sketch, to echo what was received, and which case you are in.

If you get the message saying that you are in case 'f', but the drive motor does not come on, then, you know you have a wiring problem.

When the Arduino boots up, the pins are set to input mode, initially. The servo class changes the pins to output mode. During the time that the pin is in input mode, there can be a floating condition, where the servo actually gets power, causing it to more. A resistor in parallel with the servo can stop that unwanted motion. Do a search on teh forum for details.

Did the serial print stuff and all is good. Thanks for the servo thing, I went and got a different servo and that worked. I decided to give up with that motor driver and got the qik 2s12v10 serial controller from pololu. I will have to wait until that comes to try stuff out with that.