Pages: [1]   Go Down
Author Topic: Serial communication for pwm to sabertooth motor control am i doing this right?  (Read 1016 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I hacked Jeremy Blums Led on off code to control my sabertooth motor controller it is working but i dont know how to alter it to read the rx constantly without hitting enter any ideas?
This was his code to start with:
int ledPin=13;
 void setup()
 {
 Serial.begin(9600);
 pinMode(13, OUTPUT); }
  void loop()
 {
  while (Serial.available()==0);//have the arduino wait to receive input
  int val= Serial.read()-'0';//read the input
if (val==1)
{
Serial.println("led is on");
digitalWrite(ledPin, HIGH);
}
else if (val == 0)
{
Serial.println("led is off");
digitalWrite(ledPin,LOW);
}
else
{
  Serial.println("Invalid");
}
}
This this what I did to it. It turns on the motors in the proper direction but I want it to constantly read the buttons i press or it goes to default 2.5vs. Also on bootup it runs the motors uncontrollably until i hit 5 enter.
int ledPin=9;
int ledPin1=10;
void setup()
{
  Serial.begin(9600); //serial begin
pinMode(9,OUTPUT);
pinMode(10, OUTPUT);
}
void loop()
{
 while (Serial.available()==0);//have the arduino wait to receive input
   int val= Serial.read()-'0';//read the input
if (val==8)
{
Serial.println("motor is on forward");
analogWrite(9, 0);
analogWrite(10, 127);
delay(500);
}
else if (val==4)
{
 Serial.println("motor is left");
analogWrite(9, 127);
analogWrite(10, 255);
}
else if (val==5)
{
 Serial.println("motor is neutral");
analogWrite(9, 127);
analogWrite(10, 127);
}
else if (val==6)
{
 Serial.println("motor is right");
analogWrite(9, 127);
analogWrite(10, 0);
}
else if (val==2)
{
  Serial.println("motor is on reverse");
analogWrite(9, 255);
analogWrite(10, 127);
}
else
{
  Serial.println("-");//Else sit in neutral.
analogWrite(9, 127);
analogWrite(10, 127);
}
{
  Serial.flush();
}}

Thanks for the help.


Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 67
Posts: 3701
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I want it to constantly read the buttons i press or it goes to default 2.5vs
Not sure what this means but for this:
Quote
Also on bootup it runs the motors uncontrollably until i hit 5 enter.
You can just take the code that runs for '5' i.e.
Code:
analogWrite(9, 127);
analogWrite(10, 127);

and put it at the end of the setup routine.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 615
Posts: 49392
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
any ideas?
Learn to post code properly. Code is supposed to be pasted only after clicking the icon with the # sign on it.

Before copying code to past, use Tools + Auto Format to do proper indentation and make the code readable.

Some appropriate white space would be nice, too.

Quote
This this what I did to it. It turns on the motors in the proper direction but I want it to constantly read the buttons i press or it goes to default 2.5vs. Also on bootup it runs the motors uncontrollably until i hit 5 enter.
So, in setup(), replicate the code that is executed when the 5 is entered.

You need to do one of two things. Either change the application on the PC to something other than the Serial Monitor, to one that does something unnatural, or restructure the Arduino program to do something different only when the Arduino receives new serial data. That is, get rid of the else block.

Whatever you do, get rid of the damned Serial.flush() statement, unless YOU can explain exactly what it is doing and why you have it there.




Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you both I did not know about putting the write function at the end of setup nor did I know about posting code.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Programming is not my forte, if you have questions about building or engineering I am all over that. I am learning straight from the internet and from guys like yourself. I will get rid of Serial.flush I was copying something from Jeremy Blum off of youtube. Very little of this makes any sense to me. That said, On the www.dimensionengineering.com/datasheets/Sabertooth2x25.pdf the datasheet says there are multiple ways of controlling the motors my example above works with a resistor capacitor circuit. I read in various different places that using pwm is not the easiest way.  Under Mode:3 Simplified Serial they ask for an 8N1 Protocol with 8 data bytes, no parity bits and one stop bit. I have searched google and arduino forum I hope someone can point me in the right direction as to what things I should look up and what my code would possibly look like... to boot, do I have to tx from d2? Right now I am trying to get my motors to move in a controllable fashion from a simple code, I would like to add a parallax Ping sensor to avoid hitting things. As always thanks for the help.
Logged

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9636
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You may just want to use the servo library and control the motors per the below. Should be very simple.

R/C input mode takes two standard R/C channels and uses those to set the speed and direction of the motor.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

impellbm, the two easiest ways to control the Sabertooth are RC servo control and simplified serial.
Dimension has sample code for RC servo control at http://www.dimensionengineering.com/arduino
They also have both Simplified and Packet Serial libraries with examples included.

Most of the Simplified Serial examples use TX/pin 1, on my Uno anyway.
If you want to use a different pin, take a look at the SoftwareSerial example that comes with the library.
« Last Edit: April 28, 2012, 11:36:03 am by James Bellinger » Logged

Pages: [1]   Go Up
Jump to: