Not so new but new to code and XBEE with Arduino

I am using IDE 1.0.1

A mega 1280

A seedstudio motor shield

A version 1 Xbee retail kit from sparkfun with shield and explorer.

It does compile too.

I have this code that i want to drive two dc motors with external power 7.2v 1800mAh RC battery pack.

I have used this code (minus) the // without the else if statements and it worked as an obstacle avoidance robot. Went forward ‘observed an object in path’ backed up right the forward again until the obtacle was gone.

Now, i want to redo the code somewhat to work with the xbee using the keys on the keyboard. e.g. 1, 2, 3 or maybe later w,s, a, d.

My question is will this code wear out my dc motors quickly because of the else if statements or what would happen if i pressed both the 1, 2 keys “brownout”???

Here is the code: Just not sure what to do other than if else then ect… Thanks for you help, please let me learn a bit too…

Again, i have // a lot of the code to remove the sonic range finder.

//#include <Ping.h>

//const int numOfReadings = 10;                   // number of readings to take/ items in the array

 //int readings[numOfReadings];                    // stores the distance readings in an array

//int arrayIndex = 0;                             // arrayIndex of the current item in the array

//int total = 0;                                  // stores the cumlative total

//int averageDistance = 0;                        // stores the average value

// setup pins and variables for SRF05 sonar device

//const int pingpin = 2;                         // ping pin (digital 2 pin)

//unsigned long pulseTime = 0;                    // stores the pulse in Micro Seconds

int motor1Pin1 = 8;                             // one of leads from DC motor

int motor1Pin2 = 11;                             // pin 7 on L293D

int enable1Pin1 = 9;                             // pin 1 on L293D

int motor2Pin1 = 12;                             // pin 10 on L293D

int motor2Pin2 = 13;                             // pin  15 on L293D

int enable2Pin = 10;                            // pin 9 on L293D

int spead =100;//define the spead of motor origionally set at 127.

void setup() {

  // set the motor pins as outputs:

   Serial.begin(9600);

  pinMode(motor1Pin1, OUTPUT);

  pinMode(motor1Pin2, OUTPUT);

  pinMode(enable1Pin1, OUTPUT);


  pinMode(motor2Pin1, OUTPUT);

  pinMode(motor2Pin2, OUTPUT);

  pinMode(enable2Pin, OUTPUT);

  // set enablePins high so that motor can turn on:

  digitalWrite(enable1Pin1, HIGH);

  digitalWrite(enable2Pin, HIGH);

 // pinMode(pingpin, OUTPUT);                     

//  pinMode(pingpin, INPUT);                      

  // create array loop to iterate over every item in the array

 // for (int thisReading = 0; thisReading < numOfReadings; thisReading++) {

 //   readings[thisReading] = 0;

  }



void loop() {

  if(Serial.available()>0){
 // Serial.println(Serial.read());
  
  
  Serial.println();
  
  
  //Serial.print("in, ");
  
  
  //pinMode(pingpin, OUTPUT);

 // digitalWrite(pingpin, HIGH);                  // send 10 microsecond pulse

  //delayMicroseconds(10);                        // wait 10 microseconds before turning off

  //digitalWrite(pingpin, LOW);                   // stop sending the pulse

  //pinMode(pingpin, INPUT);

 // pulseTime = pulseIn(pingpin, HIGH);           // Look for a return pulse, it should be high as the pulse goes low-high-low

 // distance = pulseTime/58;                      // Distance = pulse time / 58 to convert to cm.

  //total= total - readings[arrayIndex];          // subtract the last distance

  //readings[arrayIndex] = distance;              // add distance reading to array

 // total= total + readings[arrayIndex];          // add the reading to the total

  //arrayIndex = arrayIndex + 1;                  // go to the next item in the array

  // At the end of the array (10 items) then start again



  //if (arrayIndex >= numOfReadings)  {

  //  arrayIndex = 0;

  }

 // averageDistance = total / numOfReadings;      // calculate the average distance

  //delay(10);

  // check the average distance and move accordingly

  //if (averageDistance <= 23){

    // go backwards
    byte info=Serial.read();
Serial.println(info);
  if(info=='1'){
    analogWrite(enable1Pin1,spead);  //input a simulation value to set the speed
    analogWrite(enable2Pin,spead);
    digitalWrite(motor1Pin1, HIGH);

    digitalWrite(motor1Pin2, LOW);

    digitalWrite(motor2Pin1, HIGH);

    digitalWrite(motor2Pin2, LOW);

  }

  //if (averageDistance <= 33 && averageDistance > 23) {

    // turn
    else if (info=='2'){   //writes 180 degrees to "a direction not determined" tfw
    
   
   
    analogWrite(enable1Pin1,spead);//input a simulation value to set the speed
    analogWrite(enable2Pin,spead);
    
    digitalWrite(motor1Pin1, HIGH);

    digitalWrite(motor1Pin2, LOW);

    digitalWrite(motor2Pin1, LOW);

    digitalWrite(motor2Pin2, HIGH);

  }
else if (info = '3'){
  //if (averageDistance > 33)   {

    // go forward
    analogWrite(enable1Pin1,spead);//input a simulation value to set the speed
    analogWrite(enable2Pin,spead);
    digitalWrite(motor1Pin1, LOW);

    digitalWrite(motor1Pin2, HIGH);

    digitalWrite(motor2Pin1, LOW);

    digitalWrite(motor2Pin2, HIGH);
  }

}

