Skip to content

Instantly share code, notes, and snippets.

@logtdm
Created November 30, 2015 01:04
Show Gist options
  • Save logtdm/d53a86629fe7a0bd319c to your computer and use it in GitHub Desktop.
Save logtdm/d53a86629fe7a0bd319c to your computer and use it in GitHub Desktop.
//Assignment Five, Geo-Location Bluetooth Device
//Taylor Martin
int motor = 9; //motor attached to pin 9
float myLat = 0;
float myLong = 0;//declaring latitude and longitude
void setup()
{
// open serial port
Serial.begin(9600);
pinMode(motor, OUTPUT);//motor as an output
}
void loop(){ //continuously runs program if requirements are met
if (getBluetoothData() > 0) //connects to bluetooth if possible
{
myLat = myLatitude(); //
myLong = myLongitude() * -1; /changing llong to positive
if ((myLat < 44.094582) && (myLat > 45.08) && (myLong < 79.132955) && (myLong > 79.14))
//latitude and longitude for Uxbridge Cemetary
{
digitalWrite(motor, HIGH);
//motor turns on if at this location
}
else
{
digitalWrite(motor, LOW);
//if the location is different then the motor stays off
}
if ((myLat < 44.116130 ) && (myLat > 44.12) && (myLong < 79.114525) && (myLong > 79.13))
//latitude and longitude for 60 Bolster Lane
{
digitalWrite(motor, HIGH);
//motor turns on if at this location
}
else
{
digitalWrite(motor, LOW);
}
//if not at this location the motor stays off
if ((myLat < 44.056922) && (myLat > 44.10) && (myLong < 79.154220) && (myLong > 79.16))
//latitude and longitude for Durham Regional Hwy
{
digitalWrite(motor, HIGH); //motor turns on if at this location
}
else
{
digitalWrite(motor, LOW);
}
//motor does not run if not at this location
}
}
// Controller Decoder Functions
// *** VARIABLES *** //
// Incoming data packet; we set aside 20 bytes of data:
uint8_t packetBuffer[21];
// Size of the incoming data packet:
uint16_t packetSize = 0;
// Number of milliseconds LilyPad will listen for data from app before moving on:
uint16_t timeout = 500;
uint16_t origTimeout = 500;
// *** FUNCTIONS *** //
uint8_t getBluetoothData()
{
// Reset timer:
origTimeout = timeout;
// Reset size counter:
packetSize = 0;
// Set all bytes of 'packetBuffer' to 0:
memset(packetBuffer, 0, 20);
// Loop until the timer has run out:
while (timeout--)
{
// If the incoming data packet is too big:
if (packetSize >= 20)
{
// Exit the 'while' loop:
break;
}
// If a complete Button data packet was received:
if ((packetBuffer[1] == 'B') && (packetSize == 5))
{
// Exit the 'while' loop:
break;
}
// If a complete Location data packet was received:
if ((packetBuffer[1] == 'L') && (packetSize == 15))
{
// Exit the 'while' loop:
break;
}
// If there is a data packet available:
while (Serial.available())
{
// Store the incoming byte:
char c = Serial.read();
// If this is the first byte of the packet (indicated by '!'):
if (c == '!')
{
// Reset the size counter to 0:
packetSize = 0;
}
// Store the current byte in 'packetBuffer':
packetBuffer[packetSize] = c;
// Increment the size counter:
packetSize++;
// Reset the timer:
timeout = origTimeout;
}
// If the timer has run out:
if (timeout == 0)
{
// Exit the 'while' loop:
break;
}
// Quick delay to be safe:
delay(1);
}
// Null term:
packetBuffer[packetSize] = 0;
// If data was not successfully received:
if (!packetSize)
{
// Return null:
return 0;
}
// If the data packet is incomplete:
if (packetBuffer[0] != '!')
{
// Return null:
return 0;
}
// Perform a checksum - a type of error detection:
uint8_t xsum = 0;
uint8_t checksum = packetBuffer[packetSize - 1];
for (uint8_t i = 0; i < packetSize - 1; i++)
{
xsum += packetBuffer[i];
}
xsum = ~xsum;
// If checksume failed:
if (xsum != checksum)
{
// Return null:
return 0;
}
return packetSize;
}
// Cast the four bytes at the specified address to a float:
float parsefloat(uint8_t * buffer)
{
float f = ((float *) buffer)[0];
return f;
}
// Return your current latitude:
float myLatitude()
{
// If Location data is received:
if (packetBuffer[1] == 'L')
{
// Extract latitude data:
return parsefloat(packetBuffer + 2);
}
else
{
return 0;
}
}
// Return your current longitude:
float myLongitude()
{
// If Location data is received:
if (packetBuffer[1] == 'L')
{
// Extract longitude data:
return parsefloat(packetBuffer + 6);
}
else
{
return 0;
}
}
// Return your current altitude:
float myAltitude()
{
// If Location data is received:
if (packetBuffer[1] == 'L')
{
// Extract altitude data:
return parsefloat(packetBuffer + 10);
}
else
{
return 0;
}
}
//Assignment Five
//Taylor Martin
//Code I used for Example purposes because my Bluetooth did not
//want to cooperate!
//LilyPad vibration motor attached to pin 9
int motor = 9; // motor is connected to digital pin 9
void setup()
{
pinMode(motor, HIGH);
}
void loop() // run over and over again
{
digitalWrite(motor, HIGH); // turn the motor on
delay(1000); // delay for 1 second
digitalWrite(motor, LOW); // turn the motor off
delay(1000); // delay for 1 second
}
//Assignment Five
//Taylor Martin
//If I had chosen to use the Temperature Sensor this would be the
//code for it. As I said in class, having to activate the device
//did not fit with my Narrative so I chose to keep it out.
//I figured I'd include it for hypothetical purposes!
//pin variable
int sensorPin = 0;
void setup()
{
Serial.begin(9600); //serial connection with computer
}
void loop() // runs program continuously
{
//voltage reading fromtemperature sensor
int reading = analogRead(sensorPin);
// converting reading to voltage
float voltage = reading * 5.0;
voltage /= 1024.0;
// print out voltage
Serial.print(voltage); Serial.println(" volts");
// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((voltage - 500mV) times 100)
Serial.print(temperatureC); Serial.println(" degrees C");
// now convert to Fahrenheit
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF); Serial.println(" degrees F");
delay(1000);
//second waiting
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment