Skip to content

Instantly share code, notes, and snippets.

@jessherzog
Created April 1, 2016 01:08
Show Gist options
  • Save jessherzog/252cd4ccf8a5aa4375a2dbcfc00af5f7 to your computer and use it in GitHub Desktop.
Save jessherzog/252cd4ccf8a5aa4375a2dbcfc00af5f7 to your computer and use it in GitHub Desktop.
STUDIO/LAB MIDTERM
// 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(115200);
// Serial.write('0');
// display.begin(SSD1306_SWITCHCAPVCC, 0x3c);
// display.clearDisplay();
}
void loop() {
// raw accelerometer readings
int xRead = analogRead(Xpin);
int yRead = analogRead(Ypin);
int zRead = analogRead(Zpin);
// raw accel 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);
char input = Serial.read();
int cap1 = readCapacitivePin(capSensePin);
Serial.print(cap1);
Serial.print(",");
delay(10);
int cap2 = readCapacitivePin(capSensePin2);
Serial.print(cap2);
Serial.print(",");
delay(10);
int piezo = analogRead(piezoSensePin);
Serial.print(piezo);
Serial.print(",");
delay(10);
int temp = convertTemp(tempPin);
Serial.print(temp);
Serial.print(",");
delay(10);
int axisX = degX;
Serial.print(axisX);
Serial.print(",");
delay(10);
int axisY = degY;
Serial.print(axisY);
Serial.print(",");
delay(10);
int axisZ = degZ;
Serial.println(axisZ);
delay(100);
// }
// 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);
}
// 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