Created
March 28, 2016 20:09
-
-
Save jessherzog/0b1e13525691f4ee1c62 to your computer and use it in GitHub Desktop.
visual repair
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// global stuff | |
unsigned long cap1, cap2, piezo, temp, axisX, axisY, axisZ; | |
// lcd | |
//#include <SPI.h> | |
//#include <Adafruit_SSD1306.h> | |
//#include <Adafruit_GFX.h> | |
//#include <Wire.h> | |
//#define OLED_RESET 4 | |
//#define SSD1306_128_64 | |
//Adafruit_SSD1306 display(OLED_RESET); | |
//#define OLED_address 0x3c | |
// accel | |
int Xpin = 5; | |
int Ypin = 4; | |
int Zpin = 3; | |
int accMin = 2; | |
int accMax = 485; | |
double accX; | |
double accY; | |
double accZ; | |
// temp sensor | |
// chose to use Fahrenheit over Celsius because | |
// number changes are more drastic. | |
// C: -40 – 50 vs F: -40 – 120 | |
// range: in 76 / out 700 | |
int tempPin = 0; | |
// piezo sensor | |
// 5V -> 10ohm -> same side: red, opp side: A1 -> black | |
// plugged-in: 0 - 1021 | |
// unplugged: 402 - 408 | |
int piezoSensePin = 1; | |
// cap sensors | |
// 5V -> 10ohm -> D4 / sensor | |
// 5V -> 10ohm -> D7 / sensor | |
// range: 16 - 243 | |
int capSensePin = 4; | |
int capSensePin2 = 7; | |
void setup() { | |
Serial.begin(9600); | |
while (!Serial) {;} | |
// display.begin(SSD1306_SWITCHCAPVCC, 0x3c); | |
// display.clearDisplay(); | |
establishContact(); | |
} | |
void loop() { | |
// raw accelerometer readings | |
int xRead = analogRead(Xpin); | |
int yRead = analogRead(Ypin); | |
int zRead = analogRead(Zpin); | |
// reading -> radians -> degrees | |
int xAng = map(xRead, accMin, accMax, -90, 90); | |
int yAng = map(yRead, accMin, accMax, -90, 90); | |
int zAng = map(zRead, accMin, accMax, -90, 90); | |
int degX = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI); | |
int degY = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI); | |
int degZ = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI); | |
if (Serial.available() > 0) { | |
char inByte = Serial.read(); | |
cap1 = readCapacitivePin(capSensePin); | |
delay(10); | |
cap2 = readCapacitivePin(capSensePin2); | |
delay(10); | |
piezo = analogRead(piezoSensePin); | |
delay(10); | |
temp = convertTemp(tempPin); | |
delay(10); | |
axisX = degX; | |
delay(10); | |
axisY = degY; | |
delay(10); | |
axisZ = degZ; | |
delay(10); | |
// send to serial port | |
Serial.write(cap1); | |
Serial.write(cap2); | |
Serial.write(piezo); | |
Serial.write(temp); | |
Serial.write(axisX); | |
Serial.write(axisY); | |
Serial.write(axisZ); | |
} | |
// lcd text | |
// display.display(); | |
// display.clearDisplay(); | |
// | |
// display.setTextSize(1); | |
// display.setTextColor(WHITE); | |
// display.setCursor(0, 0); | |
// display.print("0: "); | |
// display.println(cap1); | |
// display.print("1: "); | |
// display.println(cap2); | |
// display.print("2: "); | |
// display.println(piezo); | |
// display.print("3: "); | |
// display.println(temp); | |
// display.print("4: "); | |
// display.println(axisX); | |
// display.print("5: "); | |
// display.println(axisY); | |
// display.print("6: "); | |
// display.print(axisZ); | |
} | |
void establishContact() { | |
while (Serial.available() <= 0) { | |
Serial.print('r'); | |
delay(300); | |
} | |
} | |
// voltage -> Celsius -> Fahrenheit | |
int convertTemp(float t) { | |
float tempRead = analogRead(t); | |
float voltage = tempRead * 5.0; | |
voltage /= 1024.0; | |
float tempC = (voltage - 0.5) * 100; | |
float tempF = (tempC * 9.0 / 5.0) + 32.0; | |
return tempF; | |
} | |
uint8_t readCapacitivePin(int pinToMeasure) { | |
volatile uint8_t* port; | |
volatile uint8_t* ddr; | |
volatile uint8_t* pin; | |
// Here we translate the input pin number from | |
// Arduino pin number to the AVR PORT, PIN, DDR, | |
// and which bit of those registers we care about. | |
byte bitmask; | |
if ((pinToMeasure >= 0) && (pinToMeasure <= 7)) { | |
port = &PORTD; | |
ddr = &DDRD; | |
bitmask = 1 << pinToMeasure; | |
pin = &PIND; | |
} | |
// set pin to low + output | |
*port &= ~(bitmask); | |
*ddr |= bitmask; | |
delay(1); | |
// make pin an input WITHOUT the internal pull-up on | |
*ddr &= ~(bitmask); | |
// Now see how long the pin to get pulled up | |
int cycles = 16000; | |
for (int i = 0; i < cycles; i++) { | |
if (*pin & bitmask) { | |
cycles = i; | |
break; | |
} | |
} | |
*port &= ~(bitmask); | |
*ddr |= bitmask; | |
return cycles; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment