Sunteți pe pagina 1din 4

/*

CoasterBot Master
Black Dog Robotics

I2C master, sensor input


reads inputs, controls motor speed, etc.
*/
#include <Wire.h>

union BYTE2INT {
byte asBytes[2];
int asInt;
} byteToint;

byte msg1[3];
byte msg2[5];

// set pin numbers:


#define BUTTON_PIN 8 // the number of the
pushbutton pin
#define LED_PIN 13 // the number of the LED
pin

void setup() {
// join i2c bus (address optional for master)
Wire.begin();
// initialize the LED pin as an output:
pinMode(LED_PIN, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(BUTTON_PIN, INPUT);
// flash ready
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
// wait for button press
while(digitalRead(BUTTON_PIN) == HIGH)
{
}
}
//
//
//
void loop()
{
// for now, just flash to indicate that we're not
waiting any more
// turn LED on
digitalWrite(LED_PIN, HIGH);
delay(100);
// turn LED off
digitalWrite(LED_PIN, LOW);
delay(100);
// forward full speed
byteToint.asInt = 255;
msg1[0] = 1;
msg1[1] = byteToint.asBytes[0];
msg1[2] = byteToint.asBytes[1];
Wire.beginTransmission(4); // transmit to device
#4
Wire.send(msg1, 3); // sends 3 bytes

Wire.endTransmission(); // stop transmitting


delay(1000);
// stop
byteToint.asInt = 0;
msg1[0] = 1;
msg1[1] = byteToint.asBytes[0];
msg1[2] = byteToint.asBytes[1];
Wire.beginTransmission(4); // transmit to device
#4
Wire.send(msg1, 3); // sends 3 bytes
Wire.endTransmission(); // stop transmitting
delay(1000);
// reverse full speed
byteToint.asInt = -255;
msg1[0] = 1;
msg1[1] = byteToint.asBytes[0];
msg1[2] = byteToint.asBytes[1];
Wire.beginTransmission(4); // transmit to device
#4
Wire.send(msg1, 3); // sends 3 bytes

Wire.endTransmission(); // stop transmitting


delay(1000);
// stop
byteToint.asInt = 0;
msg1[0] = 1;
msg1[1] = byteToint.asBytes[0];
msg1[2] = byteToint.asBytes[1];
Wire.beginTransmission(4); // transmit to device
#4
Wire.send(msg1, 3); // sends 3 bytes

Wire.endTransmission(); // stop transmitting


delay(1000);
// spin CCW
byteToint.asInt = 255;
msg2[0] = 2;
msg2[1] = byteToint.asBytes[0];
msg2[2] = byteToint.asBytes[1];
byteToint.asInt = -255;
msg2[3] = byteToint.asBytes[0];
msg2[4] = byteToint.asBytes[1];
Wire.beginTransmission(4); // transmit to device
#4
Wire.send(msg2, 5); // sends 5 bytes

Wire.endTransmission(); // stop transmitting


delay(1000);
// stop
byteToint.asInt = 0;
msg1[0] = 1;
msg1[1] = byteToint.asBytes[0];
msg1[2] = byteToint.asBytes[1];
Wire.beginTransmission(4); // transmit to device
#4
Wire.send(msg1, 3); // sends 3 bytes
Wire.endTransmission(); // stop transmitting
delay(1000);
// spin CW
byteToint.asInt = -255;
msg2[0] = 2;
msg2[1] = byteToint.asBytes[0];
msg2[2] = byteToint.asBytes[1];
byteToint.asInt = 255;
msg2[3] = byteToint.asBytes[0];
msg2[4] = byteToint.asBytes[1];
Wire.beginTransmission(4); // transmit to device
#4
Wire.send(msg2, 5); // sends 5 bytes
Wire.endTransmission(); // stop transmitting
delay(1000);
// stop
byteToint.asInt = 0;
msg1[0] = 1;
msg1[1] = byteToint.asBytes[0];
msg1[2] = byteToint.asBytes[1];
Wire.beginTransmission(4); // transmit to device
#4
Wire.send(msg1, 3); // sends 3 bytes

Wire.endTransmission(); // stop transmitting


delay(1000);
}

S-ar putea să vă placă și