Blog 19

From BitWizard Wiki
Revision as of 10:31, 23 November 2015 by Cartridge1987 (talk | contribs) (Created page with " == 2 Wheeled car: Arduino Version == #define Motor 0x48 #include <Wire.h> //Time to pause rotating //static unsigned long Pause = 10000; static unsigned long Spee...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

2 Wheeled car: Arduino Version

#define Motor 0x48

#include <Wire.h> 

//Time to pause rotating
//static unsigned long Pause = 10000;
static unsigned long Speed = 40;

void setup()
{
  Wire.begin(); // wake up I2C bus
  Serial.begin(9600);
}

byte get_var(byte address, byte reg)
{
  byte value;   
  Wire.beginTransmission(address);  
  Wire.write(reg);
  Wire.endTransmission(); 
  delayMicroseconds (10);
  Wire.requestFrom(Motor, 1);
  delayMicroseconds (10);
  value = Wire.read();
  delayMicroseconds  (10);
  return value;
} 

void set_var(byte address, byte reg, byte value)
{
  Wire.beginTransmission(address); // transmit to motor     
 delayMicroseconds  (10);  
  Wire.write(reg);
   delayMicroseconds  (10);
  Wire.write(value);   
   delayMicroseconds  (10);
  Wire.endTransmission();    // stop transmitting
} 

void loop()
{
  unsigned long AddressA;
  unsigned long AddressB;  

  char buf[32];
  set_var(0x48, 0x21, Speed); 
   delayMicroseconds  (10);
  set_var(0x48, 0x31, Speed);  

  AddressA = get_var(0x48, 0x21);
  sprintf (buf, "Speed:  A:%d ", AddressA);
  Serial.write (buf); 

  AddressB = get_var(0x48, 0x31);
  sprintf (buf, " B:  %d\r\n", AddressB);
  Serial.write (buf); 
 
 delay(500);

//  delay(Pause);
}

This is the list I have made for the Raspberry Pi Version, what also counts for the Arduino version(If your car is switched to the other side this can of course be the opposite):

    1. X is forwards - Y is backwards
    2. Wheels at front
  • #20 A backwards
  • #21 A forwards
  • #22 A stop
  • #30 B backwards
  • #31 B forwards
  • #32 B stop

You can also check the motor protocol, for more info.