Blog 19

From BitWizard WIKI
Revision as of 09:32, 23 November 2015 by Cartridge1987 (talk | contribs)

Jump to: navigation, search

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):

  • # X is forwards - Y is backwards
  • #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.