Scott Orban
CE 432
HW3
Problem 1:
The half bridge and quarter bridge equations are the same as the full
bridge except they are divided by 2 and 4 respectively. In other words
the half bridge is half as sensitive as the full bridge and the quarter
bridge is half as sensitive as the half bridge and a quarter as
sensitive as the full bridge.
Problem 2:
VIDEO
Code:
const int trigPin = 12;
const int echoPin = 11;
float duration, distance;
void setup()
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(10);
digitalWrite(trigPin,LOW);
duration = pulseIn(echoPin,HIGH);
distance = (duration*.0343)/2;
Serial.print("Distance: ");
Serial.println(distance);
delay(100);
}
Problem 3:
VIDEO
Code:
#include<Wire.h>
const int MPU=0x68;//Device address
float AccX, AccY, AccZ;// X, Y, and Z acceleration values
void setup(){
Serial.begin(9600);
Wire.begin();
pinMode(7,OUTPUT);
// Configure Accelerometer Sensitivity
Wire.beginTransmission(MPU);
Wire.write(0x6B);//Reset the device, very important
Wire.write(0x00);
Wire.endTransmission(true);
Wire.beginTransmission(MPU);
Wire.write(0x1C);//Talk to the ACCEL_CONFIG register (1C hex)
Wire.write(0x08);//Set the register bits as 00001000 (+/- 4g
full scale range)
Wire.endTransmission(true);
}
void loop(){
Wire.beginTransmission(MPU);
Wire.write(0x3B);//Start with register 0x3B
Wire.endTransmission(false);
Wire.requestFrom(MPU,6,true);// Read 6 registers total
AccX=(Wire.read()<<8 | Wire.read())/8192.0;
AccY=(Wire.read()<<8 | Wire.read())/8192.0;
AccZ=(Wire.read()<<8 | Wire.read())/8192.0;
Wire.endTransmission(true);
Serial.print(AccX);
Serial.print(" ");
Serial.print(AccY);
Serial.print(" ");
Serial.println(AccZ);
if (AccZ >1.5)
{
digitalWrite(7,HIGH);
delay(500);
}
else{digitalWrite(7,LOW);}
}
Problem 4:
VIDEO
Transmitter Code:
const int X_pin = 0;
const int Y_pin = 1;
int xValue;
int yValue;
void setup() {
Serial.begin(9600);
}
void loop() {
xValue = analogRead(X_pin);
yValue = analogRead(Y_pin)*(-1);
Serial.println(xValue);
Serial.println(yValue);
}
Receiver Code:
int Data, xData, yData;
const int dirPin = 2;
const int stepPin = 3;
const int MS1 = 10;
const int MS2 = 9;
const int MS3 = 8;
void setup() {
Serial.begin(9600);
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(MS1,OUTPUT);
pinMode(MS2,OUTPUT);
pinMode(MS3,OUTPUT);
}
void loop() {
while (Serial.available()) {
char incomingByte = Serial.read();
static String incomingMessage;
if (incomingByte == '\n') {
Data = incomingMessage.toInt();
incomingMessage = "";
}
else {
incomingMessage += incomingByte;
}
}
if (Data > 0){
xData = Data;
}
else if (Data < 0){
yData = Data;
}
// Serial.print("x: ");
// Serial.println(xData);
// Serial.print("y: ");
// Serial.println(yData);
// Turn motor right at high speed
if (xData > 750 & yData < -750) {
// Set speed
digitalWrite(MS1, LOW);
digitalWrite(MS2, LOW);
digitalWrite(MS3, LOW);
// Set direction
digitalWrite(dirPin, HIGH);
// Turn motor
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
Serial.println("Right High");
}
// Turn motor right at medium speed
else if (xData <= 750 & xData >= 250 & yData <
-500) {
digitalWrite(MS1, HIGH);
digitalWrite(MS2, LOW);
digitalWrite(MS3, LOW);
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
Serial.println("Right Medium");
}
// Turn motor right at low speed
else if (xData < 250 & yData < -500) {
digitalWrite(MS1, LOW);
digitalWrite(MS2, HIGH);
digitalWrite(MS3, LOW);
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
Serial.println("Right Low");
}
// Turn motor left at high speed
else if (xData > 750 & yData > -400) {
digitalWrite(MS1, LOW);
digitalWrite(MS2, LOW);
digitalWrite(MS3, LOW);
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
Serial.println("Left High");
}
// Turn motor left at medium speed
else if (xData <= 750 & xData >= 250 & yData >
-400) {
digitalWrite(MS1, HIGH);
digitalWrite(MS2, LOW);
digitalWrite(MS3, LOW);
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
Serial.println("Left Medium");
}
// Turn motor left at low speed
else if (xData < 400 & yData > -400) {
digitalWrite(MS1, LOW);
digitalWrite(MS2, HIGH);
digitalWrite(MS3, LOW);
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
Serial.println("Left Low");
}
}