Smart House

ARDUINO CODE:

/*
3.3V VCC Power
GND GND Ground
A4 SDA I2C Data
A5 SCL I2C Clock
3 INT Interrupt
8 – Speaker
*/

#include <Wire.h>
#include <SparkFun_APDS9960.h>

// Pins
#define APDS9960_INT 3 // Needs to be an interrupt pin
#define Speaker_PIN 8 // Speaker

// Constants
#define PROX_INT_HIGH 50 // Proximity level for interrupt
#define PROX_INT_LOW 0 // No far interrupt

// Global variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
uint8_t proximity_data = 0;
int isr_flag = 0;

void setup() {

// Set speaker and communication as output
pinMode(8, OUTPUT);
pinMode(APDS9960_INT, INPUT);
pinMode(7,OUTPUT);

// Initialize Serial port
Serial.begin(9600);
Serial.println(F(“Speaker is online!”));
// Initialize interrupt routine
attachInterrupt(0, interruptRoutine, FALLING);

// Initialize proximity sensor
if ( apds.init() ) {
Serial.println(F(“APDS-9960 initialization complete”));
} else {
Serial.println(F(“Something went wrong during APDS-9960 init!”));
}

// Adjust the Proximity sensor gain
if ( !apds.setProximityGain(PGAIN_2X) ) {
Serial.println(F(“Something went wrong trying to set PGAIN”));
}

// Set proximity interrupt thresholds
if ( !apds.setProximityIntLowThreshold(PROX_INT_LOW) ) {
Serial.println(F(“Error writing low threshold”));
}
if ( !apds.setProximityIntHighThreshold(PROX_INT_HIGH) ) {
Serial.println(F(“Error writing high threshold”));
}

// Start running the proximity sensor
if ( apds.enableProximitySensor(true) ) {
Serial.println(F(“Proximity sensor is now running”));
} else {
Serial.println(F(“Something went wrong during sensor init!”));
}
}

void loop() {

// If interrupt occurs, print out the proximity level
if ( isr_flag == 1 ) {

// Read proximity level and print it out
if ( !apds.readProximity(proximity_data) ) {
Serial.println(“Error reading proximity value”);
} else {
Serial.print(“Proximity detected! Level: “);
Serial.println(proximity_data);
}

// Turn on speaker play tone at 523 Hz for 300 ms

tone(8, 523, 300);
Serial.println(“HI”);

// Reset flag and clear APDS-9960 interrupt (IMPORTANT!)
isr_flag = 0;

digitalWrite(7,LOW);
if ( !apds.clearProximityInt() ) {
Serial.println(“Error clearing interrupt”);
}

}
}

void interruptRoutine() {
digitalWrite(7,HIGH);

isr_flag = 1;

}

Code Snippet for team 4.1’s U-box

Here I will explain how the code for our U-box works. In this section everyone usually says “The code was fairly simple…”. I will say, with knowledge learned prior labs it becomes significantly easier to understand what we are doing. We figured out we needed to use the Arduino to power the servo motor due to voltage issues so we then planned to use the A3BU to control the LCD user interface. The LCD logic is controlled by the ADCB channel, J2 header, ADC1 pin PB1. Similar to the temperature conversion code we used in lab 4. After initializing the board, sysclk, sleepmgr and ADC channel I used the below expressions.In the code snippet below the ASCII faces we use can be explicitly seen, as well as the progression of faces based on the input pin PB1 and a count variable. Once the switch is flipped the input signal is converted and the voltage to PB1 becomes greater than 50 and the led begins to blink with 100% duty cycle. This signal is used to activate the LCD screen.

int mcount = 0;

static void adc_handler(ADC_t *adc, uint8_t ch_mask, adc_result_t result)
{…

if ((result > 50) && (mcount < 5)){

blink = 1;

gfx_mono_draw_string(”                      “, 0, 0, &sysfont);
gfx_mono_draw_string(”                      “, 0, 8, &sysfont);
gfx_mono_draw_string(”                      “, 0, 16, &sysfont);
gfx_mono_draw_string(”                      “, 0, 24, &sysfont);

mcount = mcount + 1;

gfx_mono_draw_string(”                      “, 0, 0, &sysfont);
gfx_mono_draw_string(”      (-)   (-)      “, 0, 8, &sysfont);
gfx_mono_draw_string(”    ________     “, 0, 16, &sysfont);
gfx_mono_draw_string(”                        “, 0, 24, &sysfont);
delay_ms(500);

gfx_mono_draw_string(”       _     _      “, 0, 0, &sysfont);
gfx_mono_draw_string(”      / \\   / \\     “, 0, 8, &sysfont);
gfx_mono_draw_string(”                       “, 0, 16, &sysfont);
gfx_mono_draw_string(”    \\_______/    “, 0, 24, &sysfont);
delay_ms(500);
}
…}