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]