[quote]

You shoupdnt have to worry too much about motors wearing out when the power is pulsed to them, I wouldnt think. The same thing happens when PWM is sent to a motor, although at a much higher frequency. The motor control section looks a lot like the sample code for the Wild Thumper (link on product page), with the exception that instead of High written to the motor pin, PWM is written (leftPWM or rightPWM corresponding to each side).

If PWM is not available to you (fairly new to this myself), and you want some mixing allowed, personally I would add more buttons (like Q and E or 7 and 9), and set a variable that toggles each time through the loop when a ‘diagonal’ is pressed, alternating between the 180 turn and fwd or rev.

Thanks TrollHammer, I have tried to run the code through the freeduino with xbee shield and seeed motor shield. just for debug purposes i have used a led and the sparkfun tutorial code as this[code if(Serial.available()>0){

// Serial.println(Serial.read());

char getData = Serial.read(); //if yes, read it

if(getData == ‘a’){

digitalWrite(2, HIGH);

}else if(getData == ‘b’){

digitalWrite(2, LOW);

}][/code]

I can get the led to go high and low using xctu but not the motors :o (only one attached for now)(also using 9v walwart through freeduino) no extra external power in fear to fry the xbee shield.>>??? The motor does go on but very slowly, and does not change any speed or direction with the keystrokes. Any idea of what i am missing here. Yes, it would be cool to do diagonal but totally not there yet. any help would be appreciated.

also, when i use the serial monitor on arduino IDE, i get 255, is this the high for the pin 2 that i am sending the led on??

I have to admit I know little about the serial functions, other than how to print strings and variables for debugging.

The only correlation I can figure out is that:

digitalWrite(2, HIGH)

Would be pretty much the same as :

analogWrite(2, 255)

Each would set pin 2 to on full. This is the only thing that I can think of that would generate a 255.

The way analogWrite works is:

First number is the pin number

second number is a digital number to set pulse width modulation on a digital pin. 0 is off, 127 would be 50% duty cycle (on half the time off half the time), and 255 is on full time. Frquency is something like 400hz.

As far as I understand, though, you would have to use a transistor to run the motor, a digital pin would not source or sink enoigh power to run anything more than maybe a pager motor.

To have variable speed and direction, you would use two pins, such as 2 and 4:

(forward)

analogWrite(2, 255)

analogWrite(4, 0)

Reverse:

analogWrite(2, 0)

AnalogWrite(4, 255)

Stop (brake):

Both analogWrite(#,127)

Off:

Both analogWritr(#, 0)

Where # would be the pin nimber.

(Sorry, on a phone).

Each side of the motor is to be connected to a pin through a driver (Cmos, mosfet, darlington, whichever you want), so that when one pin pulses high, current flows one way and the motor runs one way, and when the other pin pulses instead, the current runs the other way and the motor reverses. Pulsing them both effectively shorts the moor and causes braking action, at least if you use an H bridge driver.

Then, to make diagonals work, you could set up a series of values for the PWM function. Sorta like:

If W, right forward PWM=255, left forward PWM=255

If Q, Right Forward PWM=255, left=0 or 127 (depending on if you want A to spin it in a circle or spin around one wheel)

If A, Right Forward PWM=255, left =0 or reverse 255 (depending on the result you want)

And work around the keypad, assignong values for each condition. This is similar to how the Wild Thumper board works. You could also assign S as brake (all=255) or toggle brake and off, or set a sleep function so that if no button is pressed it goes to brake for a nunber of cycles then changes to Off… Thats up to you.

Ill be more discriptive when I can access a computer with internet.

As far as serial grabbing and why you arent getting desired output, I couldnt say without it in front of me.