CE 432 Robotics II HW3
James Ferguson

1.
Using a half Wheatstone bridge removes error due to temperature changes. The full Wheatstone bridge delivers a voltage porportional to the strain while the others do not.

2.


MPU6050_Demo.ino
class HCSR04 {
  public:
  HCSR04 (int trig, int echo) {
    trig_ = trig;
    echo_ = echo;
    pinMode(trig, OUTPUT);
    pinMode(echo, INPUT);
  }
 
  float measure() {
    digitalWrite(trig_, HIGH);
    delayMicroseconds(10);
    digitalWrite(trig_, LOW);
   
    return((pulseIn(echo_, HIGH)) / 58.0);
  }
 
  private:
  int trig_;
  int echo_;
};
 
HCSR04 distanceSensor(2, 3);
 
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}
 
void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(distanceSensor.measure());
  delay(100);
}

3.


MPU6050_Z_Motion_Detector.ino
#include <Wire.h>
const byte accelerometerAddress = 0x68;
 
void setup() {
  Serial.begin(9600);
  Wire.begin();
 
  // configure
  Wire.beginTransmission(accelerometerAddress);
  Wire.write(0x1B); // select GYRO_CONFIG
  Wire.write(0x08); // write 00001000, no self test, +/- 4g
  Wire.endTransmission();
 
  Wire.beginTransmission(accelerometerAddress);
  Wire.write(0x6B); // select PWR_MGMT_1
  Wire.write(0x00); // write 000000000, turn off sleep mode use 8MHz oscilator
  Wire.endTransmission();
}
 
float lastAccelerometerZ = 0;
 
void loop() {
  Wire.beginTransmission(accelerometerAddress);
  Wire.write(0x3B); // start at ACCEL_XOUT_H
  Wire.endTransmission(false);
 
  // request registers ACCEL_XOUT_H through ACCEL_ZOUT_L
  Wire.requestFrom(accelerometerAddress, 6, true); // request 6 bytes
 
  // read data into memory and scale to g's
  float accelerometerX;
  float accelerometerY;
  float accelerometerZ;
  if (Wire.available() == 6) {
    accelerometerX = (Wire.read() << 8 | Wire.read()) / 8192.0;
    accelerometerY = (Wire.read() << 8 | Wire.read()) / 8192.0;
    accelerometerZ = (Wire.read() << 8 | Wire.read()) / 8192.0;
  }
  Wire.endTransmission(true);
 
  // print data
  Serial.print(accelerometerZ);
  Serial.println();
 
  // if detetected change in Z positional velocity, make a C4 tone for 100ms
  if (abs(accelerometerZ - lastAccelerometerZ) > 0.5) {
    tone(2, 262, 100); 
  }
 
  // update last value
  lastAccelerometerZ = accelerometerZ;
}

4.


Joystick_TX.ino
#include <SoftwareSerial.h>
 
const int joystickYPin = A7;
int joystickMaxValue = 0;
 
SoftwareSerial BlueSerial(2, 3); // RX, TX
 
void setup() {
  // set the data rate for the SoftwareSerial port
  BlueSerial.begin(9600);
}
 
void loop() {
  int joystickValue = analogRead(joystickYPin);
  if (joystickMaxValue < joystickValue) {
    joystickMaxValue = joystickValue;
  }
  BlueSerial.println(int(float(joystickValue) * 1023.0 / float(joystickMaxValue)));
}

Stepper_RX.ino
#include <SoftwareSerial.h>
 
SoftwareSerial BlueSerial(2, 3); // RX, TX
 
const int dirPin = 4;
const int stepPin = 5;
 
int joystickValue = 0;
int joystickDeadzone = 255;
int joystickCenter = 511;
 
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Native USB only
  }
 
  // set the data rate for the SoftwareSerial port
  BlueSerial.begin(9600);
}
 
void loop() {
  // Read in bytes and convert to int with '\n' delimiter
  while (BlueSerial.available()) {
    char incomingByte = BlueSerial.read();
    static String incomingMessage;
    if (incomingByte == '\n') {
      joystickValue = incomingMessage.toInt();
      incomingMessage = "";
    } else {
      incomingMessage += incomingByte;
    }
  }

  // Set Direction
  if (joystickValue - joystickCenter > joystickDeadzone) {
    digitalWrite(dirPin, HIGH);
    // Step
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
 
    Serial.print("R ");
    Serial.println(joystickValue - joystickCenter);
   
  } else if (joystickValue - joystickCenter < -joystickDeadzone) {
    digitalWrite(dirPin, LOW);
    // Step
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(2000);
    digitalWrite(stepPin, LOW);
    delayMicroseconds(2000);
 
    Serial.print("L ");
    Serial.println(joystickValue - joystickCenter);
  }
}