daily pastebin goal
41%
SHARE
TWEET

TRex and serial http://darylrobotproject.wordpress.com/

a guest Oct 13th, 2012 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Included for serial communication
  2. #include <SoftwareSerial.h>
  3.  
  4. // Define pins you're using for serial communication
  5. // Do not use pins 0 or 1 as they are reserved for
  6. // standard I/O and programming
  7. #define TXPIN 4
  8. #define RXPIN 5
  9.  
  10. // Create an instance of the software serial
  11. // communication object. This represents the
  12. // interface with the TReX Jr device
  13. SoftwareSerial pololu(RXPIN, TXPIN);
  14.  
  15. // Main application entry point
  16. void setup()
  17. {
  18.   // Define the appropriate input/output pins
  19.   pinMode(RXPIN, INPUT);
  20.   pinMode(TXPIN, OUTPUT);
  21.  
  22.   // Begin communicating with the pololu interface
  23.   Serial.begin(9600);
  24.   pololu.begin(19200);
  25. }
  26.  
  27. // Main application loop
  28. void loop()
  29. {
  30.   // Loop through 127 to 0, forward
  31.   for(int i = 127; i >= 0; i--)
  32.   {
  33.     // Say that we are setting our speed
  34.     Serial.print("Setting speed to: ");
  35.     Serial.println(i, DEC);
  36.  
  37.     // Set speed to motor 0 and forward
  38.     SetSpeed(0, true, i);
  39.     SetSpeed(1, true, i);
  40.     delay(100);
  41.   }
  42.  
  43.   // Loop through 0 to 127, backward
  44.   for(int i = 0; i < 128; i++)
  45.   {
  46.     // Say that we are setting our speed
  47.     Serial.print("Setting speed to: ");
  48.     Serial.println(i, DEC);
  49.  
  50.     // Set speed to motor 0 and forward
  51.     SetSpeed(0, false, i);
  52.     SetSpeed(1, false, i);
  53.     delay(100);
  54.   }
  55. }
  56.  
  57. // Set the motor index, direction, and speed
  58. // Motor index should either be a 0 or 1
  59. // Direction should be either true for forward or false for backwards
  60. // Speed should range between 0 and 127 (inclusivly)
  61. void SetSpeed(int MotorIndex, boolean Forward, int Speed)
  62. {
  63.   // Validate motor index
  64.   if(MotorIndex < 0 || MotorIndex > 1)
  65.     return;
  66.  
  67.   // Validate speed
  68.   if(Speed < 0)
  69.     Speed = 0;
  70.   else if(Speed > 127)
  71.     Speed = 127;
  72.  
  73.   // Send the "set" command based on the motor
  74.   // Note that we do not accelerate to the
  75.   // speed, we just instantly set it
  76.   unsigned char SendByte = 0;
  77.   if(MotorIndex == 0)
  78.     SendByte = 0xC2;
  79.   else if(MotorIndex == 1)
  80.     SendByte = 0xCA;
  81.  
  82.   // If we go backwards, the commands are the same
  83.   // but minus one
  84.   if(!Forward)
  85.     SendByte--;
  86.  
  87.   // Send the set speed command byte
  88.   pololu.write(SendByte);
  89.  
  90.   // Send the speed data byte
  91.   pololu.write(Speed);
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top